关于使用 super 和 父类名调用父类成员的区别
super 的用法分为两种 (一般在有继承关系的时候才会用):
- 调用父类中被重写或被隐藏的成员,(也可以访问非隐藏或重写的成员);
class F {
public void display(){
System.out.println(" Father ");
}
}
class S extends F {
public void display(){
super.display();
}
}
父类名调用成员用法:
- 子类只能用父类名调用父类 static 成员;
- 在非继承关系时可以使用;
一下举个例子
public class Atext {
public void show() {
System.out.println("non-static Show() ");
}
public static void print() {
System.out.println("static print() ");
}
}
class D extends Atext {
public void show() {
super.show();
super.print();
Atext.show();
Atext.print();
}
}
class E {
public void run() {
Atext.print();
Atext.show();
}
}