子类调用父类方法(super关键字)
package testextends;
public class Pad3 extends Computer3{ //子类:平板电脑
String sayhello(){ //重写父类方法‘打招呼’
return super.sayHello()+ "ipad";//调用父类方法并添加字符串
}
public static void main(String[] args) {
Computer3 pc = new Computer3(); //实例化父类对象
System.out.println(pc.sayHello());
Pad3 iPad =new Pad3(); //实例化子类对象
System.out.println(iPad.sayhello());
}
}
class Computer3{ //父类:电脑
public String sayHello() { //方法:打招呼
return "欢迎使用";
}
}
还没有评论,来说两句吧...