-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
74 lines (60 loc) · 2.65 KB
/
Copy pathApp.java
File metadata and controls
74 lines (60 loc) · 2.65 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 class_design.records;
public class App {
public static void main(String[] args) {
setStudentRecord();
System.out.println();
setStudentClass();
System.out.println();
equalsInRecord();
}
//all this can be replaced by only ONE line:
public static record Student (String firstName, String lastName, int id){}
public static record Student2 (String firstName, String lastName, int old, int id){
// we can override auto-generated constructor
// this is called "canonical constructor"
// public Student2 (String firstName, String lastName, int id) {
// if (id < 10 || id > 1_000_000) throw new IllegalArgumentException();
// this.firstName = firstName;
// this.lastName = lastName;
// this.id = id;
// }
//simpler way => "compact constructor"
// notice the syntax: no ()
// instance fields don't need to be explicitly initialized
// compact constructor could contain any business logic, e.g.
public Student2 {
if (id < 10 || id > 1_000_000) throw new IllegalArgumentException();
}
public boolean isMature(){
return this.old > 18;
}
}
//the getter is not like getFirstName(), but firstName() !!
//toString() is nicely implemented
public static void setStudentRecord(){
System.out.println("Student Record");
var theStudent = new Student("Alan", "Ly", 1);
System.out.println(theStudent.firstName());
System.out.println(theStudent.lastName());
System.out.println(theStudent.id());
System.out.println("ToString: "+ theStudent.toString());
var anotherStudent = new Student("Alan", "Ly", 1);
System.out.println("== In records : " + (theStudent == anotherStudent));
System.out.println("Equal In records: " + (theStudent.equals(anotherStudent)));
}
public static void equalsInRecord(){
var theStudent = new Student("Alan", "Ly", 1);
var anotherStudent = new Student("Alan", "Ly", 1);
System.out.println("== In Records: "+ (theStudent == anotherStudent));
System.out.println("Equals In Records: "+ (theStudent.equals(anotherStudent)));
}
//set Student Class
public static void setStudentClass(){
System.out.println("Student Class");
var theStudent = new class_design.records.Student("Alan", "Ly", 1);
System.out.println(theStudent.getFirstName());
System.out.println(theStudent.getLastName());
System.out.println(theStudent.getId());
System.out.println("ToString: "+ theStudent.toString());
}
}