在1.为Person类增加以下方法: (1)int getAge(int year) //返回在year年的年龄 (2)int getAge() //返回今年的年龄,重载 (3)int olderThen(Person per) //返回this与per //的年龄差值,按年龄比较对象大小 (4)boolean equals(Person per) //比较this与 // per引用实例对应成员变量值是否相等 2. 实现银行账户类构造方法成成员方法。 public class Account //账户类 { private String name; //储户姓名 private double balance; //账户余额 public Account(String name) //构造方法 public String getName() //返回账户名 public double balance() //查看账户余额 public boolean put(double value)//存款,存入金额 public double get(double value) //取款,参数为取款金额,返回实际取到金额 }的基础上,声明实现Comparble<T>接口,提供按照账户余额比较账户大小的方法;同时,使用Comparator<T>接口声明比较器类CompareName,按照账户名比较账户大小;编写main方法测试相应的接口实现方法。
时间: 2024-02-14 08:12:30 浏览: 69
好的,我理解了您的问题。下面是代码实现。
Person类:
```java
public class Person implements Comparable<Person> {
private String name;
private int birthYear;
public Person(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
public int getAge(int year) {
return year - birthYear;
}
public int getAge() {
return getAge(Calendar.getInstance().get(Calendar.YEAR));
}
public int olderThan(Person per) {
return this.birthYear - per.birthYear;
}
public boolean equals(Person per) {
return this.name.equals(per.name) && this.birthYear == per.birthYear;
}
@Override
public int compareTo(Person per) {
return this.birthYear - per.birthYear;
}
}
```
Account类:
```java
import java.util.Comparator;
public class Account implements Comparable<Account> {
private String name;
private double balance;
public Account(String name) {
this.name = name;
this.balance = 0;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public boolean put(double value) {
if (value > 0) {
balance += value;
return true;
}
return false;
}
public double get(double value) {
if (value > 0 && balance >= value) {
balance -= value;
return value;
}
return 0;
}
@Override
public int compareTo(Account acc) {
if (this.balance == acc.balance) {
return 0;
} else if (this.balance > acc.balance) {
return 1;
} else {
return -1;
}
}
public static class CompareName implements Comparator<Account> {
@Override
public int compare(Account acc1, Account acc2) {
return acc1.getName().compareTo(acc2.getName());
}
}
}
```
在main方法中测试:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// 测试Person类
Person p1 = new Person("Tom", 1990);
Person p2 = new Person("Jerry", 1995);
System.out.println(p1.getAge(2021)); // 输出: 31
System.out.println(p1.getAge()); // 输出: 当前年龄
System.out.println(p1.olderThan(p2)); // 输出: -5
System.out.println(p1.equals(p2)); // 输出: false
System.out.println(p1.equals(new Person("Tom", 1990))); // 输出: true
// 测试Account类
Account a1 = new Account("Tom");
Account a2 = new Account("Jerry");
a1.put(1000);
a2.put(2000);
System.out.println(a1.getBalance()); // 输出: 1000.0
System.out.println(a2.getBalance()); // 输出: 2000.0
System.out.println(a1.get(500)); // 输出: 500.0
System.out.println(a1.getBalance()); // 输出: 500.0
System.out.println(a1.compareTo(a2)); // 输出: -1
// 测试CompareName类
Account[] accounts = {new Account("Tom"), new Account("Jerry"), new Account("Bob")};
Arrays.sort(accounts, new Account.CompareName());
for (Account acc : accounts) {
System.out.println(acc.getName());
}
// 输出: Bob Jerry Tom
}
}
```
阅读全文