Skip to content

[Chapter 8] 컴포지션 예시가 좋은 예시인가..? #64

Discussion options

You must be logged in to vote

160p 내용입니다!

public class PhysicalAttack {

    int singleAttackDamage() {
        return 10;
    }

    int doubleAttackDamage() {
        return 25;
    }
}
public class FightPhysicalAttack extends PhysicalAttack {
    @Override
    int singleAttackDamage() {
        return super.singleAttackDamage() + 10;
    }

    @Override
    int doubleAttackDamage() {
        return super.doubleAttackDamage() + 10;
    }
}

이런 상속 구조에서 FightPhysicalAttack는 10 만큼 큰 데미지를 리턴하기 위한 목적으로 오버라이드 되었습니다.

PhysicalAttack physicalAttack = new FightPhysicalAttack();
int damage = physicalAttack.doubleAttackDamage();
System.out.println("damage = " + damage);  //35

이때 만약 부모 클래스가 이런 식으로 바뀐다면 결과가 달라집니다.

public class Ph…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@firewoody237
Comment options

firewoody237 Jul 7, 2024
Collaborator Author

Answer selected by firewoody237
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chapter08 강한 결합: 복잡하게 얽혀서 풀 수 없는 구조
3 participants