关于隐藏和覆盖的区别,要提到RTTI(run-time type
identification)(运行期类型检查),也就是运行期的多态,当一个父类引用指向子类对象的时候,请看下面我编写的一段代码:
public class RunTime {
public
static void main(String[]
args) {
Animal
a = new Cat();
System.out.println(a.A);
System.out.println(a.b);
a.voice();
a.method();
}
}
class Dog extends Animal {
public
int b = 3;
public
static int A =
3;
public
static void method(){
System.out.println("狗");
}
public
void voice()
{
System.out.println("狗叫");
}
}
class Cat extends Animal {
public
int b = 4;
public
static int A =
4;
public
static void method(){
System.out.println("猫");
}
public
void voice()
{
System.out.println("猫叫");
}
}
class Animal {
public
int b = 0;
public
static int A =
0;
public
static void method(){
System.out.println("动物");
}
public
void voice()
{
System.out.println("动物叫");
}
}
|
输出结果是:
0
0
猫叫
动物
您可以看到,当父类Animal的引用a指向子类Dog时,RTTI在运行期会自动确定该引用的真是类型,当子类
覆盖
了父类的方法时,则直接调用子类的方法,打印出“猫叫”;然而非静态的方法在子类中重写的话就是被覆盖,而静态的方法被子类重写的话就是隐藏,另外,静态变量和成员变量也是被隐藏,而RTTI是只针对覆盖,不针对影藏,所以,静态变量
A 和 非静态变量 b 以及静态方法method()
均不通过RTTI,是哪个类的引用就调用谁的静态方法,成员变量,而这里是父类Animal的引用,所以直接调用父类Animal中的方法以及成员变量。所以静态方法
method(), 静态变量 A 和成员变量 b
打印结果全是父类中的。只用被覆盖的非静态方法voice()才打印子类的。
如果
public static void main(String[] args) {
Animal a = new Cat();
System.out.println(a.A);
System.out.println(a.b);
System.out.println(((Cat)a).A);
System.out.println(((Cat)a).b);
a.voice();
a.method();
((Cat)a).voice();
((Cat)a).method();
}
则会输出:
0
0
4
4
猫叫
动物
猫叫
猫