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

[Java] ์ถ”์ƒ ํด๋ž˜์Šค

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

โ–ถ ์ถ”์ƒ ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ

์ž‘์„ฑํ•ด๋ณด๋ฉด ์ €๋ ‡๊ฒŒ ์˜ค๋ฅ˜๊ฐ€ ๋œฌ๋‹ค. ์˜ค๋ฅ˜์˜ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€

์ด ๋‘˜ ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ์šฐ๋ฆฌ๋Š” abstract ์˜ˆ์•ฝ์–ด๋ฅผ ๋ฐฐ์šฐ๊ณ ์žˆ์œผ๋‹ˆ๊นŒ abstract๋ฅผ ์ž…๋ ฅํ•ด๋ณธ๋‹ค.

์ถ”์ƒ ๋ฉ”์„œ๋“œ๊ฐ€ ์†ํ•œ ํด๋ž˜์Šค๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์˜ค๋ฅ˜ ํ•ด๊ฒฐ !

โ€‹

 

โ€‹

โ–ถ ์ถ”์ƒ ํด๋ž˜์Šค ์ƒ์† ๋ฐ›๊ธฐ

์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

- ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•  ๊ฒƒ

- DeskTop๋„ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค ๊ฒƒ

โ€‹

์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ๊ตฌํ˜„๋˜์ง€ ์•Š์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์•ผ ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.

์˜ค๋ฅ˜ ๋ถ€๋ถ„์— ๋งˆ์šฐ์Šค๋ฅผ ๋Œ€๊ณ  Add unimplemented methods ์˜ต์…˜์„ ๋ˆŒ๋Ÿฌ์„œ ๋‚˜๋จธ์ง€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

โ€‹

์ถ”์ƒ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

-4~12ํ–‰์€ ์ƒ์œ„ ํด๋ž˜์Šค์ธ Computer ํด๋ž˜์Šค์— ํฌํ•จ๋œ ์ถ”์ƒ ๋ฉ”์„œ๋“œ์ธ display()์™€ typing()์„ ์žฌ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์ด ๋ชธ์ฒด ๋ถ€๋ถ„์— ์›ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

 

NoteBook ํด๋ž˜์Šค

์ด ํด๋ž˜์Šค์—์„œ๋Š” ์ƒ์†๋ฐ›์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  display() ํ•˜๋‚˜๋งŒ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ NoteBook ํด๋ž˜์Šค๋Š” ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ถ”์ƒ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.

 

MyNoteBook ํด๋ž˜์Šค ๊ตฌํ˜„ํ•˜๊ธฐ

-๋ชจ๋“  ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์—” abstract ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

 

 

โ–ถ ๋ชจ๋“  ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์— abstract ์˜ˆ์•ฝ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด?

์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ๋ชฉ์ ์ด ์•„๋‹Œ ์ƒ์†๋งŒ์„ ์œ„ํ•ด ๋งŒ๋“  ์ถ”์ƒ ํด๋ž˜์Šค๋ฉด new ์˜ˆ์•ฝ์–ด๋กœ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์•ˆ๋จ.

โ€‹

โ€‹

โ€‹

๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ) ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์ธ CarTest.java์˜ ์ถœ๋ ฅ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๋„๋ก Car, Bus, AutoCar ํด๋ž˜์Šค๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๋ผ.

<์กฐ๊ฑด>

Car ํด๋ž˜์Šค : run( ), refuel( ), stop( )

Bus ํด๋ž˜์Šค : takePassenger( )

AutoCar ํด๋ž˜์Šค : load( )

โ€‹

<๊ณผ์ •>

โ€‹

1. CarTest ํด๋ž˜์Šค์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”๊ฑธ ํ™•์ธ.

package chapter9.step1;

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

		Bus bus = new Bus();
		AutoCar autoCar = new AutoCar();
		
		bus.run();
		autoCar.run();
		
		bus.refuel();
		autoCar.refuel();
		
		bus.takePassenger();
		autoCar.load();
		
		bus.stop();
		autoCar.stop();			
	}

}

 

 

2. Bus์˜ ์˜ค๋ฅ˜ ๋ถ€๋ถ„์— ๋งˆ์šฐ์Šค๋ฅผ ๋Œ€๋ณด๋ฉด 'Create class Bus' ๋ผ๊ณ  ๋œฌ๋‹ค. ๋ˆŒ๋Ÿฌ๋ณด์ž

โ€‹

โ€‹

3. AutoCar๋„ Bus์™€ ๋˜‘๊ฐ™์ด ํ•œ๋‹ค.

โ€‹

โ€‹

4. Car ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ Bus, AutoCar ํด๋ž˜์Šค์— ๊ฐ๊ฐ ์ƒ์†ํ•ด์ค€๋‹ค

โ€‹

โ€‹

5. Car ํด๋ž˜์Šค๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๊ณ  ์œ„์— ์กฐ๊ฑด์ฒ˜๋Ÿผ run( ), refuel( )์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค์ž

package chapter9.step1;

public abstract class Car {

	public abstract void run();
	public abstract void refuel();
	public void stop() {
		System.out.println("์ฐจ๊ฐ€ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}
}

 

 

6. Bus ํด๋ž˜์Šค์— ์ถ”์ƒ ๋ฉ”์„œ๋“œ ๋‚ด์šฉ์„ ๋ฉ”๊พธ์ž. (run( ), refuel( )์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ)

package chapter9.step1;

public class Bus extends Car{

	public void takePassenger() {
		System.out.println("์Šน๊ฐ์„ ๋ฒ„์Šค์— ํƒœ์›๋‹ˆ๋‹ค.");
	}

	@Override
	public void refuel() {
		System.out.println("์ฒœ์—ฐ ๊ฐ€์Šค๋ฅผ ์ถฉ์ „ ํ•ฉ๋‹ˆ๋‹ค.");
	}
	
	public void run() {
		System.out.println("๋ฒ„์Šค๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
	}
}

 

 

7. AutoCar ํด๋ž˜์Šค๋„ ๋˜‘๊ฐ™์ด ๋‚ด์šฉ์„ ๋ฉ”๊พธ์ž. (run( ), refuel( )์€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ)

package chapter9.step1;

public class AutoCar extends Car{

    public void load() {
		System.out.println("์ง์„ ์‹ฃ์Šต๋‹ˆ๋‹ค.");
	}

	@Override
	public void refuel() {
		System.out.println("ํœ˜๋ฐœ์œ ๋ฅผ ์ถฉ์ „ํ•ฉ๋‹ˆ๋‹ค.");
	}

    @Override
	public void run() {
		System.out.println("์ฐจ๊ฐ€ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}
}

 

 

8. ์ด์ œ ์˜ค๋ฅ˜๊ฐ€ ์—†์–ด์ง. ์ถœ๋ ฅํ•ด๋ณด์ž

package chapter9.step1;

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

		Bus bus = new Bus();
		AutoCar autoCar = new AutoCar();
		
		bus.run();
		autoCar.run();
		
		bus.refuel();
		autoCar.refuel();
		
		bus.takePassenger();
		autoCar.load();
		
		bus.stop();
		autoCar.stop();
	}

}

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€