-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
57 lines (47 loc) · 1.41 KB
/
Copy pathStudent.java
File metadata and controls
57 lines (47 loc) · 1.41 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
package class_design.records;
import java.util.Objects;
//the old way of creating encapsulated class
public class Student {
// 1. declare private final fields
private final String firstName;
private final String lastName;
private final int id;
// 2. define the constructor
public Student(String firstName, String lastName, int id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}
// 3. define getters
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
// 4. override toString() method
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", id=" + id + '}';
}
// 5. override equals() method
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id && Objects.equals(firstName, student.firstName) &&
Objects.equals(lastName, student.lastName);
}
// 6. override hashCode() method
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, id);
}
}