public class ExtendsTest {
public static void main(String[] args) {
System.out.println("--------父类引用指向子类对象-----------");
Father instance = new Son();
instance.printValue();
instance.printValue2();
System.out.println(instance.getClass());
// 通过父类变量引用子类对象,方法签名相同会引用子类的(除非是静态方法或者子类没有),属性相同会继续引用父类
System.out.println(instance.name);// name引用father的
System.out.println(instance.staticName);
System.out.println(Father.staticName);
instance.printValue3();
System.out.println("----------创建子类对象------------");
// 通过子类构造对下,方法签名相同会引用子类的(除非子类没有),属性相同会继续引用子类的,(除非子类没有)
Son son = new Son();
son.printValue();
son.printValue2();
System.out.println(son.name);
System.out.println(son.name2);
System.out.println(son.staticName4);
System.out.println(son.staticName2);
System.out.println(son.staticName);
son.printValue3();
System.out.println("---------创建父类对象-----------");
// 通过父类构造对,不能引用子类的方法或属性
Father father = new Father();
System.out.println(father.getClass());
father.printValue();
father.printValue2();
System.out.println(father.name);
System.out.println(father.staticName);
father.printValue3();
}
}
class Father {
public String name = "father";
public String name2 = "father";
public static String staticName = "staticName-father";
public String staticName4 = "staticName4-father";
public void printValue() {
System.out.println("this is father's printValue() method.---"+this.name);
}
public void printValue2(){
System.out.println("this is father's printValue2() method.---"+this.name);
System.out.println(getClass());
}
public static void printValue3(){
System.out.println("this is father's static printValue3() method.");
}
}
class Son extends Father {
public String name = "Son";
public static String staticName2 = "staticName2-son";
public static String staticName = "staticName-son";
public void printValue() {
System.out.println("this is Son's printValue() method.---"+this.name);
}
public static void printValue3(){
System.out.println("this is Son's static printValue3() method.");
}
public static void printValue4(){ // 通过父类引用不了,只能父类有
System.out.println("this is Son's static printValue4() method.");
}
}
输出
--------父类引用指向子类对象-----------
this is Son's printValue() method.---Son
this is father's printValue2() method.---father
class hw.基础学习.继承.Son
class hw.基础学习.继承.Son
father
staticName-father
staticName-father
this is father's static printValue3() method.
----------创建子类对象------------
this is Son's printValue() method.---Son
this is father's printValue2() method.---father
class hw.基础学习.继承.Son
Son
father
staticName4-father
staticName2-son
staticName-son
this is Son's static printValue3() method.
---------创建父类对象-----------
class hw.基础学习.继承.Father
this is father's printValue() method.---father
this is father's printValue2() method.---father
class hw.基础学习.继承.Father
father
staticName-father
this is father's static printValue3() method.
分析:
1、父类引用指向子类对象,对象调用的方法如果已经被子类重写过了则调用的是子类中重写的方法,而不是父类中的方法;
2、父类引用指向子类对象,如果想要调用子类中和父类同名的成员变量,则必须通过getter方法或者setter方法;
3、父类引用指向子类对象,如果想调用子类中和父类同名的静态方法,直接子类“类名点” 操作获取,不要通过对象获取;
4、父类引用指向子类对象的两种写法:(第二种得了解,面试中可能用到)
// 1、第一种常规写法
Father instance = new Son();
// 2、第二种变形写法;
Son son = new Son();
Father mson = (Father) son;