forked from liujiboy/Java_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
84 lines (73 loc) · 1.58 KB
/
Student.java
File metadata and controls
84 lines (73 loc) · 1.58 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
75
76
77
78
79
80
81
82
83
84
package edu.cqu;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 自定义的类应该实行equals、hashCode和toString函数。
* 利用Eclipse的功能可以很方便的生成equals和hashCode。
* @author liuji
*
*/
public class Student {
private String name;
private String id;
public Student(String name, String id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
StringBuffer buffer=new StringBuffer();
buffer.append("id=");
buffer.append(id);
buffer.append("\n");
buffer.append("name=");
buffer.append(name);
buffer.append("\n");
return buffer.toString();
}
public static void main(String args[])
{
Student s1=new Student("B","1");
Student s2=new Student("A","1");
List<Student> l=new ArrayList<Student>();
l.add(s1);
l.add(s2);
System.out.println(l);
Set<Student> s=new HashSet<Student>();
s.add(s1);
s.add(s2);
System.out.println(s);
}
}