具有泛型类型的接口和方法
本文关键字:方法 接口 泛型类型 | 更新日期: 2023-09-27 18:21:16
我有两个小类,Pet
和Book
,每个类都有几个属性和一个函数,其方法类似
public static List<T> GetSubSet<T>(List<T> incomingList)
{
var returnList = new List<T>();
Random r = new Random();
Console.WriteLine("Enter size of random subset: ");
int randomInt = 0;
int size = Convert.ToInt32(Console.ReadLine());
while (size > incomingList.Count)
{
Console.WriteLine("Size too large, enter smaller subset: ");
size = Convert.ToInt32(Console.ReadLine());
}
while (returnList.Count < size)
{
randomInt = r.Next(incomingList.Count);
if (!returnList.Contains(incomingList[randomInt]))
{
returnList.Add(incomingList[randomInt]);
}
}
return returnList;
}
它获取一个通用的对象列表,并返回一个较小的子集。该功能起作用,可以接受Pet或Book对象。我想在包含宠物和书籍类型的列表上使用相同的功能。我知道如何做到这一点的最好方法是使用接口(继承在这里没有意义)。
interface ISubset<T>
{
IEnumerable<T> GetSubset();
}
当我在Pet类上实现接口时,它看起来像
class Pet : ISubset<Pet>
在我的主课上,我有一份宠物和书籍的清单。我想将这两个对象都添加到ISubset
对象的列表中,这样我就可以对这两个都使用GetSubset函数。然而,我不能声明像这样的列表
List<ISubset<T>> list = new List<ISubset<T>>();
我得到错误'the type or namespace T could not be found
`
当接口接受泛型类型时,如何声明ISubset对象的列表?
好的,我有两个类似的清单
List<Pet> petList = new List<Pet>();
petList.Add(new Pet() { Name = "Mr.", Species = "Dog" });
petList.Add(new Pet() { Name = "Mrs.", Species = "Cat" });
petList.Add(new Pet() { Name = "Mayor", Species = "Sloth" });
petList.Add(new Pet() { Name = "Junior", Species = "Rabbit" });
List<Book> bookList = new List<Book>();
bookList.Add(new Book() { Author = "Me", PageCount = 100, Title = "MyBook" });
bookList.Add(new Book() { Author = "You", PageCount = 200, Title = "YourBook" });
bookList.Add(new Book() { Author = "Pat", PageCount = 300, Title = "PatsBook" });
如果我想将这些列表一起添加到另一个列表中,类型应该是什么?
您只能使用List<object>
,但如果您想按照建议的方式进行,则需要创建一个包含两个类的签名的接口
public interface IGen
{
int A;
int Method;
}
然后在你的类中继承/实现这个接口
public class Pet : IGen
{
public int A { get; set; }
private int Method(){ ... }
}
public class Book : IGen
{
public int A { get; set; }
private int Method(){ ... }
}
然后你可以像一样进入你的GetSubSet
GetSubSet<IGen>(List<IGen> incomingList) { ... }
我希望这能有所帮助。
List<object>
应该可以正常工作。
以下内容在LinqPad 中进行了测试
void Main()
{
List<Pet> petList = new List<Pet>();
petList.Add(new Pet() { Name = "Mr.", Species = "Dog" });
petList.Add(new Pet() { Name = "Mrs.", Species = "Cat" });
petList.Add(new Pet() { Name = "Mayor", Species = "Sloth" });
petList.Add(new Pet() { Name = "Junior", Species = "Rabbit" });
List<Book> bookList = new List<Book>();
bookList.Add(new Book() { Author = "Me", PageCount = 100, Title = "MyBook" });
bookList.Add(new Book() { Author = "You", PageCount = 200, Title = "YourBook" });
bookList.Add(new Book() { Author = "Pat", PageCount = 300, Title = "PatsBook" });
List<object> both = petList.OfType<object>().Union(bookList.OfType<object>()).ToList().Dump();
}
// Define other methods and classes here
public class Pet
{
public string Name { get; set; }
public string Species { get; set; }
}
public class Book
{
public string Author { get; set; }
public int PageCount { get; set; }
public string Title { get; set; }
}