英文:
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<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;
}
}
Is there a concrete collection I can store these pairs in that isn't a List<dynamic>
? I thought it would look like List<Pair<,>>
or List<Pair>
, 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<A, B> : BasePair
{
...
public override string ValueOneString()
{
return value1.ToString();
}
}
Then your collection can use BasePair
for the generic parameter:
var pairs = new List<BasePair>();
Edit: If the base is entirely abstract, there's no reason it couldn't be an interface instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论