๐ 12์ฅ ์ถ์ํด๋์ค์ ์ธํฐํ์ด์ค
๊ธฐ์กด์ ์์ ๋ฌธ์
์์ธ ์ ์๊ฐ ๋ฏธ์ ์ธ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์์์ ์ฌ๋ฃ๋ก ์ฌ์ฉ๋ ํด๋์ค

๋์ฑ 1. attack() ๋ฉ์๋์ ๋ด๋ถ๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๋๊ธฐ


๐ก
๋ฏธ๋์ ํ์ ์์ ํด๋์ค๊ฐ ํด๋น ํด๋์ค๋ฅผ ์์๋ฐ์ ์ค๋ฒ๋ผ์ด๋ ํ ๊ฒ์ ์์
๋ฌธ์ ์ : ๋ฏธ๋์ ํด๋น ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ ์ ํ ๊ฐ๋ฅ์ฑ์ด ์๋ค.



์๋์น ์์ new ์ฌ์ฉ


์ถ์ ํด๋์ค
๐ก
๋ฏธ ๊ตฌํ๋ ํด๋์ค๋ฅผ ๋ง๋ค ๋ ์ถ์ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค
์ถ์ ๋ฉ์๋ (abstract method)๋ฅผ ๊ฐ์ง ์ถ์ ํด๋์ค

public abstract class Character {
private String name;
private int hp;
public Character(String name) {
this.name = name;
hp = 100;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public abstract void attack(Kinoko kinoko);
}
๐ก
๋ฏธ ๊ตฌํ๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง ์ถ์ ํด๋์ค. ์ถ์ ๋ฉ์๋๋ฅผ ๊ฐ์ง๋ ค๋ฉด ์ถ์ ํด๋์ค์ฌ์ผ๋ง ํ๋ค.
์ถ์ํด๋์ค์ ์ ์ฝ : ์ถ์ํด๋์ค๋ new์ ์ํ ์ธ์คํด์คํ๊ฐ ๊ธ์ง๋์ด ์๋ค.

์ถ์ํด๋์ค์ ์ ์ฝ : ์ค๋ฒ๋ผ์ด๋ ๊ฐ์

์ถ์ ์์ ๋ค๊ณ์ธต ๊ตฌ์กฐ

๐ก
Italic ์ฒด์ ํด๋์ค๋ ์ถ์ ํด๋์ค์ด๋ค.
Monster
, WalkingMonster
, FlyingMonster
๋ ์ถ์ ํด๋์ค์ด๋ค.
๐ก
์ถ์ํด๋์ค๋ฅผ ์์๋ฐ์ ์ถ์ํด๋์ค๋ ๋ถ๋ชจ ์ถ์ ํด๋์ค์ ์ถ์ ๋ฉ์๋๋ฅผ ๊ตฌํํ์ง ์์๋ ๋๊ณ ๋ ์ถ๊ฐํ ์ถ์ ํด๋์ค๋ฅผ ์ ์ํ ์๋ ์๋ค.
์ธํฐํ์ด์ค
๐ก
๋ชจ๋ ๋ฉ์๋๋ ์ถ์ ๋ฉ์๋์ฌ์ผ ํ๋ค. ํ๋(์์ฑ)๋ฅผ ๊ฐ์ง์ง ์๋๋ค.

๐ก
๋ชจ๋ ๋ฉ์๋๊ฐ ์ถ์ ๋ฉ์๋ ์ด๋ฏ๋ก, public abstract์ ํค์๋๋ฅผ ์๋ตํ๋ค.
์ธํฐํ์ด์ค์์ ์์ ์ ์ธ

๐ก
ํ๋ ์์ฑ ๊ฐ์ด ์๋, public static final์ด ์๋ต๋ ์์๋ฅผ ์ ์ธํ ์ ์๋ค.
๋ฐ๋ผ์, ํด๋น ์์๊ฐ์
์ธํฐํ์ด์ค.์์๋ช
์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
์ธํ์ ์ธํฐํ์ด์ค

์ธํฐํ์ด์ค ๊ตฌํ
๐ก
implements
ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ธํฐํ์ด์ค ๋ด ๋ฏธ๊ตฌํ๋ ์ถ์ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ฉด ๋๋ค.

๐ก
์ฌ๋ฌ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ ์๋ค.
์ธํฐํ์ด์ค์ ํจ๊ณผ
- ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค๋ค์ ๊ณตํต ๋ฉ์๋๋ฅผ ๊ตฌํํ๋๋ก ๊ฐ์ ๋๋ค.
- ์ด๋ค ํด๋์ค๊ฐ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ณ ์๋ค๋ฉด, ์ ์ด๋ ๊ทธ ์ธํฐํ์ด์ค์ ์ ์๋ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ด ๋ณด์ฆ๋๋ค.
- ๋ค์ค ์์์ ํจ๊ณผ๋ฅผ ๋ผ ์ ์๋ค.
์ธํฐํ์ด์ค์ ์์

๐ก
์ธํฐํ์ด์ค๊ฐ ๋ค๋ฅธ ์ถ์ ํด๋์ค๋ฅผ ์์๋ฐ์์ผ๋ก ์ด ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ํด๋์ค๋ ๋ค์ค ์์์ ๋ฐ๋ ํจ๊ณผ๋ฅผ ๊ฐ์ง ์ ์๋ค.
implemnets์ extends์ ์ฌ์ฉ๋ฐฉ๋ฒ ์ ๋ฆฌ

ํ์ฉ ์์) extends์ implements๋ฅผ ํต์์ ์ฌ์ฉ

๐์ฐ์ต ๋ฌธ์
12-1




package Exam;
public class Book extends TangibleAsset {
private String isbn;
public Book(String name, int price, String color, String isbn) {
super(name, price, color);
this.isbn = isbn;
}
public String getIsbn() {
return this.isbn;
}
}
package Exam;
public class Computer extends TangibleAsset {
private String makerName;
public Computer(String name, int price, String color, String makerName) {
super(name, price, color);
this.makerName = makerName;
}
public String getMakerName() {
return this.makerName;
}
}
package Exam;
public abstract class TangibleAsset {
private String name;
private int price;
private String color;
public TangibleAsset(String name, int price, String color){
this.name = name;
this.price = price;
this.color = color;
}
public String getName() {
return this.name;
}
public int getPrice() {
return this.price;
}
public String getColor() {
return this.color;
}
}
12-2

package Exam;
public abstract class Asset {
public abstract String getName();
public abstract int getPrice();
public abstract String getColor();
}
package Exam;
public abstract class TangibleAsset extends Asset{
private String name;
private int price;
private String color;
public TangibleAsset(String name, int price, String color){
this.name = name;
this.price = price;
this.color = color;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getPrice() {
return this.price;
}
@Override
public String getColor() {
return this.color;
}
}
12-3

package Exam;
public interface Thing {
double getWeight();
void setWeight(double weight);
}
12-4

package Exam;
public abstract class TangibleAsset extends Asset implements Thing{
private String name;
private int price;
private String color;
private double weight;
public TangibleAsset(String name, int price, String color){
this.name = name;
this.price = price;
this.color = color;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getPrice() {
return this.price;
}
@Override
public String getColor() {
return this.color;
}
@Override
public double getWeight() {
return this.weight;
}
@Override
public void setWeight(double weight) {
this.weight = weight;
}
}
package Exam;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book book = new Book("์ดํํฐ๋ธ ์๋ฐ 3/E", 30000, "green", "A123");
Computer computer = new Computer("gram", 1500000, "Black", "LG");
book.setWeight(150.0);
computer.setWeight(980.5);
System.out.println(computer.getName() + " : " + computer.getPrice() + " "
+ computer.getColor() + " " + computer.getMakerName() + " " + computer.getWeight());
System.out.println(book.getName() + " : " + book.getPrice() + " " + book.getColor() + " "
+ book.getIsbn() + " " + book.getWeight());
}
}

Uploaded by N2T