-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path40_CopyObjects
More file actions
75 lines (65 loc) · 1.59 KB
/
40_CopyObjects
File metadata and controls
75 lines (65 loc) · 1.59 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
// if use car1= car2 will get same address, this method works for different address
<Main.java>
public class Main{
public static void main(String[] args){
Car car1 = new Car("Chevrolet","Camera",2021);
//Car car2 = new Car("Ford","Mustang",2022);
//car2.copy(car1);
Car car2 = new Car(car1);
System.out.println(car1);
System.out.println(car2);
System.out.println();
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getYear());
System.out.println();
System.out.println(car2.getMake());
System.out.println(car2.getModel());
System.out.println(car2.getYear());
}
}
public class Car{
private String make;
private String model;
private int year;
Car(String make, String model, int year){
this.setMake(make);
this.setModel(model);
this.setYear(year);
}
Car(Car x){
this.copy(x);
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public int getYear(){
return year;
}
public void setMake(String make){
this.make=make;
}
public void setModel(String model){
this.model = model;
}
public void setYear(int year){
this.year=year;
}
public void copy(Car x){
this.setMake(x.getMake());//this refer car 2 (object who called method)
this.setModel(x.getModel());
this.setYear(x.getYear());
}
}
>>
Car@27d6c5e0
Car@4f3f5b24
Chevrolet
Camera
2021
Chevrolet
Camera
2021