/**
* By slee on 2023/3/20
* 父类引用指向子类对象,这个引用既属于子类,又属于父类
* 但是如果各自创建对象的话,父类对象就不属于子类
*/
public class Test {
public static void main(String[] args) {
Circle circle = new Circle();//子类自建对象
Shape shape = new Shape();//父类自建对象
System.out.println(circle instanceof Shape);//true,子类对象属于父类
System.out.println(shape instanceof Circle);//false,父类对象不属于子类
System.out.println("----------------------------");
Shape s = new Circle();//父类引用指向子类对象,这个【多态指针既属于子类,又属于父类】
System.out.println(s instanceof Circle);//true
System.out.println(s instanceof Shape);//true
}
}
标签:instanceof,子类,多态,System,Circle,父类,out
From: https://www.cnblogs.com/shallow920/p/17237637.html