JAVA์˜ ์ •์„ (ch 7) (3)

Du-Hyeon, Kimยท2023๋…„ 8์›” 12์ผ
0

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
10/11
post-thumbnail

๐Ÿ’ป OOPs - 3

1. isntance of ์—ฐ์‚ฐ์ž


class test{
	public static void main(String args[]) {
		Car c = new Car();
		FireEngine fe = new FireEngine();
		Ambulance ab = new Ambulance();
		Bigban bb = new Bigban();
		
		
		
		System.out.println("car instanceof FireEngine : "+ (c instanceof FireEngine));
		System.out.println("fe instanceof Car : " + (fe instanceof Car));
//		System.out.println("ab instanceof FireEngine : "+ (ab instanceof FireEngine));
		System.out.println("car instanceof ab : "+ (c instanceof Ambulance));
		System.out.println("bb instanceof Car : " + (bb instanceof Car));
		
		Car c2 = fe;
		
		System.out.println("c2 instanceof FireEngine : " + (c2 instanceof FireEngine));
		System.out.println("c2 instanceof Ambulance : " + (c2 instanceof Ambulance));
		
	}
}

class Car {}

class FireEngine extends Car {}

class Ambulance extends Car{}

class Bigban extends Ambulance{}

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

์„ค๋ช…

๊ธฐ๋ณธ์ ์œผ๋กœ ์†Œ๋ฐฉ์ฐจ, ๊ตฌ๊ธ‰์ฐจ๋Š” ์ฐจ์˜ ์†์„ฑ์„ ๊ฐ€์ง„ ์ž์†๋“ค์ด๋‹ค.
์ด๋•Œ ์ด ์†Œ๋ฐฉ์ฐจ์—์„œ ์ฐจ์˜ ์†์„ฑ๋งŒ ๊ฐ€์ง€๋Š” c2 ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ๋งŒ์•ฝ ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค๋ฉด,
์ด๋•Œ, ์ฐจ๋Š” ์†Œ๋ฐฉ์ฐจ์ธ์ง€ ๊ตฌ๊ธ‰์ฐจ์ธ์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ instanceof๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ,
์ด๋–„ ํ™•์ธ๋œ instance๋ฅผ ํ†ตํ•ด์„œ ๊ทธ ์ธ์Šคํ„ด์Šค์˜ method์—๋„ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค.

์š”์•ฝ

ํ™•์žฅ๋œ ์ž์† ์ธ์Šคํ„ด์Šค๋ฅผ ์กฐ์ƒ ์ธ์Šคํ„ด์Šค๋กœ ๋„˜๊ฒจ ๋ฐ›์•˜์„ ๋•Œ, ๋‹ค์‹œ ์–ด๋–ค ์ž์† ์ธ์Šคํ„ด์Šค์—์„œ ์˜จ ๊ฒƒ์ด๋‹ˆ์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ


2. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ

์˜ˆ์‹œ๋กœ ์ดํ•ดํ•˜๋Š” ํŽธ์ด ๋น ๋ฅด๋‹ค.

class Product {
	int price;
	int bonusPoint;
}
class Tv extends Product {}
class Computer {}

class buyer {
	void buy(Tv) {/*same code*/}
	void buy(Computer) {/*same code*/}
}

๋Œ€์‹ ์— Tv์™€ Computer์€ ๋ชจ๋‘ Product์— ์ƒ์†๋˜์—ˆ๊ธฐ ๋–„๋ฌธ์—
์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„ํŽธํ•˜๊ฒŒ ํ‘œ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

class Product {
	int price;
	int bonusPoint;
}
class Tv extends Product {}
class Computer {}

class buyer {
	void buy(Product p){
		money -= p.price;
		bonusPoint += p.bonusPoint;
	}
}

๋งค๊ฒจ๋ณ€์ˆ˜์— ์„ ์–ธ๋œ ํƒ€์ž…์˜ ์ž์†๋“ค์€ ๋ชจ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด
๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์˜ˆ์ œ

class Product {
	int price;
	int bonusPoint;

	Product(int price) {
		this.price = price;
		bonusPoint = (int)(price/10.0);
	}
}

class Tv1 extends Product {
	Tv1() {	super(100);}
	//์กฐ์ƒ ์ƒ์„ฑ์ž์— ์ ‘๊ทผํ•˜์—ฌ price ์„ค์ •
	public String toString() { return "Tv"; }
}

class Computer extends Product {
	Computer() { super(200); }
	public String toString() { return "Computer"; }
}

class Buyer {
	int money = 1000;
	int bonusPoint = 0;

	void buy(Product p) {
		if(money < p.price) {
			System.out.println("no money");
			return;
		}

		money -= p.price;
		bonusPoint += p.bonusPoint;
		System.out.println(p + " is buyed");
	}
}

class Ex7_8 {
	public static void main(String args[]) {
		Buyer b = new Buyer();

		b.buy(new Tv1());
		b.buy(new Computer());

		System.out.println("Current money : " + b.money);
		System.out.println("Current bonusPoint" + b.bonusPoint);
	}
}

3. ์—ฌ๋Ÿฌ ์ข…๋ฅ˜ ๊ฐ์ฒด ๋ฐฐ์—ด

๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ๊ณผ ๋น„์Šทํ•˜๊ฒŒ,
๋™์ผํ•œ ์กฐ์ƒ์„ ๊ฐ€์ง„ ์ž์†๋“ค์„ ์กฐ์ƒ ํด๋ž˜์Šค ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

class Product {}
class Tv extends Product{}
class Computer extends Product{}
class Audio extends Product{}

Product p[] = new Product[3];
p[0] = new Tv();
p[1] = new Computer();
p[2] = new Audio();

์˜ˆ์‹œ๋กœ,
Product[] cart = new Product[10];์™€ ๊ฐ™์ด product cart๋ฅผ ์„ค์ •ํ•˜์—ฌ
๊ตฌ๋งคํ•œ ๋ฌผ๊ฑด๋“ค์„ listํ•˜๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

TMI
class array์˜ ๊ฒฝ์šฐ, ๋ฐฐ์—ด ๊ฐœ์ˆ˜๋ฅผ ์œ ๋™์ ์œผ๋กœ ์กฐ์ ˆํ•ด ์ฃผ๋Š” vector class๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
(๋’ค์— ๋‚˜์˜ฌ ๋“ฏ...)


4. ์ถ”์ƒ ํด๋ž˜์Šค(abstract class)

๋ฏธ์™„์„ฑ๋œ ํด๋ž˜์Šค : ๋ฏธ์™„์„ฑ ๋งค์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

  • ์ถ”์ƒํด๋ž˜์Šค๋กœ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.
  • ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ƒ์†์„ ํ†ตํ•ด์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.
  • ์ƒ์†์„ ํ†ตํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ์˜ˆ๊ธฐ๋Š”, TV๋„ 90%์ •๋„๋Š” ๋ชจ๋‘ ๋™์ผํ•œ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, TV์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์„ค๊ณ„๋Š” ๋˜์–ด์žˆ๋Š” abstract๋ฅผ ์ƒ์†๋ฐ›์•„ ์ตœ์ข… TV๋ฅผ ์™„์„ฑํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
abstract class ํด๋ž˜์Šค์ด๋ฆ„{
	//methods
	//instance variables
}

5. ์ถ”์ƒ ๋ฉ”์„œ๋“œ(abstract method)

  • ๋ฉ”์„œ๋“œ = ์„ ์–ธ๋ถ€ + ๊ตฌํ˜„๋ถ€
  • ์ถ”์ƒ๋ฉ”์„œ๋“œ = ์„ ์–ธ๋ถ€ + NULL

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

ํ˜•์‹

abstract returnType methodName();

์ถ”์ƒํด๋ž˜์Šค๋ฅด ์ƒ์† ๋ฐ›์€ ์ž์† ํด๋ž˜์Šค๋Š”, OVERRIDING์„ ํ†ตํ•ด์„œ abstract method๋ฅผ ๊ตฌํ˜„ํ•จ
(ํ•˜๋‚˜๋ผ๋„ ์™„์„ฑ๋˜์ง€ ์•Š์œผ๋ฉด ์ž์†ํด๋ž˜์Šค ์—ญ์‹œ ์ถ”์ƒํด๋ž˜์Šค๋กœ ์ง€์ •ํ•ด์•ผํ•จ)

abstract class Player{
	abstract void play(int pos);
	abstract void stop();
}

//์ถ”์ƒ ๋งค์„œ๋“œ ๋ชจ๋‘ overriding
class AudioPlayer extends Player{
	void play(int pos)	{ /* method */}
	void stop() 		{ /* method */}
}

//์ถ”์ƒ ๋ฉ”์„œ๋“œ ์ผ๋ถ€๋งŒ overriding
abstract class AbstracPlter extends Player{
	void play(int pos) 	{ /* method */}
}

6.์ถ”์ƒํ™”

  • ์ถ”์ƒํ™”
    ๊ธฐ์กด ํด๋ž˜์Šค์˜ ๊ณตํ†ต๋ถ€๋ถ„์„ ๋ฝ‘์•„๋‚ด์„œ ์กฐ์ƒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •
  • ์ƒ์†๊ณ„์ธต๋„๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐˆ์ˆ˜๋ก ํด๋ž˜์Šค๋Š” ์ ์  ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์–ด ๊ตฌ์ฒดํ™”์˜ ์ •๋„๊ฐ€ ์‹ฌํ•ด์ง
  • ์ƒ์†๊ณ„์ธต๋„๋ฅผ ๋”ฐ๋ผ ์˜ฌ๋ผ๊ฐˆ์ˆ˜๋ก ํด๋ž˜์Šค๋Š” ์ถ”์ƒํ™”์˜ ์ •๋„๊ฐ€ ์‹ฌํ•ด์ง
  • ์ƒ์†๊ณ„์ธต์„ ์˜ฌ๋ผ๊ฐˆ์ˆ˜๋ก ๊ณตํ†ต์š”์†Œ๋งŒ ๋‚จ๊ฒŒ๋จ

์˜ˆ์‹œ

abstract class Unit{
	int x, y;
	abstract void move(int x, int y);
	void stop() {};
}

class Marine extends Unit{
	void move(int x, int y) {}
	void stimPack() {}
}

class Tank extends Unit{
	void move(int x, int y) {}
	void changeMode() {}
}

class Dropship extends Unit{
	void move(int x, int y) {}
	void load(){}
	void unload(){}
}

์กฐ์ƒํด๋ž˜์Šค์˜ ์ถ”์ƒ๋ฉ”์„œ๋“œ move๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜๋ฏธ๋กœ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๋‹ค.

move๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”์ƒ๋ฉ”์„œ๋“œ๋กœ ์„ ์–ธ๋จ์œผ๋กœ์„œ ์•ž์œผ๋กœ Unit class์—์„œ ์ƒ์†๋ฐ›์•„์„œ ์ž‘์„ฑ๋˜๋Š” ํด๋ž˜์Šค๋Š” move๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ์˜ ํด๋ž˜์Šค์— ์•Œ๋งž๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค.

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 8์›” 12์ผ

์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ