提问者:小点点

通用抽象方法


在尝试创建一个抽象类和其中的一个本质上是泛型的方法时,我遇到了麻烦。

class GameRoomManager : MonoBehaviour {
    public GameRoom GetSomething(string s){
        //do things here
        return GameRoomvar;
    }
}

现在我有另一个类做类似的事情,但涉及不同的类

class PlayerManager : MonoBehaviour{
    public Player GetSomething(string s){
        //player related things here
        return Playervar;
    }
}

我想让GameRoomManager和PlayerManager两个类都继承自抽象类Abs

class GameRoomManager : Abs{
    public override GameRoom GetSomething<GameRoom>(string s){
        return GameRoomvar;
    }
}

那里

public abstract class Abs{
    public T GetSomething<T>(string s);
}

当我在寻找解决方案时,我已经看到了关于这个主题的一些答案,并且都建议抽象类本身是通用的。我不想让抽象类变得通用,因为我看到的示例会让我做类GameRoomManager: Abs

我对泛型不太熟悉,所以如果我出错了,请给我指出正确的方向


共1个答案

匿名用户

您必须与PQRHIJ有共同之处,类才能使用通用方法。

A计划

用接口连接事物。

public interface IPart
{
    // put things here that are common between Part and GameRoom
    int ID { get; }
}
public interface IAbs
{
    IPart GetSomething(string name);
}

public class GameRoom : IPart
{
    public int ID { get; set; }
}

public class GameRoomManager : IAbs
{
    GameRoom part;

    #region IAbs Members

    public GameRoom GetSomething(string name)
    {
        return part;
    }


    IPart IAbs.GetSomething(string name)
    {
        return GetSomething(name);
    }

    #endregion
}

public class Player : IPart
{
    public int ID { get; set; }
}

public class PlayerManager : IAbs
{
    Player part;


    #region IAbs Members

    public Player GetSomething(string name)
    {
        return part;
    }
    IPart IAbs.GetSomething(string name)
    {
        return GetSomething(name);
    }

    #endregion
}

B计划

使用泛型类型的基类

public interface IItem
{
    // put things here that are common between Part and GameRoom
    int ID { get; }
}
public class GameRoom : IItem
{
    public int ID { get; set; }
}
public class Player : IItem
{
    public int ID { get; set; }
}


public interface IAbs
{
    IItem GetItem(string guid);
}
public abstract class Abs<T> : IAbs
    where T : IItem
{
    protected abstract T GetItem(string name);

    protected Abs(T item)
    {
        this.Item=item;
    }
    protected T Item { get; private set; }

    #region IAbs Members

    IItem IAbs.GetItem(string name)
    {
        return GetItem(name);
    }

    #endregion
}


public class GameRoomManager : Abs<GameRoom>
{
    public GameRoomManager(GameRoom room) : base(room)
    {
    }
    protected override GameRoom GetItem(string guid)
    {
        return Item;
    }
    public GameRoom GetRoom(string guid) { return GetItem(guid); }
}

public class PlayerManager : Abs<Player>
{
    public PlayerManager(Player player)
        : base(player)
    {
    }
    protected override Player GetItem(string guid)
    {
        return Item;
    }
    public Player GetPlayer(string guid) { return GetItem(guid); }
}

以下是一些示例用法:

class Program
{
    static void Main(string[] args)
    {

        List<IAbs> managers=new List<IAbs>();
        var pm=new PlayerManager(new Player() { ID=1001 });
        var gm=new GameRoomManager(new GameRoom() { ID=2050 });

        managers.Add(pm);
        managers.Add(gm);

        IItem part = managers[0].GetItem("0000");

    }
}