상속



GameObject1 {

    int posX;

    int posY;

    int object1_property1;

    int object1_property2;

    ...

}


GameObject2 {

    int posX;

    int posY;

    int object2_property1;

    int object2_property2;

    ...

}

...

GameObject10001 {

    int posX;

    int posY;

    int object1001_property1;

    int object1001_property2;

    ...

}

-----------------------------------------------

게임플레이시 모든 게임 오브젝트 상단에 이름을 출력하기로 하여

다음 처럼 displayName 속성을 추가함.



-----------------------------------------------

GameObject1 {

    int     posX;

    int     posY;

    String  displayName; // <- 추가 

    int     object1_property1;

    int     object1_property2;

    ...

}


GameObject2 {

    int     posX;

    int     posY;

    String  displayName; // <- 추가 

    int object2_property1;

    int object2_property2;

    ...

}


GameObject3 { ... }

GameObject4 { ... }

GameObject5 { ... }

GameObject6 { ... }

GameObject7 { ... }

GameObject8 { ... }

GameObject9 { ... }

...


GameObject10001 {

    int     posX;

    int     posY;

    String  displayName; // <- 추가 

    int object1001_property1;

    int object1001_property2;

    ...

}


헥... 공통된 속성을 수정하려면 오브젝트가 너무 많아서 힘듬....


-----------------------------------------------

C, Pascal 등 객체 지향 이전의 구조적 프로그래밍 방식의 해결법 .  


GameObject {

    int posX;

    int posY;

}


GameObject1 {

    GameObject gameObject;

    int object1_property1;

    int object1_property2;

    ...

}


GameObject2 {

    GameObject gameObject;

    int object2_property1;

    int object2_property2;

    ...

}

...



GameObject 클래스에 displayName 추가함.


GameObject {

    int     posX;

    int     posY;

    String  displayName; // <- 추가 

}


// 기존 코드 변경없음 

GameObject1 {

    GameObject gameObject;

    int object1_property1;

    int object1_property2;

    ...

}


// 기존 코드 변경없음 

GameObject2 {

    GameObject gameObject;

    int object2_property1;

    int object2_property2;

    ...

}


-----------------------------------------------

C++, java, c# 등에서 해결법.


GameObject {

    int     posX;

    int     posY;

    String  displayName; // <- 추가 

}


GameObject1 : public GameObject{

    int object1_property1;

    int object1_property2;

    ...

}


GameObject2 : public GameObject{

    int object2_property1;

    int object2_property2;

    ...

}



====================================================================================================

이런관점에서 OOP에서의 상속은 기존 C 나 Pascal 에서의 구조체, 레코드형과 다르지 않음.

물론 OOP 에서는 상속을 통해 as-is (부모 자식 관계)를 정의 할수 있게 함으로써 

메소드 오버로딩을 통해 유저가 기존 코드를 부분적으로 수정해서 쓰기쉽게 한다던지 하는 훨씬 편리한 기능을 

제공 함.


====================================================================================================

결론. 


설계시에 상속으로 설계를 해야 할지 말아야 할지 고민된다면, 

공통된 속성이 존재 하는지 아닌지를 따져 보면 됨.