java实验:为person增加以下方法:
public int getAge(int year) //返回当前对象在year年份的年龄
public int getAge() //返回当前对象今年的年龄,重载
public int olderThen(person per)//返回this与per对象出生年份差值,按年龄比较大小
public boolen equals(person per)//比较当前对象与per引用实例对应成员变量值是否相等
java—为person类增加以下方法
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
threenewbee 2023-05-21 23:37关注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; } }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用