看下面一段关于继承的Java代码
public class Demo2 {
public static void main(String args[]){
Dog obj = new Dog();
obj.move();
}
}
class Animal{
private String desc = "Animals are human's good friends";
public String getDesc(){return desc;}
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
super.move();
System.out.println("Dogs can walk and run");
System.out.println("Please remember: "+ this.getDesc());
}
}
上一段代码中getDesc()方法可以使用this,因为此方法已被继承,推荐使用super。
而move()方法只能使用super,因为move方法在Dog本类中被重写了。