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

[Java] λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©

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

β–Ά λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©

μƒμœ„ ν΄λž˜μŠ€μ— μ •μ˜ν•œ λ©”μ„œλ“œκ°€ ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ κ΅¬ν˜„ν•  λ‚΄μš©κ³Ό λ§žμ§€ μ•Šμ„ κ²½μš°μ— ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ 이 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•  수 μžˆλ‹€. 이λ₯Ό λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”©μ΄λΌ ν•œλ‹€.

    @Override
    public int calcPrice(int price) {  // μž¬μ •μ˜ν•œ λ©”μ„œλ“œ
    bonusPoint += price * bonusRatio;  // λ³΄λ„ˆμŠ€ 포인트 적립
    return price - (int)(price * saleRatio);  // ν• μΈλœ 가격을 κ³„μ‚°ν•˜μ—¬ λ°˜ν™˜
	}

 

package inheritance;

public class OverridingTest1 {
	public static void main(String[] args) {
		Customer customerLee = new Customer(10010, "μ΄μˆœμ‹ ");
		customerLee.bonusPoint = 1000;

		VIPCustomer customerKim = new VIPCustomer(10020, "κΉ€μœ μ‹ ", 12345);
		customerKim.bonusPoint = 10000;

		int price = 10000;
		System.out.println(customerLee.getCustomerName() + " λ‹˜μ΄ μ§€λΆˆν•΄μ•Ό ν•˜λŠ” κΈˆμ•‘μ€ " + customerLee.calcPrice(price) + "μ›μž…λ‹ˆλ‹€.");
		System.out.println(customerKim.getCustomerName() + " λ‹˜μ΄ μ§€λΆˆν•΄μ•Ό ν•˜λŠ” κΈˆμ•‘μ€ " + customerKim.calcPrice(price) + "μ›μž…λ‹ˆλ‹€.");
	}
}

 

λ°˜μ‘ν˜•

λŒ“κΈ€