-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-overrideRecord.jsh
54 lines (36 loc) · 1.38 KB
/
demo-overrideRecord.jsh
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
System.out.println("MVP Java - JDK 14 Records Demo - Override Record")
//customize/Override record
record Location (float latitude, float longitude){
static float DEFAULT_LATITUDE = 0F;
static float DEFAULT_LONGITUDE = 0F;
public Location // canonical constructor (don't put empty parenthesis!
{
if(latitude < -90 || latitude > 90){
throw new IllegalArgumentException("Invalid latititude " + latitude);
}
// add longitude validation too ...
// what is missing here? ... assignment done for us anyways!
}
public String toString()
{
return "lat = " + latitude + " long = " + longitude;
}
//can override hashCode .. etc ..
public float getLatitude (){
return latitude;
}
public float getLongitude (){
return longitude;
}
public static Location origin()
{
return new Location(DEFAULT_LATITUDE, DEFAULT_LONGITUDE);
}
}
Location santaMaria = new Location (36.947613F, -25.146546F);
System.out.println ("santaMaria.toString() = " + santaMaria);
System.out.println ("santaMaria latititude = " + santaMaria.getLatitude());
System.out.println ("santaMaria longitude = " + santaMaria.getLongitude());
System.out.println ();
Location originLocation = Location.origin();
System.out.println ("originLocation.toString() = " + originLocation);