1
public abstract class State
{
public virtual Enter(/* THIS NEED A PARAMETER */)
{
// an empty method
}
}
public class PlayerState : State
{
public override Enter(Player pl)
{
// method implementation
}
}
public class GoalkeeperState : State
{
public override Enter(Goalkeeper gk)
{
// method implementation
}
}
//EXAMPLE OF USE
public State globalState;
globalState.Enter(owner);
// OWNER CAN BE PLAYER OR GOALKEEPER
가상 및 재정의 된 메서드는 동일한 "인쇄"가 있어야한다는 것을 알고 있습니다. 그래서 여기에 디자인 결함이 있습니다. 이렇게 가능한 것이 있습니다. 어떻게해야합니까? 어떻게 하시겠습니까?CSHARP 가상 메서드 및 매개 변수
이public abstract class State<T>
{
public virtual Enter(T item)
{
// an empty method
}
}
public class PlayerState : State<Player>
{
public override Enter(Player pl)
{
// method implementation
}
}
public class GoalkeeperState : State<Goalkeeper>
{
public override Enter(Goalkeeper gk)
{
// method implementation
}
}
그리고 상태를 어쩌면 – Phil
으로 지정하거나 'Enter'도 추상적으로 만듭니다. 우리가 코드에 대해 충분히 알지 못한다고 생각합니다. 따라서 직접적인 질문에 대답하고 자신의 시나리오에서 잘못 될 수 있다는 힌트를 피하는 데 집중했습니다. –
@DanielHilgarth 이런 종류의 메소드를 필요로하지 않는 많은 클래스들은 클래스 – Orvel