2013-03-25 4 views
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 
    } 
} 

답변

5

현재 제네릭을 사용할 수 있습니다

public class Player 
{ 
    public virtual Enter() {} 
} 

public class GoalKeeper : Player 
{ 
    public override Enter() {} 
} 


public class State 
{ 
    public List<Player> players {get; private set;} 

    public State() { players = new List<Player(); } 
} 
+0

그리고 상태를 어쩌면 – Phil

+0

으로 지정하거나 'Enter'도 추상적으로 만듭니다. 우리가 코드에 대해 충분히 알지 못한다고 생각합니다. 따라서 직접적인 질문에 대답하고 자신의 시나리오에서 잘못 될 수 있다는 힌트를 피하는 데 집중했습니다. –

+0

@DanielHilgarth 이런 종류의 메소드를 필요로하지 않는 많은 클래스들은 클래스 – Orvel

0

당신이

public override Enter(State pl) 

을 정의하거나 수 있지만, 난 당신이 이런 식으로, 제대로 뭔가를 원하는 것을 내가 이해 확실하지 않다 :