0
是否有可能有一個PrintList()
方法覆蓋Kid
,Student
和Person
類?任何類從一般基類派生的方法
我不希望實現PrintList()
作爲一個類的方法到Person
或Kid
,Student
和Person
。
public class Person<T>
{
public T Id { get; set; }
public string Name { get; set; }
}
public class Kid : Person<byte> { }
public class Student : Person<short> { }
public class Adult : Person<int> { }
/* ... more classes ... */
class Program
{
static void PrintList(List<T> list) where T : Person /* NOT POSSIBLE */
{
foreach (var item in list)
Console.WriteLine("Id: {0}, Name: {1}", item.Id, item.Name);
}
static void Main(string[] args)
{
var students = new List<Student>();
students.Add(new Student() { Id = 1, Name = "SA" });
students.Add(new Student() { Id = 2, Name = "SB" });
var adults = new List<Adult>();
adults.Add(new Adult() { Id = 1, Name = "AA" });
adults.Add(new Adult() { Id = 2, Name = "AB" });
adults.Add(new Adult() { Id = 3, Name = "AC" });
PrintList(students);
PrintList(adults);
Console.ReadLine();
}
}