instanceOf 只要有父子类关系,sout(A instanceOf B)结果就位ture,反之false 先前定义 class Person ; method calss Father ; method money() class Son:method go() calss Object 类型转换 父转子:向下造型, 优点 缺点 强制转 Father obj= new Son() ; Father.go(); //这时候不行,是错的,父类不可用子类方法 ,要用就得强制转换,如下 e.g.
we have four specific casting operators:dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses
Java引用变量有两个类型:编译时类型和运行时类型.编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定. 若编译时类型和运行时类型不一致,就出现多态 正常的方法调用(本态调用) Person e = new Person(); e.say(); Student e = new Student(); e.say; 虚拟方法调用(多态情况下) Person e = new Student(); e.say(); // 调用Student类的say()方法 编译时类型和运行时
Instanceof: 判断一个对象是什么类型的~,可以判断两个类之间是否存在父子关系 package com.oop.demo07; public class Person { public void run(){ System.out.println("run"); } } package com.oop.demo07; public class Student extends Person { public void go() { System.out.println("