-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
74 lines (61 loc) · 1.61 KB
/
Copy pathUser.java
File metadata and controls
74 lines (61 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package collection.java2;
import java.util.Objects;
public class User implements Comparable{
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
System.out.println("User equals() ...");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age &&
Objects.equals(name, user.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
// 姓名从大到小, 年龄从小到大
@Override
public int compareTo(Object o) {
if(o instanceof User){
User user = (User) o;
// return this.name.compareTo(user.name);
int compare = -this.name.compareTo(user.getName());
if(compare != 0){
return compare;
}else{
return Integer.compare(this.age, user.getAge());
}
}
else{
throw new RuntimeException("输入类型不匹配");
}
}
}