java实验:为person增加以下方法:
public int getAge(int year) //返回当前对象在year年份的年龄
public int getAge() //返回当前对象今年的年龄,重载
public int olderThen(person per)//返回this与per对象出生年份差值,按年龄比较大小
public boolen equals(person per)//比较当前对象与per引用实例对应成员变量值是否相等
class Person {
private int YearOfBirth;
public Person(int y) { YearOfBirth = y; }
public int getAge(int year) { //返回当前对象在year年份的年龄
return year - this.YearOfBirth;
}
public int getAge() { //返回当前对象今年的年龄,重载
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
return getAge(year);
}
public int olderThan(person per) { //返回this与per对象出生年份差值,按年龄比较大小
return this.YearOfBirth - per.YearOfBirth;
}
public boolean equals(person per) { //比较当前对象与per引用实例对应成员变量值是否相等
return this.YearOfBirth == per.YearOfBirth;
}
}