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
6 changes: 3 additions & 3 deletions TIL/2026-06-10-캡슐화, 컬렉션.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class ListExam {
System.out.println(numbers);
System.out.println(numbers.contains(1));
HashSet<Integer> numberSet = new HashSet<>();
numbers.add(1);
numbers.add(1);
numbers.add(1);
numberSet.add(1);
numberSet.add(1);
numberSet.add(1);
System.out.println(numberSet);
System.out.println(numberSet.contains(2));

Expand Down
60 changes: 60 additions & 0 deletions TIL/2026-06-11-상속.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 2026-06-11 상속

## 오늘 배운 내용

- 상속을 받으면 부모 클래스의 생성자를 반드시 호출해야만 한다.
- @Override를 통해 기존 부모의 것을 가져 올 수 있고 필요하면 수정도 가능하다.

## 실습 코드

```java
public class SuperHero extends Hero {
public static void main(String[] args) {
SuperHero superHero = new SuperHero("홍길동", 50);
superHero.attack();
superHero.run();
superHero.setFlying(true);
}

private boolean isFlying;

//상속을 받으면 부모 클래스의 생성자를 반드시 호출해야만 한다.
public SuperHero(String name, int hp) {
super(name, hp);
}


public boolean isFlying() {
return isFlying;
}

public void setFlying(boolean flying) {
isFlying = flying;
}

@Override
public void run() {
System.out.println("매우 빠르게 달렸다.");
}

@Override
void attack() {
super.attack();
if (isFlying) {
System.out.println("한번 더 때려라");
}
}
}
```

## 어려웠던 점

- 상속받는 클래스 만드는 것과 테스트 코드 짜는법

## 해결 방법

- 계속 원하는 바가 안나와서 테스트 코드를 보니 When 순서가 이상해서 안되었다는 걸 알아서 앞으로 When 순서도 잘 생각해서 넣어야한다는 걸 알았습니다.

## 내일 더 공부할 것

- 상속에 관련 클래스를 더 공부하고 여러개를 만들어볼 것
59 changes: 59 additions & 0 deletions game2/game2.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@startuml
class SuperHero extends Hero {
+void setFlying(bool flying)
+boolean isFlying()
+ run()+
}
class Hero {
- int hp 100
- int mp
- int money 100
-String name
+ void attack()
+ run()
}
class Cleric {
- int hp
- int mp
- int MaxHP
- int MaxMP
- String name
+ selfAid()
+ pray()
}
class GreatWizard extends Wizard {
+heal()+
+superHeal()
}

class Wizard {
- int hp
- int mp = 100
- Wand wand
- String name
+ setWand()
+ heal()
}

class Wand {
- String name
- double power
}

class Person {
-int age
-String name
-int birthYear
}
class PoisonSlime extends Slime {
+attack()+
}

class Slime {
-String suffix
-int hp
+attack()
}


@enduml
14 changes: 14 additions & 0 deletions game2/src/main/java/com/survivalcoding/260616_연습문제
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
슈퍼클래스 서브클래스
1 Person Student
2 Car Engine
3 Father Child
4 Food Susi
5 SuperMan Man

2, 3, 5

부모 클래스와 자식 클래스 1개씩

기계 기계 책
Phone Car Dictionary
아이폰 슈퍼카 영어사전
53 changes: 53 additions & 0 deletions game2/src/main/java/com/survivalcoding/GreatWizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.survivalcoding;

import static com.survivalcoding.Hero.MaxHP;

public class GreatWizard extends Wizard {
private int Mp = 150;

@Override
public int getMp() {
return Mp;
}

@Override
public void setMp(int mp) {
Mp = mp;
}
Comment on lines +14 to +16

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

setMp 오버라이드가 상위 클래스의 음수 MP 방어 계약을 깨고 있습니다.

Line 14-16은 음수 MP를 그대로 허용합니다. Wizard.setMp()는 음수 입력을 예외 처리하므로, 하위 클래스도 동일 제약을 유지해야 타입 대체가 안전합니다.

제안 수정안
 `@Override`
 public void setMp(int mp) {
-    Mp = mp;
+    if (mp < 0) {
+        throw new IllegalArgumentException("마법사의 MP는 0 이상이여야 한다");
+    }
+    Mp = mp;
 }
📝 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 void setMp(int mp) {
Mp = mp;
}
public void setMp(int mp) {
if (mp < 0) {
throw new IllegalArgumentException("마법사의 MP는 0 이상이여야 한다");
}
Mp = mp;
}
🤖 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 `@game2/src/main/java/com/survivalcoding/GreatWizard.java` around lines 14 -
16, The setMp method in the GreatWizard class is not validating that the mp
parameter is non-negative, which violates the contract established by the parent
class Wizard.setMp(). To fix this, add the same negative value validation in
GreatWizard.setMp() that exists in the parent Wizard class - check if mp is less
than zero and throw an appropriate exception (such as IllegalArgumentException)
with a descriptive message, or alternatively call the parent class's setMp
method to leverage its existing validation logic. This ensures type substitution
safety and maintains consistent precondition enforcement across the inheritance
hierarchy.


public GreatWizard() {
super();
}


@Override
void heal(Hero hero) {
int basePoint = 25;
int baseMp = 5;

if (this.getMp() >= 5) {
hero.setHp(hero.getHp() + basePoint);
this.setMp(this.getMp() - baseMp);
System.out.println("힐을 시전했습니다.");
System.out.println(hero + "HP :" + hero.getHp());
} else {
System.out.println("마나가 부족합니다");
}
}

void superHeal(Hero hero) {

int basePoint = MaxHP;
int baseMp = 50;


if (this.getMp() >= 50) {
hero.setHp(basePoint);
this.setMp(this.getMp() - baseMp);
System.out.println("슈퍼 힐을 시전했습니다");
System.out.println(hero + "HP :" + hero.getHp());
} else {
System.out.println("마나가 부족합니다");
}
}
}
20 changes: 20 additions & 0 deletions game2/src/main/java/com/survivalcoding/Hero.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

public class Hero {
public static int money = 100;
public static int MaxHP = 100;

private String name;
private int hp;
Expand All @@ -13,6 +14,21 @@ public int getHp() {
}

public void setHp(int hp) {
if (hp < 0) {
hp = 0;
}
this.hp = hp;
}

public Hero() {
this.name = "홍길동";
this.hp = 100;
System.out.println("홍길동 생성자");

}

public Hero(String name, int hp) {
this.name = name;
this.hp = hp;
}
Comment on lines +30 to 33

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

인자 생성자가 HP 음수 클램프 계약을 우회합니다.

Line 30-33에서 this.hp = hp;로 직접 대입하면, setHp()가 보장하는 음수 방어(Line 17-21)가 적용되지 않아 객체가 음수 HP로 생성될 수 있습니다. 생성 시점에도 동일한 불변식을 유지하도록 setter를 사용하세요.

제안 수정안
 public Hero(String name, int hp) {
     this.name = name;
-    this.hp = hp;
+    setHp(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 `@game2/src/main/java/com/survivalcoding/Hero.java` around lines 30 - 33, The
Hero constructor in the parameterized constructor (lines 30-33) directly assigns
hp with `this.hp = hp;` which bypasses the validation logic in the setHp()
method (lines 17-21) that prevents negative values. Replace the direct
assignment `this.hp = hp;` with a call to `setHp(hp)` to ensure the same HP
validation invariant is enforced during object construction as well as when
updating the value later.


Expand Down Expand Up @@ -40,6 +56,10 @@ void attack() {
hp -= 1;
System.out.println("공격했다");
}

public void run() {
System.out.println("달렸다");
}
}


37 changes: 0 additions & 37 deletions game2/src/main/java/com/survivalcoding/ListExam.java

This file was deleted.

2 changes: 1 addition & 1 deletion game2/src/main/java/com/survivalcoding/Person.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.survivalcoding;

public class Person {
final int age;
int age;
private String name;
private int birthYear;

Expand Down
31 changes: 31 additions & 0 deletions game2/src/main/java/com/survivalcoding/PoisonSlime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.survivalcoding;

public class PoisonSlime extends Slime {
private int poisonCount = 5;

public int getPoisonCount() {
return poisonCount;
}

public void setPoisonCount(int poisonCount) {
this.poisonCount = poisonCount;
}

public PoisonSlime(String name) {
super(name);
}

@Override
void attack(Hero hero) {

if (this.getPoisonCount() > 0) {
super.attack(hero);
System.out.println("추가로, 독 포자를 살포했다!");
hero.setHp(hero.getHp() - (int) (hero.getHp()) / 5);
System.out.println("~포인트 데미지");
this.poisonCount--;
} else {
super.attack(hero);
}
}
}
17 changes: 17 additions & 0 deletions game2/src/main/java/com/survivalcoding/Slime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.survivalcoding;

public class Slime {
final 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);
}
}
39 changes: 39 additions & 0 deletions game2/src/main/java/com/survivalcoding/SuperHero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.survivalcoding;

public class SuperHero extends Hero {
public static void main(String[] args) {
SuperHero superHero = new SuperHero("홍길동", 50);
superHero.attack();
superHero.run();
superHero.setFlying(true);
}

private boolean isFlying;

//상속을 받으면 부모 클래스의 생성자를 반드시 호출해야만 한다.
public SuperHero(String name, int hp) {
super(name, hp);
}


public boolean isFlying() {
return isFlying;
}

public void setFlying(boolean flying) {
isFlying = flying;
}

@Override
public void run() {
System.out.println("매우 빠르게 달렸다.");
}

@Override
void attack() {
super.attack();
if (isFlying) {
System.out.println("한번 더 때려라");
}
}
}
Loading