通用抽象方法

时间:2016-10-21 19:19:05

标签: c# generics abstraction

我在尝试创建一个抽象类和一个通用的方法时遇到了麻烦。

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);
}

当我在寻找解决方案时,我已经看到了关于这个主题的一些答案,并且所有人都建议抽象类本身是通用的。我不想让抽象类具有通用性,因为我看到的例子会让我做class GameRoomManager : Abs<GameRoomManager>。但我希望该方法返回GameRoom类型,而不是GameRoomManager。

我对仿制药并不完全熟悉,所以如果我出错了,请指出正确的方向

1 个答案:

答案 0 :(得分:0)

您必须与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

使用具有泛型类型的基类&amp;接口

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");

    }
}