访问泛型基类上的方法

huangapple go评论59阅读模式
英文:

Accessing a method on a generic base class

问题

以下是翻译好的代码部分:

我有多个从通用基类继承的类。我想要拥有这些子类的任意集合实例,并且在任何给定的子类上,都能够调用通用基类中的方法。

public class Pair<A, B>
{
    protected readonly A value1;
    protected readonly B value2;

    public Pair(A value1, B value2)
    {
        this.value1 = value1;
        this.value2 = value2;
    }

    public string ValueOneString()
    {
        return value1.ToString();
    }
}

public class AuPair : Pair<string, int>
{
    public AuPair(string key, int value) : base(key, value) { }
}

public class RePair : Pair<bool, double>
{
    public RePair(bool key, double value) : base(key, value) { }
}

public class Program
{
    public static int Main(string[] args)
    {
        var pair = new Pair<int, int>(76, 4);
        var auPair = new AuPair("Mart", 25);
        var rePair = new RePair(false, 2.371);

        var pairs = new List<dynamic> { pair, auPair, rePair };

        Console.WriteLine(pairs[0].ValueOneString());
        Console.WriteLine(pairs[1].ValueOneString());
        Console.WriteLine(pairs[2].ValueOneString());

        return 0;
    }
}

是否有一个具体的集合类型可以存储这些配对而不是 List<dynamic>?我以为它看起来像 List<Pair<,>>List<Pair>,但都不正确,我找不到一个适合的集合类型来使用。我想在具体的类上调用 GetKeyString() 方法,而不是使用动态关键字。

英文:

I have multiple classes inheriting from a generic base class. I would like to have an arbitrary collection of instances of these child classes, and on any given child, to be able to call a method from the generic base class.

public class Pair&lt;A, B&gt;
{
    protected readonly A value1;
    protected readonly B value2;

    public Pair(A value1, B value2)
    {
        this.value1 = value1;
        this.value2 = value2;
    }

    public string ValueOneString()
    {
        return value1.ToString();
    }
}

public class AuPair : Pair&lt;string, int&gt;
{
    public AuPair(string key, int value) : base(key, value) { }
}

public class RePair : Pair&lt;bool, double&gt;
{
    public RePair(bool key, double value) : base(key, value) { }
}

public class Program
{
    public static int Main(string[] args)
    {
        var pair = new Pair&lt;int, int&gt;(76, 4);
        var auPair = new AuPair(&quot;Mart&quot;, 25);
        var rePair = new RePair(false, 2.371);

        var pairs = new List&lt;dynamic&gt; { pair, auPair, rePair };

        Console.WriteLine(pairs[0].ValueOneString());
        Console.WriteLine(pairs[1].ValueOneString());
        Console.WriteLine(pairs[2].ValueOneString());

        return 0;
    }
}

Is there a concrete collection I can store these pairs in that isn't a List&lt;dynamic&gt; ? I thought it would look like List&lt;Pair&lt;,&gt;&gt; or List&lt;Pair&gt;, but neither are correct and I cannot find a proper collection to use. I would like to call the GetKeyString() method on a concrete class rather than gamble with the dynamic keyword.

答案1

得分: 0

以下是翻译好的代码部分:

你可以创建一个抽象的基类,不使用泛型,并确保在其中定义方法:

public abstract class BasePair
{
    public abstract string ValueOneString();
}

然后,泛型类可以从它派生:

public class Pair<A, B> : BasePair
{
    ...

    public override string ValueOneString()
    {
        return value1.ToString();
    }
}

然后,你的集合可以使用 BasePair 作为泛型参数:

var pairs = new List<BasePair>();

编辑:如果基类完全是抽象的,那么它也可以是一个接口。

英文:

You can make an abstract base class that isn't generic and make sure the methods are defined on it:

public abstract class BasePair
{
    public abstract string ValueOneString();
}

then the generic can derive from it:

public class Pair&lt;A, B&gt; : BasePair
{
    ...

    public override string ValueOneString()
    {
        return value1.ToString();
    }
}

Then your collection can use BasePair for the generic parameter:

var pairs = new List&lt;BasePair&gt;();

Edit: If the base is entirely abstract, there's no reason it couldn't be an interface instead.

huangapple
  • 本文由 发表于 2023年5月25日 05:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定