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

[Java] 가상 λ©”μ„œλ“œ

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

β–Ά 가상 λ©”μ„œλ“œ

μƒμ†μ—μ„œ μƒμœ„ ν΄λž˜μŠ€μ™€ ν•˜μœ„ ν΄λž˜μŠ€μ— 같은 μ΄λ¦„μ˜ λ©”μ„œλ“œκ°€ μ‘΄μž¬ν•  λ•Œ ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œλŠ” μΈμŠ€ν„΄μŠ€μ— 따라 κ²°μ •λœλ‹€. λ‹€μ‹œ 말해 μ„ μ–Έν•œ ν΄λž˜μŠ€ν˜•μ΄ μ•„λ‹Œ μƒμ„±λœ μΈμŠ€ν„΄μŠ€μ˜ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λŠ” 것이닀.

μ΄λ ‡κ²Œ μΈμŠ€ν„΄μŠ€μ˜ λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜λŠ” κΈ°μˆ μ„ '가상 λ©”μ„œλ“œ' 라고 ν•œλ‹€.

λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°

 

 

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

​

​

​

β–Ά 가상 λ©”μ„œλ“œμ˜ 원리

package inheritance;

public class OverridingTest3 {
	public static void main(String[] args) {
		int price = 10000;

		Customer customerLee = new Customer(10010, "μ΄μˆœμ‹ ");  // Customer μΈμŠ€ν„΄μŠ€ 생성
		System.out.println(customerLee.getCustomerName() + " λ‹˜μ΄ μ§€λΆˆν•΄μ•Ό ν•˜λŠ” κΈˆμ•‘μ€ " + customerLee.calcPrice(price) + "μ›μž…λ‹ˆλ‹€.");

		VIPCustomer customerKim = new VIPCustomer(10020, "κΉ€μœ μ‹ ", 12345);  // VIPCustomer μΈμŠ€ν„΄μŠ€ 생성
		System.out.println(customerKim.getCustomerName() + " λ‹˜μ΄ μ§€λΆˆν•΄μ•Ό ν•˜λŠ” κΈˆμ•‘μ€ " + customerKim.calcPrice(price) + "μ›μž…λ‹ˆλ‹€.");

		Customer vc = new VIPCustomer(10030, "λ‚˜λͺ°λΌ", 2000);  // VIPCustomer μΈμŠ€ν„΄μŠ€λ₯Ό Customer νƒ€μž…μ— λŒ€μž…
		System.out.println(vc.getCustomerName() + " λ‹˜μ΄ μ§€λΆˆν•΄μ•Ό ν•˜λŠ” κΈˆμ•‘μ€ " + vc.calcPrice(10000) + "μ›μž…λ‹ˆλ‹€.");

	}
}
λ°˜μ‘ν˜•

λŒ“κΈ€