1、instanceof关键词
A instanceof B是用来判断内存中的实际对象A是不是B类型(具体事例看3小节代码)
2、重写条件:
1、继承关系
2、子类重写父类方法
3、方法名一样,参数个数和类型一样
3、继承
子类Student继承父类Person,实例化子类对象student的时候,在堆中会为其分配内存空间,在其内存空间中再划分一部分空间作为实例化一个父类new Person()的内存空间,这个父类对象的属性方法只为子类对象student服务
装箱和拆箱
public class Person {
String name;
}
public class Student extends Person{
int height;
public void study() {
System.out.println("学习");
}
}
public class Test05 {
public static void main(String[] args) {
Person p = new Person();
Student s = new Student();
//instanceof可用于if判断中
System.out.println(p instanceof Person); //true
System.out.println(s instanceof Student); //true<