day09--继承重写的应用案例

This commit is contained in:
2026-01-05 15:02:55 +08:00
parent 292c0b8cc4
commit a36c25d3b8
3 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package com.inmind.extends_override03.test;
import java.awt.print.PrinterJob;
/*
重写的应用案例
*/
public class Demo05 {
public static void main(String[] args) {
//创建一个旧手机
Phone phone = new Phone();
phone.call();
phone.send();
phone.showNumber();
//创建一个新手机
NewPhone newPhone = new NewPhone();
newPhone.call();
newPhone.send();
newPhone.showNumber();
}
}

View File

@@ -0,0 +1,14 @@
package com.inmind.extends_override03.test;
public class NewPhone extends Phone{
//对原本的功能call()send()进行了沿用
//对原本的来电显示要扩展新的功能
@Override
public void showNumber(){
//要求:显示电话号码要沿用,扩展显示头像和归属地
super.showNumber();
System.out.println("显示头像");
System.out.println("显示归属地");
}
}

View File

@@ -0,0 +1,15 @@
package com.inmind.extends_override03.test;
public class Phone {
public void call(){
System.out.println("打电话");
}
public void send(){
System.out.println("发短信");
}
public void showNumber(){
System.out.println("显示电话号码");
}
}