Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions TIL/sample/2026-06-16-상속.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 2026-06-16 상속

## 오늘 배운 내용

- 상속: 클래스의 확장
- 메소드 override vs 클래스 overload
- String 클리스 상속 못함, final 붙이면 상속 못함.
-
- (왜) 복사붙여넣기의 문제점 (추가, 수정에 시간이 걸림. 소스의 파악이나 관리가 어려워짐.)
- (어떻게) 클래스 상속 키워드: extands, 매소드 상속 키워드: super
Comment on lines +6 to +10

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

용어와 오탈자를 바로잡아 주세요.

override/overload는 각각 메서드 재정의 / 메서드 오버로딩을 뜻합니다. 또한 클리스, extands, 매소드는 오탈자라 문서 신뢰도가 떨어집니다.

🛠️ پیشن수정 예시
-- 메소드 override vs 클래스 overload
-- String 클리스 상속 못함, final 붙이면 상속 못함.
-- (어떻게) 클래스 상속 키워드: extands, 매소드 상속 키워드: super
+- 메서드 override vs 메서드 overload
+- String 클래스는 상속할 수 없음. `final`이 붙은 클래스도 상속할 수 없음.
+- (어떻게) 클래스 상속 키워드: extends, 메서드 상속 키워드: super
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@TIL/sample/2026-06-16-상속.md` around lines 6 - 10, Correct the terminology and
spelling errors in the inheritance document to improve credibility. Replace
"override vs 클래스 overload" with proper Korean terminology for "메서드
재정의(override)" and "메서드 오버로딩(overload)". Fix the typos: change "클리스" to "클래스",
"extands" to "extends", and "매소드" to "메서드" throughout the document. These
corrections will ensure the technical terms are accurate and the document is
free of spelling errors that undermine its trustworthiness.

- (언제) 자식 is a 부모 조건 성립시 e.g. 스마트폰 is a 전자기기
- (어디서)새로운 자식 클래스에서
- (무엇을)부모 클래스를
- (누가) JAVA



## 실습 코드

```java

```

31 changes: 31 additions & 0 deletions game/game.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@startuml
scale 2

class SuperHero extends Hero {
+void setFlying(bool flying)
+boolean isFlying()
Comment on lines +5 to +6

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

PlantUML 시그니처가 실제 코드와 불일치합니다.

접근제어자(+ vs package-private)와 타입(bool)이 코드와 맞지 않아 다이어그램 신뢰도가 떨어집니다. 현재 코드 기준으로 동기화해 주세요.

🔧 제안 수정안
 class SuperHero extends Hero {
-    +void setFlying(bool flying)
+    +void setFlying(boolean flying)
     +boolean isFlying()
 }

 class Wizard {
-    +void heal(Hero)
+    ~void heal(Hero)
 }

 class GreatWizard extends Wizard {
-    +void superHeal(Hero)
+    ~void superHeal(Hero)
 }

 class Slime {
-    +void attack(Hero)
+    ~void attack(Hero)
 }

 class PoisonSlime extends Slime {
-    +void attack(Hero) <<`@Override`>>
+    ~void attack(Hero) <<`@Override`>>
 }

Also applies to: 17-17, 21-21, 25-25, 29-29

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/game.puml` around lines 5 - 6, Update the PlantUML diagram signatures
for the methods setFlying and isFlying to match the actual code implementation.
Change the access modifier from + (public) to match the package-private
visibility of the actual methods in the code. Additionally, correct the type
declarations from bool to boolean to match the Java type conventions used in the
actual implementation. Apply these same corrections to all other occurrences
mentioned at lines 17, 21, 25, and 29 to ensure the entire diagram is consistent
with the current codebase.

}

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
21 changes: 21 additions & 0 deletions game/src/main/java/com/survivalcoding/GreatWizard.java
Original file line number Diff line number Diff line change
@@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read-only verification: locate hp max constants and references
rg -n -C2 '\bMax_Hp\b|\bMAX_HP\b|\bmaxHp\b'
fd -i 'Hero.java' | xargs -I{} sed -n '1,220p' {}
fd -i 'Wizard.java' | xargs -I{} sed -n '1,220p' {}

Repository: SurvivalCodingCampus/polytech_java2

Length of output: 5060


Max_Hp는 정의되지 않은 식별자입니다. Hero.MAX_HP를 사용해주세요.

Line 15의 Max_Hp는 현재 파일 범위에서나 상속 계층에서도 선언되지 않았습니다. Hero 클래스에서 정의된 static final int MAX_HP = 50을 참조해야 하므로 Hero.MAX_HP로 변경하세요. 또한 Java 명명 규칙에 따라 상수는 MAX_HP (대문자)로 표기합니다.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/GreatWizard.java` at line 15, The
identifier `Max_Hp` on line 15 in the GreatWizard.java file is undefined and
does not follow Java naming conventions for constants. Replace `Max_Hp` with
`Hero.MAX_HP` when calling the `hero.setHp()` method, as MAX_HP is defined as a
static final constant in the Hero class with proper uppercase naming convention.

hero.setMp(hero.getMp() -50);
System.out.println("슈퍼 힐을 시전했습니다. 대상 HP: " + hero.getHp());
Comment on lines +4 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

GreatWizard의 MP 상태 모델이 깨져 있습니다(필드 섀도잉 + 대상 MP 차감).

Line 4에서 부모 Wizard.mp를 섀도잉하고, Line 7/16에서는 시전자 MP가 아니라 대상 hero MP를 차감하고 있습니다. 결과적으로 GreatWizard 자신의 MP 관리가 일관되지 않습니다.

🔧 제안 수정안
 public class GreatWizard extends Wizard{
-    private int mp = 150;
+    public GreatWizard() {
+        this.setMp(150);
+    }

     void heal (Hero hero) {
+        if (this.getMp() < 5) {
+            throw new IllegalStateException("마나가 부족합니다.");
+        }
         hero.setHp(hero.getHp() +25);
-        hero.setMp(hero.getMp() -5);
+        this.setMp(this.getMp() -5);

     }
     void superHeal (Hero hero) {
-        if (hero.getMp() < 50) {
+        if (this.getMp() < 50) {
             System.out.println("마나가 부족합니다.");
         }
         else{
             hero.setHp(Max_Hp);
-            hero.setMp(hero.getMp() -50);
+            this.setMp(this.getMp() -50);
             System.out.println("슈퍼 힐을 시전했습니다. 대상 HP: " + hero.getHp());
         }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/GreatWizard.java` around lines 4 - 17,
The GreatWizard class has an MP management issue caused by field shadowing and
incorrect target selection. Remove the shadowing mp field declaration at line 4,
and fix both the heal and superHeal methods to deduct MP from the GreatWizard
caster (using this context or inherited mp from parent Wizard) instead of from
the target hero parameter. This applies to both the setMp call in the heal
method and the setMp call in the superHeal method, ensuring consistent MP cost
accounting for the spell caster's own resource usage.

}

}
}
29 changes: 29 additions & 0 deletions game/src/main/java/com/survivalcoding/PoisonSlime.java
Original file line number Diff line number Diff line change
@@ -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;
}
}



}
20 changes: 20 additions & 0 deletions game/src/main/java/com/survivalcoding/Slime.java
Original file line number Diff line number Diff line change
@@ -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;
}
Comment on lines +7 to +11

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Slime(String suffix, int hp) 생성자에서 필수 필드 초기화가 누락되었습니다.

Line 7의 생성자가 비어 있어서 final 필드 suffix가 초기화되지 않고, hp도 반영되지 않습니다. 현재 상태로는 컴파일 실패 가능성이 큽니다.

🔧 제안 수정안
 public class Slime {
     final String suffix;
     int hp;

     public Slime(String suffix, int hp) {
+        this.suffix = suffix;
+        this.hp = hp;
     }
     public Slime(String suffix) {
-        this.suffix = suffix;
+        this(suffix, 100);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public Slime(String suffix, int hp) {
}
public Slime(String suffix) {
this.suffix = suffix;
}
public class Slime {
final String suffix;
int hp;
public Slime(String suffix, int hp) {
this.suffix = suffix;
this.hp = hp;
}
public Slime(String suffix) {
this(suffix, 100);
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/Slime.java` around lines 7 - 11, The
Slime(String suffix, int hp) constructor at line 7 is empty and fails to
initialize the required fields. Inside this constructor body, add statements to
initialize the suffix field with the suffix parameter and assign the hp
parameter to the corresponding hp field, following the same pattern as shown in
the Slime(String suffix) constructor which properly assigns this.suffix =
suffix.


void attack(Hero hero) {
System.out.println("슬라임 " + suffix + "이/가 공격했다" );
System.out.println("10의 데미지");

hero.setHp(hero.getHp() - 10);
}

}
22 changes: 11 additions & 11 deletions game/src/main/java/com/survivalcoding/Wizard.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -14,8 +20,6 @@ public void setHp(int hp) {
this.hp = hp;
}

int hp;

public int getMp() {
return mp;
}
Expand All @@ -28,8 +32,6 @@ public void setMp(int mp) {
this.mp = mp;
}

int mp;

public String getName() {
return name;
}
Expand All @@ -45,8 +47,6 @@ public void setName(String name) {
this.name = name;
}

private String name;

public Wand getWand() {
return wand;
}
Expand All @@ -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);
Comment on lines 62 to +67

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

heal()이 시전자 MP가 아니라 대상 MP를 차감하고 있습니다.

Line 67에서 hero.setMp(...)를 호출해 대상의 MP를 소모하고 있어, Wizard.mp 상태가 전혀 사용되지 않습니다. 시전자 자원 소모로 수정하는 게 맞습니다.

🔧 제안 수정안
 void heal(Hero hero) {
     int recoverPoint =  20;
     int usedPoint = 10;
+    if (this.mp < usedPoint) {
+        throw new IllegalStateException("마나가 부족합니다.");
+    }

     hero.setHp(hero.getHp() + recoverPoint);
-    hero.setMp(hero.getMp() - usedPoint);
+    this.setMp(this.mp - usedPoint);
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@game/src/main/java/com/survivalcoding/Wizard.java` around lines 62 - 67, The
heal() method in the Wizard class is incorrectly consuming the target hero's MP
instead of the caster's MP. In the heal() method, the line that calls
hero.setMp(hero.getMp() - usedPoint) is decrementing the target's MP, but it
should instead decrement the Wizard's own MP (the caster). Change the MP
deduction to use the Wizard instance's MP property instead of the hero
parameter's MP property to correctly represent resource consumption by the spell
caster.

}
}