제목을 뭐라고 해야할지 몰라서... 일단 이런 느낌이에여
Parent가 직접적으로 생성되어 사용되는 일은 없고
FirstChild / SecondChild를 생성 후 사용해요.
만약에 뭔가 계산하는게 있다고 한다면..
아 글고 c#에여
public abstract class Parent
{
public int Something();
~~~~
public virtual int Cal();
}
public class FirstChild : Parent
{
public override int Cal()
{
return 1;
}
}
public class SecondChild : Parent
{
public override int Cal()
{
return 2;
}
}
이런 식으로 해서 First / Second 안에 다시 딴 놈 집어 넣으면 되는데..
만약에 인터페이스로 빼서 만든다고 한다면
public interface IParent
{
int Something();
}
public class Parent : IParent
{
public int Something();
}
public interface IFirstChild
{
int Cal();
}
public class FirstChild : Parent , IFirstChild
{
public int Cal()
{
return 1;
}
}
public interface ISecondChild
{
int Cal();
}
public class SecondChild : Parent , ISecondChild
{
public int Cal()
{
return 2;
}
}
이런 식으로 각각 인터페이스를 따로 뜯어서 쓰는게 좋을까요?
아니면
public interface IParent
{
int Something();
int Cal();
}
public abstract class Parent : IParent
{
public int Something();
public int Cal();
}
public interface IFirstChild : IParent
{
new int Cal();
}
public class FirstChild : Parent , IFirstChild
{
new public int Cal()
{
return 1;
}
}
public interface ISecondChild
{
new int Cal();
}
public class SecondChild : Parent , ISecondChild
{
new public int Cal()
{
return 2;
}
}
이런 식으로 인터페이스 상속 시킨 뒤, new 키워드로 하는 것이 좋을까요?
목적에 따라서 다르겠지만.. 그래도 어떤 식으로 하는게 좋을지 궁금합니다.
----------------------------추가--------------------------
이런 고민을 하게 된 이유가... 일단 Parent는 굳이 객체 생성으로 안쓰고 있는 상황인데..
다른 행동을 할 수 있는 Cal() 이라는 함수가 Parent의 Interface로 빠져서 사용되는 상황( + Parent에서 사용은 안되는데 자식들은 사용함. 그리고 계산도 다름 )
그런데 Parent의 Interface를 만들어놨는데 그곳에 집어넣어놔야 하는지...
아니면 나중에 자식들 기능이 다른 부분이 있을 때 그 부분에 각각 따로 집어넣어야 하는지 궁금해서 올렸습니다.
인터페이스가 아니라 클래스라면 그냥 virutal처럼 하면 될 것 같은데 인터페이스 사용에 있어서 아는게 부족하다보니 궁금해서 질문드립니당.
댓글 0