-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerson.java
More file actions
105 lines (88 loc) · 3.01 KB
/
Person.java
File metadata and controls
105 lines (88 loc) · 3.01 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class Person {
// property
private String name;
// private 접근제어
// private 라고 쓰면 class에서만 접근 가능하도록 하게됨
private int age;
private int cashAmount;
private BankAccount account;
// 생성자 생성
// 생성자의 파라미터로 속성을 받아 각 인스턴스에 지정
public Person(String name, int age, int cashAmount) {
this.name = name;
if (age < 0) {
this.age = 12;
} else {
this.age = age;
}
this.cashAmount = Math.max(cashAmount, 0);
}
// 생성자도 메소드 오버로딩이 가능하다
// 파라미터가 가장 많은 생성자를 적은 쪽에서 호출하는 것이 효율적이다.
public Person(String name, int age) {
// if (age < 0) {
// this.age = 12;
// } else {
// this.age = age;
// }
// this.name = name;
// this.cashAmount = 0;
this(name, age, 0);
}
public Person(String name) {
this(name, 12, 0); // 12살을 기본 나이로 설정, 초기 현금 보유액은 0원.
}
public void setAge(int newAge) {
if (newAge > 0) { // 새로운 값이 양수일때만 넣어준다.
age = newAge;
}
}
public int getAge() {
return age;
}
/*
public void setName(String newName) {
// 파라미터 이름은 반드시 다르게 해야 한다.
// 그렇게 하고 싶지않은 경우 this키워드를 사용하면된다.
name = newName;
}
*/
public void setName(String name) {
// 파라미터 이름은 반드시 다르게 해야 한다.
// 그렇게 하고 싶지않은 경우 this키워드를 사용하면된다.
// 아래와 같이.
// this.name은 인스턴스 변수 name을 가르키게 된다.
this.name = name;
}
public String getName() {
return name;
}
public void setCashAmount(int cashAmount) {
if (cashAmount >= 0) {
this.cashAmount = cashAmount;
}
}
public int getCashAmount() {
return cashAmount;
}
public BankAccount getAccount() {
return account;
}
public void setAccount(BankAccount account) {
this.account = account;
}
// account와 account 간의 거래이므로, 첫째로 BankAccount 클래스의 transfer 메소드를 써야함.
// 둘째로 Person to의 정보를 to의 account 정보로 바꾸어 호출.
public boolean transfer(Person to, int amount) {
return account.transfer(to.getAccount(), amount);
}
// public boolean transfer(Person to, int amount) {
// return account.transfer(to, amount);
// account와 account 간의 거래이므로, BankAccount 클래스의 transfer 메소드를 써야함.
public boolean transfer(BankAccount to, int amount) {
return account.transfer(to, amount);
}
public void sayHello() {
System.out.println("Hi. My name is "+ this.name);
}
}