簡體   English   中英

具有通用類型的接口和方法

[英]Interfaces and methods with generic types

我有兩個小類, PetBook每個類都有幾個屬性,以及一個帶有類似方法的函數

 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對象。 我想用同樣的功能,包含PET和圖書類型列表上。 我知道如何做到這一點的最好方法是使用接口(繼承在這里沒有意義)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM