2
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

仮想メソッドとオーバーライドされたメソッドは同じ「印刷」を持つ必要があることを理解しています。したがって、ここには設計上の欠陥があります。だから、このようなことが可能です。これどうやってするの ?これをどのように行いますか?

4

2 に答える 2

6

ここでジェネリックを使用できます:

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
    }
}
于 2013-03-25T11:24:33.690 に答える
0

あなたは定義することができます

public override Enter(State pl)

または、しかし、私はあなたが正しくやりたいことを理解したかどうかわかりません、このようなもの:

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(); }
}
于 2013-03-25T11:27:13.247 に答える