多态性
多个样子,
Person e = new Student;
e.school = “xxx”; //报错,e是School类型,Person中无School属性,类中无School
父类类型的使用可以指向子类对象
调用方法时,与之相反(和内存有关)
instanceof操作符
看看对象是不是子类
Student s = new Student();
Person p = new Person();
System.out.println(s instanceof Person);
//输出turn,说明子类属于Person
System.out.println(p instanceof Person)
//输出turn
System.out.println(p instanceof Student)
//输出false
Person e = new Student();
System.out.println(e instanceof Student);
//输出turn
Object类
Equals:判断是不是一个对象
例如:
System.out,println(p.equals(e));
//输出turn或false
hashCode方法:获得hash码
toString()方法:引用对象所在地址
父类可以接受子类的对象
Objact o = new Student();
//没问题,
Objact o = new Person();
//可以用父类对象接受任何子类的实例
类型转换
对象的转换: 子类:Student 父类:Person
Student s = new Student();
Person p = s; //自动转换
Person p = new Person();
Student s = (Student) p ; //强制转换
无父子类关系是非法的
Objact是所有类的父类,可自动转换
操作符与equals方法
Person p1 = new Person();
Person p2 = new Person();
System.out.println( p1== p2);
//输出false,不是指向一个对象
当:Person p2 =p1;
//输出turn
当:Test t = new Test();
System.out.println( p1== t); //报错,之间无关系,不兼容