继承一个类
实例变量和动态变量称为域 ,类中的域,方法和嵌套类/接口统称为类成员 反射机制 :一种在程序运行期间查找类及其成员的能力
return **super.**getSalary()+bonus;
因为Manager类的构造函数,不能访问Employee类的私有变量,所以他必须通过父类的构造函数来初始化他们。父类构造函数的调用必须是子类构造函数的第一条语句。
public Manager(String name,double salary){
super(name,salary);
bonus = 0;
}
不同于接口,抽象类可以拥有实例变量和构造函数。构造抽象类的实例是不可能的,但是你可以拥有一个类型为抽象类的变量,前提是该变量引用一个具体的子类的对象。
public class Student extends Person{
private int id;
public Student(String name,int id){ super(name);this.id=id;}
public int getId{return id;}
}
Person p = new Student("April",1996);//OK,student是具体的子类
Object:终极父类
hashcode和equals方法必须是兼容的 克隆对象
浅拷贝 object.clone 他简单的从源对象中拷贝所有实例变量到被拷贝对象里,如果实力变量都是基本类型或者不会改变,那没问题,但是如果他们不是原对象和克隆对象,将共享可变的状态,这会有问题。 深拷贝 例如有列表变量等,改变其一另一个也会被改变
一般情况下,当你需要实现一个类,使你要考虑如下情况,是否提供克隆方法。如果不提供克隆方法,那么继承自父类的克隆方法是否可以接受,如果继承自父类的克隆方法不可接受。就需要提供实现深拷贝的克隆方法。 如果需要深拷贝,你的类必须实现cloneable接口,这是一个没有任何方法的接口,称作标签接口。克隆方法执行浅拷贝之前会检查这个接口是否被实现,如果没有会抛出异常。