λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Java

[Java] λ‹€ν˜•μ„±

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 19.
λ°˜μ‘ν˜•

β–Ά λ‹€ν˜•μ„±

λ‹€ν˜•μ„±μ΄λž€ ν•˜λ‚˜μ˜ μ½”λ“œκ°€ μ—¬λŸ¬ μžλ£Œν˜•μœΌλ‘œ κ΅¬ν˜„λ˜μ–΄ μ‹€ν–‰λ˜λŠ” 것.

μ‰½κ²Œ 말해 같은 μ½”λ“œμ—μ„œ μ—¬λŸ¬ μ‹€ν–‰ κ²°κ³Όκ°€ λ‚˜μ˜¨λ‹€.

λ‹€ν˜•μ„± ν…ŒμŠ€νŠΈν•˜κΈ°

 

 

<μ‹€ν–‰ κ²°κ³Ό>

​

​

​​

β–Ά λ‹€ν˜•μ„±μ˜ μž₯점

μƒμœ„ ν΄λž˜μŠ€μ—μ„œ 곡톡 λΆ€λΆ„μ˜ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜κ³ , ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œλŠ” 그에 κΈ°λ°˜ν•œ μΆ”κ°€ μš”μ†Œλ₯Ό 덧뢙여 κ΅¬ν˜„ν•˜λ©΄ μ½”λ“œ 양도 쀄어듀고 μœ μ§€ λ³΄μˆ˜λ„ νŽΈλ¦¬ν•˜λ‹€.

또, ν•„μš”μ— 따라 상속받은 λͺ¨λ“  클래슀λ₯Ό ν•˜λ‚˜μ˜ μƒμœ„ 클래슀둜 μ²˜λ¦¬ν•  수 있고 λ‹€ν˜•μ„±μ— μ˜ν•΄ 각 클래슀의 μ—¬λŸ¬ κ°€μ§€ κ΅¬ν˜„μ„ μ‹€ν–‰ν•  수 μžˆμœΌλ―€λ‘œ ν”„λ‘œκ·Έλž¨μ„ μ‰½κ²Œ ν™•μž₯ν•  수 μžˆλ‹€.

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 ν΄λž˜μŠ€μ— μΆ”κ°€ν•΄μ€˜μ•Ό λ©”μ„œλ“œκ°€ 싀행됨.

​

​

<μ‹€ν–‰ κ²°κ³Ό>

 

λ°˜μ‘ν˜•

λŒ“κΈ€