260616_10_박승빈#61
Conversation
상속 과제 코드 리뷰🎯 좋은 점들1. 캡슐화 원칙의 적용
2. 상속의 올바른 활용
3. 접근 제어자의 적절한 선택
❓ 생각해볼 점들1. 클래스 정의의 일관성 SuperHero superHero = new SuperHero("홍길동", 100);
2. 부모 클래스의 초기화
3. Hero와 Wizard의 관계
이렇게 공통된 부분이 있을 때, 상속의 관점에서 어떻게 설계할 수 있을까요? 💭
4. setHp() 메서드의 범용성
5. 메서드 오버라이딩의 의도 public void heal(Hero hero) {
super.heal(hero);
}현재는 부모 메서드를 그대로 호출하고 있습니다.
📝 학습을 위한 제안상속 설계 재검토: 접근 제어자의 역할: 수고하셨습니다! 상속의 기본 개념을 잘 이해하고 구현하고 있으세요. 위의 질문들을 통해 더욱 견고한 설계를 만들어보세요. 🚀 Walkthrough
Changes상속 기반 게임 클래스 계층 구현
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with 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.
Inline comments:
In `@game/src/main/Exercise.md`:
- Line 3: The markdown headings are missing required spaces after the hash
symbol, violating the MD018 markdownlint rule. In the file
game/src/main/Exercise.md, add a space between the hash symbol and the heading
text at line 3 (change `#문제 1` to `# 문제 1`) and at line 13 (change `#문제` to `#
문제`) to comply with proper markdown syntax for ATX headings.
In `@game/src/main/java/com/survivalcoding/Hero.java`:
- Around line 54-60: The sit method directly modifies this.hp instead of using
the setHp setter method, which bypasses the boundary value contract enforcement.
Replace the direct assignment this.hp += sec with a call to setHp(this.hp + sec)
in the sit method. Additionally, apply the same fix to the other location at
lines 68-70 where this.hp is directly modified, ensuring all HP modifications
throughout the Hero class use the setHp method to maintain consistency and
enforce upper/lower bounds.
- Around line 32-34: The setHp method only constrains the upper limit of HP to
MAX_HP using Math.min, but does not enforce a lower bound, allowing negative HP
values to be stored. Modify the setHp method to also apply a lower bound
constraint of 0, ensuring that HP never goes below zero. This can be
accomplished by applying both Math.min for the upper limit and Math.max for the
lower limit (0) to the hp parameter.
In `@game/src/main/java/com/survivalcoding/Wizard.java`:
- Around line 13-16: The mana condition in the heal method of the Wizard class
is hardcoded to check if this.mp >= 10, but then deducts this.mp -= mpCost,
which may be a different value (e.g., 5 in GreatWizard subclass). Change the
condition from this.mp >= 10 to this.mp >= mpCost so that both the validation
check and the mana deduction use the same field, ensuring that subclass
overrides of mpCost are properly respected and the logic remains consistent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 77c8db58-2c75-4f08-950d-b3620360b068
📒 Files selected for processing (7)
game/src/main/Exercise.mdgame/src/main/java/com/survivalcoding/GreatWizard.javagame/src/main/java/com/survivalcoding/Hero.javagame/src/main/java/com/survivalcoding/Main.javagame/src/main/java/com/survivalcoding/PoisonSlime.javagame/src/main/java/com/survivalcoding/Slime.javagame/src/main/java/com/survivalcoding/Wizard.java
| @@ -0,0 +1,22 @@ | |||
| 11장 상속 연습문제 | |||
|
|
|||
| #문제 1 | |||
There was a problem hiding this comment.
ATX 헤딩 뒤 공백 누락(MD018) 수정이 필요합니다.
Line 3, Line 13의 #문제 형식은 markdownlint 경고 대상입니다. # 문제로 공백을 넣어 주세요.
수정 제안
-#문제 1
+# 문제 1
...
-#문제 2
+# 문제 2Also applies to: 13-13
🧰 Tools
🪛 markdownlint-cli2 (0.22.1)
[warning] 3-3: No space after hash on atx style heading
(MD018, no-missing-space-atx)
🤖 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/Exercise.md` at line 3, The markdown headings are missing
required spaces after the hash symbol, violating the MD018 markdownlint rule. In
the file game/src/main/Exercise.md, add a space between the hash symbol and the
heading text at line 3 (change `#문제 1` to `# 문제 1`) and at line 13 (change `#문제`
to `# 문제`) to comply with proper markdown syntax for ATX headings.
Source: Linters/SAST tools
| public void setHp(int hp) { | ||
| this.hp = Math.min(hp, MAX_HP); | ||
| } |
There was a problem hiding this comment.
setHp가 음수 HP를 허용하고 있습니다.
Line 32-34에서 상한만 제한해 음수값이 그대로 저장됩니다. 전투/독 데미지 누적 시 HP가 음수로 내려가 UI/로직이 깨질 수 있습니다.
수정 제안
public void setHp(int hp) {
- this.hp = Math.min(hp, MAX_HP);
+ this.hp = Math.max(0, Math.min(hp, MAX_HP));
}📝 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.
| public void setHp(int hp) { | |
| this.hp = Math.min(hp, MAX_HP); | |
| } | |
| public void setHp(int hp) { | |
| this.hp = Math.max(0, Math.min(hp, 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/Hero.java` around lines 32 - 34, The
setHp method only constrains the upper limit of HP to MAX_HP using Math.min, but
does not enforce a lower bound, allowing negative HP values to be stored. Modify
the setHp method to also apply a lower bound constraint of 0, ensuring that HP
never goes below zero. This can be accomplished by applying both Math.min for
the upper limit and Math.max for the lower limit (0) to the hp parameter.
| void sit(int sec) { | ||
| //앉은 초 수만큼 hp 증가 | ||
| this.hp += sec; | ||
|
|
||
| System.out.println(this.name + "는" + sec + "초 앉았다"); | ||
| System.out.println("HP가" + sec + "포인트 회복되었다"); | ||
| } |
There was a problem hiding this comment.
HP 변경 로직이 setter를 우회해 상한/하한 계약이 깨집니다.
Line 54-60, 68-70에서 this.hp를 직접 변경/대입합니다. 이미 setHp로 경계값 계약을 관리하고 있으므로, 내부 행동 메서드도 동일 경로를 사용해야 일관성이 유지됩니다.
수정 제안
void sit(int sec) {
- this.hp += sec;
+ setHp(this.hp + sec);
System.out.println(this.name + "는" + sec + "초 앉았다");
System.out.println("HP가" + sec + "포인트 회복되었다");
}
void sleep() {
- this.hp = 100;
+ setHp(MAX_HP);
System.out.println(this.name + "는 잠을 자고 회복했다!");
}Also applies to: 68-70
🤖 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/Hero.java` around lines 54 - 60, The
sit method directly modifies this.hp instead of using the setHp setter method,
which bypasses the boundary value contract enforcement. Replace the direct
assignment this.hp += sec with a call to setHp(this.hp + sec) in the sit method.
Additionally, apply the same fix to the other location at lines 68-70 where
this.hp is directly modified, ensuring all HP modifications throughout the Hero
class use the setHp method to maintain consistency and enforce upper/lower
bounds.
| if (this.mp >= 10) { | ||
| hero.setHp(hero.getHp() + heal); | ||
| this.mp -= mpCost; | ||
| System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp()); |
There was a problem hiding this comment.
마나 조건이 mpCost를 무시하고 하드코딩되어 있습니다.
Line 13에서 this.mp >= 10을 사용해 mpCost 변경(예: GreatWizard의 5)이 반영되지 않습니다. 조건과 차감 기준을 동일 필드(mpCost)로 맞춰야 합니다.
수정 제안
- if (this.mp >= 10) {
+ if (this.mp >= this.mpCost) {
hero.setHp(hero.getHp() + heal);
this.mp -= mpCost;
System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp());📝 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.
| if (this.mp >= 10) { | |
| hero.setHp(hero.getHp() + heal); | |
| this.mp -= mpCost; | |
| System.out.println("힐을 시전했습니다. " + hero.name + "HP : " + hero.getHp()); | |
| if (this.mp >= this.mpCost) { | |
| hero.setHp(hero.getHp() + heal); | |
| this.mp -= mpCost; | |
| System.out.println("힐을 시전했습니다. " + hero.name + "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/Wizard.java` around lines 13 - 16, The
mana condition in the heal method of the Wizard class is hardcoded to check if
this.mp >= 10, but then deducts this.mp -= mpCost, which may be a different
value (e.g., 5 in GreatWizard subclass). Change the condition from this.mp >= 10
to this.mp >= mpCost so that both the validation check and the mana deduction
use the same field, ensuring that subclass overrides of mpCost are properly
respected and the logic remains consistent.
YYMMDD_01_홍길동
📝 과제 정보
상속
부모클래스와 자식클래스를 올바르게 사용하는 방법
📋 체크리스트
📷 실행 결과 (또는 테스트 결과)
❓질문 및 어려웠던 점
자식클래스가 부모클래스의 필드값이나 클래스를 사용할 수 있게 만드는 것
클래스의 필드값이 상수일 때 가져오게 하는 방법
🔄 자체 평가 & 회고
상속에서 잘못 이해했던 점에 대해 다시 한번 재대로 이해함
getter와 setter를 재대로 사용하는 방법에 대해 배