diff --git "a/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" "b/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" new file mode 100644 index 0000000..d4baea5 --- /dev/null +++ "b/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" @@ -0,0 +1,23 @@ +# 2026-06-16 상속 + +## 오늘 배운 내용 + +- 상속: 클래스의 확장 +- 메소드 override vs 클래스 overload +- String 클리스 상속 못함, final 붙이면 상속 못함. +- +- (왜) 복사붙여넣기의 문제점 (추가, 수정에 시간이 걸림. 소스의 파악이나 관리가 어려워짐.) +- (어떻게) 클래스 상속 키워드: extands, 매소드 상속 키워드: super +- (언제) 자식 is a 부모 조건 성립시 e.g. 스마트폰 is a 전자기기 +- (어디서)새로운 자식 클래스에서 +- (무엇을)부모 클래스를 +- (누가) JAVA + + + +## 실습 코드 + +```java + +``` + diff --git a/game/game.puml b/game/game.puml new file mode 100644 index 0000000..81894c4 --- /dev/null +++ b/game/game.puml @@ -0,0 +1,31 @@ +@startuml +scale 2 + +class SuperHero extends Hero { + +void setFlying(bool flying) + +boolean isFlying() +} + +class Hero { + -int hp //private + -String name + + +void attack() +} + +class Wizard { + +void heal(Hero) +} + +class GreatWizard extends Wizard { + +void superHeal(Hero) +} + +class Slime { + +void attack(Hero) +} + +class PoisonSlime extends Slime { + +void attack(Hero) <<@Override>> +} +@enduml \ No newline at end of file diff --git a/game/src/main/java/com/survivalcoding/GreatWizard.java b/game/src/main/java/com/survivalcoding/GreatWizard.java new file mode 100644 index 0000000..ed41082 --- /dev/null +++ b/game/src/main/java/com/survivalcoding/GreatWizard.java @@ -0,0 +1,21 @@ +package com.survivalcoding; + +public class GreatWizard extends Wizard{ + private int mp = 150; + void heal (Hero hero) { + hero.setHp(hero.getHp() +25); + hero.setMp(hero.getMp() -5); + + } + void superHeal (Hero hero) { + if (hero.getMp() < 50) { + System.out.println("마나가 부족합니다."); + } + else{ + hero.setHp(Max_Hp); + hero.setMp(hero.getMp() -50); + System.out.println("슈퍼 힐을 시전했습니다. 대상 HP: " + hero.getHp()); + } + + } +} diff --git a/game/src/main/java/com/survivalcoding/PoisonSlime.java b/game/src/main/java/com/survivalcoding/PoisonSlime.java new file mode 100644 index 0000000..30358e5 --- /dev/null +++ b/game/src/main/java/com/survivalcoding/PoisonSlime.java @@ -0,0 +1,29 @@ +package com.survivalcoding; + +public class PoisonSlime extends Slime{ + private int poisonCount = 5; + + + public PoisonSlime(String suffix, int hp) { + super(suffix, hp); + } + public PoisonSlime(String suffix) { + super(suffix, 15); // 기본 독슬라임 hp 값: 15 + } + + @Override + void attack(Hero hero) { + super.attack(hero); + + if (poisonCount != 0){ + System.out.println("추가로 독 포자를 살포했다!"); + int poisonDemage = (int)(hero.getHp() / 5); + hero.setHp(hero.getHp() - poisonDemage); + + poisonCount -= 1; + } + } + + + +} diff --git a/game/src/main/java/com/survivalcoding/Slime.java b/game/src/main/java/com/survivalcoding/Slime.java new file mode 100644 index 0000000..25d7a43 --- /dev/null +++ b/game/src/main/java/com/survivalcoding/Slime.java @@ -0,0 +1,20 @@ +package com.survivalcoding; + +public class Slime { + final String suffix; + int hp; + + public Slime(String suffix, int hp) { + } + public Slime(String suffix) { + this.suffix = suffix; + } + + void attack(Hero hero) { + System.out.println("슬라임 " + suffix + "이/가 공격했다" ); + System.out.println("10의 데미지"); + + hero.setHp(hero.getHp() - 10); + } + +} diff --git a/game/src/main/java/com/survivalcoding/Wizard.java b/game/src/main/java/com/survivalcoding/Wizard.java index 74e9ea1..d76cd12 100644 --- a/game/src/main/java/com/survivalcoding/Wizard.java +++ b/game/src/main/java/com/survivalcoding/Wizard.java @@ -1,6 +1,12 @@ package com.survivalcoding; public class Wizard { + static final int MAX_MP = 100; + int hp; + int mp = MAX_MP; + Wand wand; + private String name; + public int getHp() { return hp; } @@ -14,8 +20,6 @@ public void setHp(int hp) { this.hp = hp; } - int hp; - public int getMp() { return mp; } @@ -28,8 +32,6 @@ public void setMp(int mp) { this.mp = mp; } - int mp; - public String getName() { return name; } @@ -45,8 +47,6 @@ public void setName(String name) { this.name = name; } - private String name; - public Wand getWand() { return wand; } @@ -59,11 +59,11 @@ public void setWand(Wand wand) { this.wand = wand; } - Wand wand; - void heal(Hero hero) { - int basePoint = 10; - int recovPoint = (int) (basePoint * this.wand.power); - hero.setHp(hero.getHp() + recovPoint); + int recoverPoint = 20; + int usedPoint = 10; + + hero.setHp(hero.getHp() + recoverPoint); + hero.setMp(hero.getMp() - usedPoint); } }