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

[Java] ν…œν”Œλ¦Ώ λ©”μ„œλ“œ

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

β–Ά 좔상 ν΄λž˜μŠ€μ™€ ν…œν”Œλ¦Ώ λ©”μ„œλ“œ

ν…œν”Œλ¦Ώ λ©”μ„œλ“œ : 틀이 μžˆλŠ” λ©”μ„œλ“œ(싱글톀과 같은 λ””μžμΈ νŒ¨ν„΄)

ν”„λ ˆμž„μ›Œν¬μ—μ„œ 많이 μ“°μž„

 

package template;

public abstract class Car {
	
	public abstract void drive();
	public abstract void stop();

	public void startCar() {
		System.out.println("μ‹œλ™μ„ μΌ­λ‹ˆλ‹€");
	}
	
	public  void turnOff() {
		System.out.println("μ‹œλ™μ„ λ•λ‹ˆλ‹€.");
	}
	
	final public void run() {
		startCar();
		drive();
		stop();
		turnOff();
	}
    
}

 

package template;

public class AICar extends Car {

	@Override
	public void drive() {
		System.out.println("자율 μ£Όν–‰ν•©λ‹ˆλ‹€");
		System.out.println("μžλ™μ°¨κ°€ 슀슀둜 λ°©ν–₯을 μ „ν™˜ν•©λ‹ˆλ‹€.");
	}

	@Override
	public void stop() {
		System.out.println("슀슀둜 멈μΆ₯λ‹ˆλ‹€.");
	}
    
}

 

package template;

public class ManualCar extends Car {

	@Override
	public void drive() {
		System.out.println("μ‚¬λžŒμ΄ μš΄μ „ν•©λ‹ˆλ‹€");
		System.out.println("μ‚¬λžŒμ΄ 핸듀을 μ‘°μž‘ν•©λ‹ˆλ‹€");
	}

	@Override
	public void stop() {
		System.out.println("브레이크둜 μ •μ§€ν•©λ‹ˆλ‹€");

	}
    
}

 

package template;

public class CarTest {
	public static void main(String[] args) {

		System.out.println("=== μžμœ¨μ£Όν–‰ ν•˜λŠ” μžλ™μ°¨ ===");
		Car myCar = new AICar();
		myCar.run();

		System.out.println("=== μ‚¬λžŒμ΄ μš΄μ „ν•˜λŠ” μžλ™μ°¨ ===");
		Car hisCar = new ManualCar();
		hisCar.run();
	}

}

 

 

 

β–Ά ν…œν”Œλ¦Ώ λ©”μ„œλ“œμ˜ μ—­ν• 

ν…œν”Œλ¦Ώ λ©”μ„œλ“œμ˜ 역할은 λ©”μ„œλ“œ μ‹€ν–‰ μˆœμ„œμ™€ μ‹œλ‚˜λ¦¬μ˜€λ₯Ό μ •μ˜ν•˜λŠ” 것.

​

​

​

λ‚˜ 혼자 μ½”λ”©) λͺ¨λ“  차에 와이퍼 κΈ°λŠ₯을 μΆ”κ°€ν•˜λ €κ³  ν•œλ‹€. 좔상 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 차쒅이 μ—¬λŸ¬ 개일 λ•Œ 각 ν΄λž˜μŠ€μ— 와이퍼 κΈ°λŠ₯을 κ΅¬ν˜„ν•œλ‹€. wiper( ) 좔상 λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•œ Car 클래슀λ₯Ό λ‹€μ΄μ–΄κ·Έλž¨μœΌλ‘œ κ·Έλ € 보면 λ‹€μŒκ³Ό κ°™λ‹€.

ManualCar의 wiper( ) λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ 'μ‚¬λžŒμ΄ λΉ λ₯΄κΈ°λ₯Ό μ‘°μ ˆν•©λ‹ˆλ‹€.'라고 좜λ ₯ν•˜κ³ , AICar의 wiper( ) λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜λ©΄ 'λΉ„λ‚˜ 눈의 양에 따라 λΉ λ₯΄κΈ°κ°€ μžλ™μœΌλ‘œ μ‘°μ ˆλ©λ‹ˆλ‹€.' 라고 좜λ ₯ν•˜λ„λ‘ μ•žμ˜ 예제λ₯Ό μˆ˜μ •ν•΄λ³΄λΌ.

 

<λ‚΄κ°€ ν•œ 것>

package template;

public abstract class Car {

	public abstract void drive();

	public abstract void stop();

	public void startCar() {
		System.out.println("μ‹œλ™μ„ μΌ­λ‹ˆλ‹€");
	}

	public void turnOff() {
		System.out.println("μ‹œλ™μ„ λ•λ‹ˆλ‹€.");
	}

    public abstract void wiper();

	final public void run() {
		startCar();
		drive();
        wiper();
		stop();
		turnOff();
	}
    
}

 

package template;

public class AICar extends Car {

	@Override
	public void drive() {
		System.out.println("자율 μ£Όν–‰ν•©λ‹ˆλ‹€");
		System.out.println("μžλ™μ°¨κ°€ 슀슀둜 λ°©ν–₯을 μ „ν™˜ν•©λ‹ˆλ‹€.");
	}

	@Override
	public void stop() {
		System.out.println("슀슀둜 멈μΆ₯λ‹ˆλ‹€.");
	}

    @Override
	public void wiper() {
		System.out.println("λΉ„λ‚˜ 눈의 양에 따라 λΉ λ₯΄κΈ°κ°€ μžλ™μœΌλ‘œ μ‘°μ ˆλ©λ‹ˆλ‹€.");
	}
    
}

 

package template;

public class ManualCar extends Car {

	@Override
	public void drive() {
		System.out.println("μ‚¬λžŒμ΄ μš΄μ „ν•©λ‹ˆλ‹€");
		System.out.println("μ‚¬λžŒμ΄ 핸듀을 μ‘°μž‘ν•©λ‹ˆλ‹€");
	}

	@Override
	public void stop() {
		System.out.println("브레이크둜 μ •μ§€ν•©λ‹ˆλ‹€");

     @Override
	public void wiper() {
		System.out.println("μ‚¬λžŒμ΄ λΉ λ₯΄κΈ°λ₯Ό μ‘°μ ˆν•©λ‹ˆλ‹€.");
	}
    
}

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

​

λ°˜μ‘ν˜•

'Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[Java] final μ˜ˆμ•½μ–΄  (0) 2021.02.20
[Java] ν…œν”Œλ¦Ώ λ©”μ„œλ“œ μ‘μš©ν•˜κΈ°  (0) 2021.02.20
[Java] 좔상 클래슀  (0) 2021.02.20
[Java] μ‹€μŠ΅  (0) 2021.02.20
[Java] λ‹€μš΄ μΊμŠ€νŒ…κ³Ό instanceof  (0) 2021.02.20

λŒ“κΈ€