[Java] 12. 상속_2.추상화&다형성

김미숙's avatar
Feb 17, 2025
[Java] 12. 상속_2.추상화&다형성
‼️
다형성은 is 를 만족
morning is morning = morning is car
 
package ex05.ch02; class Car { void run() { System.out.println("자동차 달린다"); } } class Morning extends Car { void run() { System.out.println("모닝 달린다"); } } class Sonata extends Car { void run() { System.out.println("소나타 달린다"); } } public class ExMet01 { public static void main(String[] args) { Morning a1 = new Morning(); // [morning(run), car(run)] a1.run(); Car a2 = new Morning(); // [morning(run), car (run)] a2.run(); // 부모를 바라보지만 부모 메서드가 실행되지 않음 Car a3 = new Sonata(); // [sonata(run), car (run)] a3.run(); } }
 

1. 상속에서 변수 찾기

package ex05.ch02; class Animal { int i = 1; String name = "동물"; } class Cat extends Animal { int k = 2; String name = "고양이"; } class Dog extends Animal { String name = "강아지"; } public class ExVar01 { public static void main(String[] args) { Animal a1 = new Animal(); // [animal(i=1, name = 동물)] System.out.println(a1.i + ", " + a1.name); Dog a2 = new Dog(); // [animal(i=1, name = 동물), dog(name = 강아지)] System.out.println(a2.i + ", " + a2.name); Animal a3 = new Dog(); // [animal(i=1, name = 동물), dog(name = 강아지)] System.out.println(a3.i + ", " + a3.name); Cat a4 = new Cat(); // [animal(i=1, name = 동물), cat(k = 2,name = 고양이)] System.out.println(a4.i + ", " + a4.name); System.out.println("Cat k값: "+a4.k); Animal a5 = new Cat(); // [animal(i=1, name = 동물), cat(k = 2,name = 고양이)] System.out.println(a5.i + ", " + a5.name); System.out.println("Cat k값: "+a5.k); // 오류 } }
 

2. 상속에서 메서드 찾기

package ex05.ch02; class 운동선수 { // getter public int getHp() { return 34893439; } // setter public void setHp(int hp) { } } class 타이슨 extends 운동선수 { int hp = 100; // getter public int getHp() { return hp; } // setter // context (문맥) public void setHp(int hp) { this.hp = hp; } } class 맥그리거 extends 운동선수 { // getter int hp = 100; public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } } public class ExMet02 { static void attack(맥그리거 u1) { u1.hp = u1.hp - 10; } static void attack(타이슨 u1) { u1.hp = u1.hp - 10; } static void attack(운동선수 u1) { u1.setHp(u1.getHp() - 10); } public static void main(String[] args) { 맥그리거 u1 = new 맥그리거(); attack(u1); System.out.println(u1.hp); 타이슨 u2 = new 타이슨(); attack(u2); System.out.println(u1.hp); 운동선수 u3 = new 타이슨(); attack(u3); attack(u3); attack(u3); System.out.println(u3.getHp()); } }
 

3. 오버라이드 문제 풀기

‼️
DarkTempler (hp=100, power=70)
Arkan (hp=100, power=70)
 
5가지 유닛이 서로 다 공격할 수 있게 attack() 구현하기
 
생성
→ 질럿2
→ 드라군2
→ 다크템플러2
→ 리버2
→ 아칸2 공격 → 질럿이 드라군 공격 hp 확인
→ 질럿이 다크템플러 공격 hp 확인
→ 리버가 아칸 공격 hp 확인
→ 아칸 리버 공격 hp 확인
→ 드라군이 다크템플러 공격 hp 확인
→ 리버가 리버 공격 hp 확인
 
  1. 오버로딩으로 게임 만들기
package ex05.ch03; class Protoss { } class Arkan { int hp; int power; public Arkan() { this.hp = 100; this.power = 70; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } } class DarkTempler { int hp; int power; public DarkTempler() { this.hp = 100; this.power = 70; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } } class River { int hp; int power; public River() { this.hp = 100; this.power = 50; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } } class Dragoon { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } } class Zealot { int hp; int power; public Zealot() { this.hp = 100; this.power = 20; } public void attack(Arkan unit) { unit.hp = unit.hp - this.power; } public void attack(DarkTempler unit) { unit.hp = unit.hp - this.power; } public void attack(Dragoon unit) { unit.hp = unit.hp - this.power; } public void attack(Zealot unit) { unit.hp = unit.hp - this.power; } public void attack(River unit) { unit.hp = unit.hp - this.power; } } public class StarGame { public static void main(String[] args) { Zealot z1 = new Zealot(); Zealot z2 = new Zealot(); Dragoon d1 = new Dragoon(); Dragoon d2 = new Dragoon(); River r1 = new River(); River r2 = new River(); DarkTempler da1 = new DarkTempler(); DarkTempler da2 = new DarkTempler(); Arkan a1 = new Arkan(); Arkan a2 = new Arkan(); z1.attack(d1); System.out.println("드라군 d1의 hp : " + d1.hp); z1.attack(z2); System.out.println("질럿 z2의 hp : " + z2.hp); // → 질럿이 드라군 공격 hp 확인 z1.attack(d2); System.out.println("드라군 d2의 hp : " + d1.hp); // → 질럿이 다크템플러 공격 hp 확인 z1.attack(da1); System.out.println("다크템플러 da1의 hp: " + da1.hp); // → 리버가 아칸 공격 hp 확인 r1.attack(a1); System.out.println("아칸 a1의 hp : " + a1.hp); // → 아칸 리버 공격 hp 확인 a1.attack(r1); System.out.println("리버 r1의 hp : " + r1.hp); // → 드라군이 다크템플러 공격 hp 확인 d1.attack(da1); System.out.println("다크템플러 da1의 hp : " + da1.hp); // → 리버가 리버 공격 hp 확인 r1.attack(r2); System.out.println("리버 r2의 hp : " + r2.hp); } }
 
  1. 오버라이드로 게임 만들기
package ex05.ch04; class Protoss { public int getHp() { return 100; } public void setHp(int hp) { } public void attack(Protoss protoss) { } } class Arkan extends Protoss { int hp; int power; public Arkan() { this.hp = 100; this.power = 70; } // getter public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } public void attack(Protoss a1) { a1.setHp(a1.getHp() - power); } } class DarkTempler extends Protoss { int hp; int power; public DarkTempler() { this.hp = 100; this.power = 70; } // getter public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } public void attack(Protoss da1) { da1.setHp(da1.getHp() - this.power); } } class River extends Protoss { int hp; int power; public River() { this.hp = 100; this.power = 50; } // getter public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } public void attack(Protoss r1) { r1.setHp(r1.getHp() - this.power); } } class Dragoon extends Protoss { int hp; int power; public Dragoon() { this.hp = 100; this.power = 10; } // getter public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } public void attack(Protoss d1) { d1.setHp(d1.getHp() - this.power); } } class Zealot extends Protoss { int hp; int power; public Zealot() { this.hp = 100; this.power = 20; } // getter public int getHp() { return hp; } // setter public void setHp(int hp) { this.hp = hp; } public void attack(Protoss z1) { z1.setHp(z1.getHp() - this.power); } } public class StarGame { public static void main(String[] args) { // 2개씩 생성 Protoss z1 = new Zealot(); Protoss z2 = new Zealot(); Protoss d1 = new Dragoon(); Protoss d2 = new Dragoon(); Protoss r1 = new River(); Protoss r2 = new River(); Protoss da1 = new DarkTempler(); Protoss da2 = new DarkTempler(); Protoss a1 = new Arkan(); Protoss a2 = new Arkan(); // 공격 //→ 질럿이 드라군 공격 hp 확인 z2.attack(d2); System.out.println(d2.getHp()); //→ 질럿이 다크템플러 공격 hp 확인 z2.attack(da1); System.out.println(da1.getHp()); //→ 리버가 아칸 공격 hp 확인 r1.attack(a1); System.out.println(a1.getHp()); //→ 아칸 리버 공격 hp 확인 a2.attack(r1); System.out.println(r1.getHp()); //→ 드라군이 다크템플러 공격 hp 확인 d1.attack(da2); System.out.println(da2.getHp()); //→ 리버가 리버 공격 hp 확인 r1.attack(r2); System.out.println(r2.getHp()); } }
 
Share article

parangdajavous