Java中的多态
public class Father{
public int a = 3;
public void method(){
System.out.println("这是父类中的方法");
}
}
public class Son extends Father{
public int a = 9;
public void method(){
System.out.println("这是子类中的方法");
}
}
public class Test{
public static void main(String[] args){
Father fa = new Son();
System.out.println(fa.a);
fa.method();
}
}
结果如下
说明父类向子类转型时,属性是自己的,方法是子类的。即编译时看等号左边,运行时看等号右边。