1. 阻止继承:final类和final方法
使用关键字final定义的类不允许被继承
使用关键字final定义的方法不允许被子类覆盖,final类中的方法自动为final方法
成员变量如果被声明为final,则构造对象后不允许改变它们的值。对于final类,成员变量不会自动final
2. 控制可见性的4个访问修饰符
1)仅对本类可见——private
2)对本包可见——默认
3)对本包和所有子类可见——protected
4)对所有类可见——public
3. equals方法
该方法用于判断两个对象是否相当,可重写该方法
class Employee {
public boolean equals(Object otherObject) {
//a quick test to see if the objects are identical
if(this == otherObject) {
return true;
}
//must return false if the explicit parameter is null
if(otherObject == null) {
return false;
}
//if the classes don't match, they can't be equal
if(getClass() != otherObject.getClass()) {
return false;
}
//check whether otherObject is Employee
if(!(otherObject instanceof Employee) {
return false;
}
//now we know otherObject is a non-null Employee
Employee other = (Employee) otherObject;
//test whether the fields have identical values
return name.equals(other.name) && salary==other.salary && hireDay.equals(other.hireDay);
}
}