-
Notifications
You must be signed in to change notification settings - Fork 18
260616_07_문재원 #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: student/07_문재원
Are you sure you want to change the base?
260616_07_문재원 #54
Changes from all commits
3ee8792
ab6dfcb
9b858b3
1b49366
5955968
51fc702
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # 2026-06-15 캡슐화-컬렉션 | ||
|
|
||
| ## 오늘 배운 내용 | ||
|
|
||
| - 상속: 이전에 만든 클래스와 닮았지만, 일부 다른 클래스 를 만들 필요가 있을 경우가 늘어날 것이다. | ||
| - extends 로 상속을 쓴다. | ||
| - public class SuperHero extends Hero | ||
| - SuperHero가 자식 클래스 이름, Hero가 부모 클래스 | ||
| - boolean 같은 경우 private로 하고 getter 만들때 is를 붙이는게 관습이다. | ||
| - private boolean isFlying; | ||
| - public boolean isFlying(){return isFlying;} | ||
| - 다중상속은 Java에서는 금지 | ||
|
|
||
| - final을 쓰면 상속을 막을 수가 있다. | ||
| - public final class String 하면 상속이 되지 않는다. | ||
|
|
||
| - 상속을 받은 생성자는 부모 생성자부터 호출 후 자식 생성자를 호출한다. | ||
| - 부모 클래스에 생성자가 없다면 자식 클래스에서 에러가 뜬다. | ||
| - 상속을 받으면 반드시 부모(super) 클래스의 생성자를 반드시 호출해야 된다. | ||
| - 해결방법 | ||
| public SuperHero(String name, int hp){super(name,hp);} | ||
| - 단, 부모 클래스에 생성자가 있다면 에러가 나지는 않는다. | ||
|
|
||
| - 부모 클래스(위로(super))로 갈수록 추상적인 것으로 일반화가 되고, 자식 클래스 일 수록 구체화 된다. | ||
|
|
||
|
|
||
| - 오버라이드(Override) | ||
| - 부모 클래스에 있는 메서드를 자식 클래스에서 재정의 하는 것 | ||
| - 상속 받은 메서드를 재정의 | ||
|
|
||
| - 메서드도 부모 클래스에 final 이 있다면 상속을 막을 수 있다. | ||
| - public final void slip() | ||
| - public void attack() { super.attack(); << 상속 받은 것. | ||
|
|
||
|
|
||
| - 어노테이션 (주석) | ||
| - @Override 같은 것들 | ||
|
|
||
| ## 기억할 것 | ||
|
|
||
| - 상속 받은 메서드도 재정의를 할 수 있다. | ||
| - 그 외 오늘 배운 내용을 메모 한 것을 기억해야 한다. | ||
|
|
||
| ========= | ||
|
|
||
| ## 실습 코드 | ||
|
|
||
| ```java | ||
| package com.survivalcoding; | ||
|
|
||
| public class GreatWizard extends Wizard { | ||
| private int maxHp = 300; | ||
|
|
||
| GreatWizard(String name, int hp, int mp) { | ||
| super(name, hp, mp); | ||
| setMp(150); | ||
| } | ||
|
|
||
| @Override | ||
| void heal() { | ||
| if (getMp() < 5) { | ||
| System.out.println("마나가 부족합니다."); | ||
| } else { | ||
| setMp(getMp() - 5); | ||
| setHp(getHp() + 25); | ||
| System.out.println("힐을 시전했습니다. " + getName() + " HP : " + getHp()); | ||
| } | ||
| } | ||
|
Comment on lines
+60
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL 실습 코드의 메서드 시그니처가 실제 구현과 불일치합니다. TIL 문서의 GreatWizard 예제는 Hero 파라미터 없이 작성되었으나, 실제 game/src/main/java/com/survivalcoding/GreatWizard.java와 테스트 케이스(game/src/test/java/com/survivalcoding/GreatWizardTest.java)에서는 모두 Hero 파라미터를 요구합니다. 이러한 불일치는 학습자가 작성한 코드와 실제 프로젝트 코드 사이에 큰 혼동을 초래할 수 있습니다.
📍 Affects 1 file
🤖 Prompt for AI Agents |
||
|
|
||
| void superHeal() { | ||
| if (getMp() < 50) { | ||
| System.out.println("마나가 부족합니다."); | ||
| } else { | ||
| setMp(getMp() - 50); | ||
| setHp(maxHp); | ||
| System.out.println("슈퍼 힐을 시전했습니다. " + getName() + " HP : " + getHp()); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## 어려웠던 점 | ||
|
|
||
| - 어렵다기보다 헷갈린 점인데, 상속 받은 후에 super 클래스에서 private 면 get, set을 써야한다는 점 | ||
|
|
||
| ## 해결 방법 | ||
|
|
||
| - 연습 말고는 없음 | ||
|
|
||
| ## 내일 더 공부할 것 | ||
|
|
||
| - 부모,자식 클래스를 만들어서 여러 번 해보는 것 | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||
| @startuml | ||||||||
| scale 1 | ||||||||
|
|
||||||||
| class GreatWizard extends Wizard { | ||||||||
| -int maxHp = 300 | ||||||||
| +GreatWizard(String:name, int:hp, int:mp | ||||||||
| <<super(name,hp,np)>>, setMp(150)) | ||||||||
|
Comment on lines
+6
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GreatWizard 생성자 표기 오타( Line 7의 🔧 제안 수정안- +GreatWizard(String:name, int:hp, int:mp
- <<super(name,hp,np)>>, setMp(150))
+ +GreatWizard(String:name, int:hp, int:mp, <<super(name,hp,mp)>>, setMp(150))📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
| +<<override>>void heal(Hero hero) | ||||||||
| +void superHeal(Hero hero) | ||||||||
| } | ||||||||
|
|
||||||||
| class Wizard { | ||||||||
| -String name | ||||||||
| -int hp | ||||||||
| -int mp = 100 | ||||||||
| -Wand wand | ||||||||
|
|
||||||||
| +Wizard(String:name, int:hp, int:mp, Wand:wand) | ||||||||
| +Wizard(String:name, int:hp, int:mp) | ||||||||
| +void heal(Hero hero) | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| class PoisonSlime extends Slime { | ||||||||
| -int poisonCount = 5 | ||||||||
|
|
||||||||
| +PoisonSlime(String:suffix<<super(suffix)>>) | ||||||||
| +<<override>>void attack(Hero hero) | ||||||||
| } | ||||||||
|
|
||||||||
| class Slime { | ||||||||
| -final String suffix | ||||||||
| -int hp = 100 | ||||||||
|
|
||||||||
| +Slime(String:suffix) | ||||||||
| +void attack(Hero hero) | ||||||||
| } | ||||||||
|
|
||||||||
| class Hero { | ||||||||
| -String name; | ||||||||
| -int hp; | ||||||||
| -int maxHp = 300; | ||||||||
|
|
||||||||
| +Hero(String name, int hp) | ||||||||
| +void attack() | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| @enduml | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.survivalcoding; | ||
|
|
||
| public class GreatWizard extends Wizard { | ||
| private int maxHp = 300; | ||
|
|
||
| GreatWizard(String name, int hp, int mp) { | ||
| super(name, hp, mp); | ||
| setMp(150); | ||
| } | ||
|
|
||
| @Override | ||
| void heal(Hero hero) { | ||
| if (getMp() < 5) { | ||
| System.out.println("마나가 부족합니다."); | ||
| } else { | ||
| setMp(getMp() - 5); | ||
| hero.setHp(hero.getHp() + 25); | ||
| System.out.println("힐을 시전했습니다. " + hero.getName() + " HP : " + hero.getHp()); | ||
| } | ||
| } | ||
|
|
||
| void superHeal(Hero hero) { | ||
| if (getMp() < 50) { | ||
| System.out.println("마나가 부족합니다."); | ||
| } else { | ||
| setMp(getMp() - 50); | ||
| hero.setHp(hero.getMaxHp()); | ||
| System.out.println("슈퍼 힐을 시전했습니다. " + hero.getName() + " HP : " + hero.getHp()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,33 @@ | ||
| package com.survivalcoding; | ||
|
|
||
| public class Hero { | ||
| String name; | ||
| private String name; | ||
| private int hp; | ||
| private int maxHp = 300; | ||
|
|
||
| Hero(String name, int hp) { | ||
| setName(name); | ||
| setHp(hp); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getMaxHp() { | ||
| return maxHp; | ||
| } | ||
|
|
||
| public void setMaxHp(int maxHp) { | ||
| this.maxHp = maxHp; | ||
| } | ||
|
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 25-27에서 🔧 제안 수정안 public void setMaxHp(int maxHp) {
- this.maxHp = maxHp;
+ if (maxHp < 0) {
+ throw new IllegalArgumentException("최대 HP는 0 이상이어야 합니다.");
+ }
+ this.maxHp = maxHp;
+ if (this.hp > maxHp) {
+ this.hp = maxHp;
+ }
} public void setHp(int hp) {
if (hp < 0) {
throw new IllegalArgumentException("HP는 0 이상이어야 합니다.");
}
- this.hp = hp;
+ this.hp = Math.min(hp, maxHp);
}🤖 Prompt for AI Agents |
||
|
|
||
| public int getHp() { | ||
|
|
||
| return hp; | ||
| } | ||
|
|
||
|
|
||
| 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) { | ||
| super(suffix); | ||
| } | ||
|
|
||
| public int getPoisonCount() { | ||
| return poisonCount; | ||
| } | ||
|
|
||
| @Override | ||
| void attack(Hero hero) { | ||
| if (poisonCount > 0) { | ||
| super.attack(hero); | ||
| System.out.println("추가로, 독 포자를 살포했다!"); | ||
| int poisonDamage = hero.getHp() / 5; | ||
| hero.setHp(hero.getHp() - poisonDamage); | ||
| System.out.println(poisonDamage + "포인트 데미지"); | ||
| poisonCount--; | ||
| } | ||
| if (poisonCount <= 0) { | ||
| super.attack(hero); | ||
| } | ||
|
Comment on lines
+16
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 독 카운트가 1일 때 기본 공격이 한 번 더 적용됩니다. Line 16-26은 🔧 제안 수정안 `@Override`
void attack(Hero hero) {
if (poisonCount > 0) {
super.attack(hero);
System.out.println("추가로, 독 포자를 살포했다!");
int poisonDamage = hero.getHp() / 5;
hero.setHp(hero.getHp() - poisonDamage);
System.out.println(poisonDamage + "포인트 데미지");
poisonCount--;
- }
- if (poisonCount <= 0) {
+ return;
+ }
+ if (poisonCount <= 0) {
super.attack(hero);
}
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.survivalcoding; | ||
|
|
||
| public class Slime { | ||
| private final String suffix; | ||
| private int hp = 100; | ||
|
|
||
| public Slime(String suffix) { | ||
| this.suffix = suffix; | ||
| } | ||
|
|
||
| public String getSuffix() { | ||
| return suffix; | ||
| } | ||
|
|
||
| public int getHp() { | ||
| return hp; | ||
| } | ||
|
|
||
| public void setHp(int hp) { | ||
| this.hp = hp; | ||
| } | ||
|
|
||
| void attack(Hero hero) { | ||
| System.out.println("슬라임 " + suffix + "이/가 공격했다"); | ||
| System.out.println("10의 데미지"); | ||
| hero.setHp(hero.getHp() - 10); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
불완전한 코드 예제를 수정하세요.
라인 33의 코드 조각이 미완성 상태입니다. 주석 표기 "<<" 가 있고 메서드 본문이 제대로 작성되지 않았습니다. 명확한 예제 코드로 교체하거나 설명 텍스트로 변경하는 것이 좋습니다.
💡 개선 제안
라인 33을 다음과 같이 개선할 수 있습니다:
또는 완전한 예제:
🤖 Prompt for AI Agents