βΆ λ€νμ±
λ€νμ±μ΄λ νλμ μ½λκ° μ¬λ¬ μλ£νμΌλ‘ ꡬνλμ΄ μ€νλλ κ².
μ½κ² λ§ν΄ κ°μ μ½λμμ μ¬λ¬ μ€ν κ²°κ³Όκ° λμ¨λ€.
<μ€ν κ²°κ³Ό>
β
β
ββ
βΆ λ€νμ±μ μ₯μ
μμ ν΄λμ€μμ κ³΅ν΅ λΆλΆμ λ©μλλ₯Ό μ 곡νκ³ , νμ ν΄λμ€μμλ κ·Έμ κΈ°λ°ν μΆκ° μμλ₯Ό λ§λΆμ¬ ꡬννλ©΄ μ½λ μλ μ€μ΄λ€κ³ μ μ§ λ³΄μλ νΈλ¦¬νλ€.
λ, νμμ λ°λΌ μμλ°μ λͺ¨λ ν΄λμ€λ₯Ό νλμ μμ ν΄λμ€λ‘ μ²λ¦¬ν μ μκ³ λ€νμ±μ μν΄ κ° ν΄λμ€μ μ¬λ¬ κ°μ§ ꡬνμ μ€νν μ μμΌλ―λ‘ νλ‘κ·Έλ¨μ μ½κ² νμ₯ν μ μλ€.
package polymorphism;
public class Customer {
protected int customerID;
protected String customerName;
protected String customerGrade;
int bonusPoint;
double bonusRatio;
public Customer() {
initCustomer(); // κ³ κ° λ±κΈκ³Ό 보λμ€ ν¬μΈνΈ μ 립λ₯ μ§μ ν¨μ νΈμΆ
}
public Customer(int customerID, String customerName) {
this.customerID = customerID;
this.customerName = customerName;
initCustomer(); // κ³ κ° λ±κΈκ³Ό 보λμ€ ν¬μΈνΈ μ 립λ₯ μ§μ ν¨μ νΈμΆ
}
private void initCustomer() { // λ©€λ² λ³μμ μ΄κΈ°ν λΆλΆ
customerGrade = "SILVER";
bonusRatio = 0.01;
}
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
public String showCustomerInfo() {
return customerName + " λμ λ±κΈμ " + customerGrade + "μ΄λ©°, 보λμ€ ν¬μΈνΈλ " + bonusPoint + "μ μ
λλ€.";
}
}
package polymorphism;
public class VIPCustomer extends Customer {
private int agentID;
double saleRatio;
public VIPCustomer(int customerID, String customerName, int agentID) {
super(customerID, customerName);
customerGrade = "VIP";
bonusRatio = 0.05;
saleRatio = 0.1;
this.agentID = agentID;
}
public int calcPrice(int price) { // μ§λΆ κ°κ²© λ©μλ μ¬μ μ
bonusPoint += price * bonusRatio;
return price - (int) (price * saleRatio);
}
public String showCustomerInfo() { // κ³ κ° μ 보 μΆλ ₯ λ©μλ μ¬μ μ
return super.showCustomerInfo() + " λ΄λΉ μλ΄μ λ²νΈλ " + agentID + "μ
λλ€";
}
public int getAgentID() {
return agentID;
}
}
package polymorphism;
public class Customer {
protected int customerID;
protected String customerName;
protected String customerGrade;
int bonusPoint;
double bonusRatio;
public Customer() {
initCustomer();
}
public Customer(int customerID, String customerName) {
this.customerID = customerID;
this.customerName = customerName;
initCustomer();
}
private void initCustomer() {
customerGrade = "SILVER";
bonusRatio = 0.01;
}
public int calcPrice(int price) {
bonusPoint += price * bonusRatio;
return price;
}
public String showCustomerInfo() {
return customerName + " λμ λ±κΈμ " + customerGrade + "μ΄λ©°, 보λμ€ ν¬μΈνΈλ " + bonusPoint + "μ μ
λλ€.";
}
public int getCustomerID() { // get(), set() λ©μλ μΆκ°
return customerID;
}
public void setCustomerID(int customerID) { // get(), set() λ©μλ μΆκ°
this.customerID = customerID;
}
public String getCustomerName() { // get(), set() λ©μλ μΆκ°
return customerName;
}
public void setCustomerName(String customerName) { // get(), set() λ©μλ μΆκ°
this.customerName = customerName;
}
}
package polymorphism;
public class CustomerTest {
public static void main(String[] args) {
Customer customerLee = new Customer();
customerLee.setCustomerID(10010);
customerLee.setCustomerName("μ΄μμ ");
customerLee.bonusPoint = 1000;
System.out.println(customerLee.showCustomerInfo());
Customer customerKim = new VIPCustomer(10020, "κΉμ μ ", 12345); // VIPCustomerλ₯Ό CustomerνμΌλ‘ μ μΈ
customerKim.bonusPoint = 1000;
System.out.println(customerKim.showCustomerInfo());
System.out.println("====== ν μΈμ¨κ³Ό 보λμ€ ν¬μΈνΈ κ³μ° =======");
int price = 10000;
int leePrice = customerLee.calcPrice(price);
int kimPrice = customerKim.calcPrice(price);
System.out.println(customerLee.getCustomerName() + " λμ΄ " + leePrice + "μ μ§λΆνμ
¨μ΅λλ€.");
System.out.println(customerLee.showCustomerInfo());
System.out.println(customerKim.getCustomerName() + " λμ΄ " + kimPrice + "μ μ§λΆνμ
¨μ΅λλ€.");
System.out.println(customerKim.showCustomerInfo());
}
}
-λ§μ§λ§ 3λ²μ§Έ ν΄λμ€μμ get, set λ©μλλ₯Ό μμ Customer ν΄λμ€μ μΆκ°ν΄μ€μΌ λ©μλκ° μ€νλ¨.
β
β
<μ€ν κ²°κ³Ό>
'Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] λ€μ΄ μΊμ€ν κ³Ό instanceof (0) | 2021.02.20 |
---|---|
[Java] λ€νμ± νμ©νκΈ° (0) | 2021.02.20 |
[Java] κ°μ λ©μλ (0) | 2021.02.19 |
[Java] 묡μμ ν΄λμ€ ν λ³νκ³Ό λ©μλ μ¬μ μ (0) | 2021.02.19 |
[Java] λ©μλ μ€λ²λΌμ΄λ© (0) | 2021.02.19 |
λκΈ