编写父类Phone.java
package com.iflytek.day09.demo06;
public class Phone {
public void call(){
System.out.println("打电话");
}
public void send(){
System.out.println("发短信");
}
public void show(){
System.out.println("显示电话");
}
}
编写子类NewPhone.java
package com.iflytek.day09.demo06;
// 定义一个新手机,使用老手机作为父类
public class NewPhone extends Phone {
//这里因为继承了父类Phone,直接写show+回车即可自动生成
@Override
public void show() {
super.show(); // 把父类的show方法拿过来重复利用
// 自己子类再来添加更多内容
System.out.println("显示姓名");
System.out.println("显示头像");
}
}
编写主类Demo01Phone.java
package com.iflytek.day09.demo06;
public class Demo01Phone {
public static void main(String[] args) {
Phone phone = new Phone();
phone.call();
phone.send();
phone.show();
System.out.println("==========");
NewPhone newPhone = new NewPhone();
newPhone.call();
newPhone.send();
newPhone.show();
}
}