๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ์ƒ์†

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 19.
๋ฐ˜์‘ํ˜•

โ–ถ ์ƒ์†์ด๋ž€ ?

์ƒ์†์€ ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ ๋“ฏ ๋ฌด์—‡์ธ๊ฐ€๋ฅผ ๋ฌผ๋ ค๋ฐ›๋Š”๋‹ค๋Š” ์˜๋ฏธ.

Bํด๋ž˜์Šค๊ฐ€ Aํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด Bํด๋ž˜์Šค๋Š” Aํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋žจ์€ ์œ ์ง€ ๋ณด์ˆ˜ํ•˜๊ธฐ ํŽธํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์œ ์—ฐํ•œ๋ฐ, ๊ทธ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๊ธฐ์ˆ ์ด ๋ฐ”๋กœ ์ƒ์†์ด๋‹ค.

์ž๋ฐ” ๋ฌธ๋ฒ•์œผ๋กœ ์ƒ์†์„ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” โ€‹extends ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โ€‹

โ€‹

โ€‹

โ–ถ ์ƒ์†์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ๊ฐ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„ํ•˜๊ธฐ

package inheritance;

public class Customer {
    
    // ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
	private int customerID;  // ๊ณ ๊ฐ ์•„์ด๋””
	private String customerName;  // ๊ณ ๊ฐ ์ด๋ฆ„
	private String customerGrade;  // ๊ณ ๊ฐ ๋“ฑ๊ธ‰
	int bonusPoint;  // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ
	double bonusRatio;  // ์ ๋ฆฝ ๋น„์œจ

	public Customer() {  // ๋””ํดํŠธ ์ƒ์„ฑ์ž
		customerGrade = "SILVER";  // ๊ธฐ๋ณธ ๋“ฑ๊ธ‰
		bonusRatio = 0.01;  // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ ๊ธฐ๋ณธ ์ ๋ฆฝ ๋น„์œจ
	}

	public int calcPrice(int price) {  // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ ์ ๋ฆฝ, ์ง€๋ถˆ ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ ๋ฉ”์„œ๋“œ
		bonusPoint += price * bonusRatio;  // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ ๊ณ„์‚ฐ
		return price;
	}

	public String showCustomerInfo() {  // ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
		return customerName + " ๋‹˜์˜ ๋“ฑ๊ธ‰์€ " + customerGrade + "์ด๋ฉฐ, ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ๋Š” " + bonusPoint + "์ž…๋‹ˆ๋‹ค.";
	}
}

 

package inheritance;

public class VIPCustomer extends Customer {

	private int customerID;  // ๊ณ ๊ฐ ์•„์ด๋””
	private String customerName;  // ๊ณ ๊ฐ ์ด๋ฆ„
	private String customerGrade;  // ๊ณ ๊ฐ ๋“ฑ๊ธ‰
	int bonusPoint;  // ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ
	double bonusRatio;  // ์ ๋ฆฝ

    // VIP ๊ณ ๊ฐ ๊ด€๋ จ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ๋•Œ๋งŒ ํ•„์š”ํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
    private int agentID;  
    double saleRatio;

	public VIPCustomer() {  // ๋””ํดํŠธ ์ƒ์„ฑ์ž
		customerGrade = "VIP";
		bonusRatio = 0.05;
		saleRatio = 0.1;
	}

	public int calcPrice(int price) {
		bonusPoint += price * bonusRatio;
		return price - (int) (price * saleRatio);  // ํ• ์ธ์œจ ์ ์šฉ
	}

	public int getAgentID() {  // VIP ๊ณ ๊ฐ์—๊ฒŒ๋งŒ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ
		return agentID;
	}

    public String showCustomerInfo() {
        return customerName + " ๋‹˜์˜ ๋“ฑ๊ธ‰์€" + customerGrade + "์ด๋ฉฐ, ๋ณด๋„ˆ์Šค ํฌ์ธํŠธ๋Š”" + bonusPoint + "์ž…๋‹ˆ๋‹ค.";
    }
}

 

VIPCustomer ํด๋ž˜์Šค์— extends ์˜ˆ์•ฝ์–ด ์‚ฌ์šฉ

-์ฝ”๋“œ๋ฅผ ์ ๋‹ค ๋ณด๋ฉด customerGrade ๋ณ€์ˆ˜์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•จ. private ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ–ˆ๊ธฐ๋•Œ๋ฌธ์ด๋‹ค.

โ€‹

protected ๋ณ€์ˆ˜ ์„ ์–ธ + get( ), set( ) ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€

 

package inheritance;

public class CustomerTest1 {
	public static void main(String[] args) {
		Customer customerLee = new Customer();
		customerLee.setCustomerID(10010);  // customerID๋Š” protected ๋ณ€์ˆ˜์ด๋ฏ€๋กœ set() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		customerLee.setCustomerName("์ด์ˆœ์‹ ");  // customerName๋Š” protected ๋ณ€์ˆ˜์ด๋ฏ€๋กœ set() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		customerLee.bonusPoint = 1000;
		System.out.println(customerLee.showCustomerInfo());

		VIPCustomer customerKim = new VIPCustomer();
		customerKim.setCustomerID(10020);  // customerID๋Š” protected ๋ณ€์ˆ˜์ด๋ฏ€๋กœ set() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		customerKim.setCustomerName("๊น€์œ ์‹ ");  // customerName๋Š” protected ๋ณ€์ˆ˜์ด๋ฏ€๋กœ set() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
		customerKim.bonusPoint = 10000;
		System.out.println(customerKim.showCustomerInfo());
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

โ–ถ ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ณผ์ •

์ƒ์†์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ณผ์ • (1)

 

์ƒ์†์—์„œ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ณผ์ • (2)

 

package inheritance;

public class CustomerTest2 {
	public static void main(String[] args) {
		VIPCustomer customerKim = new VIPCustomer();  // ํ•˜์œ„ ํด๋ž˜์Šค ์ƒ์„ฑ
		customerKim.setCustomerID(10020);
		customerKim.setCustomerName("๊น€์œ ์‹ ");
		customerKim.bonusPoint = 10000;
		System.out.println(customerKim.showCustomerInfo());
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

-์ƒ์œ„ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€