2015-04-07 99 views
1

我对通用继承方法有疑问。通用继承方法

说了一个例子,我有有一个基管理类ListManager:

public class ListManager<T> 
{ 
    private List<T> m_list; 

    public ListManager() 
    { 
     m_list = new List<T>(); 
    } 

    public int Count 
    { 
     get { } 
    } 

    public void Add(T aType) 
    { 
    } 

    public bool DeleteAt(int anIndex) 
    { 
    } 

    public string[] ToStringArray() 
    { 
    } 

    public List<string> ToStringList() 
    { 
    } 

    public bool CheckIndex(int index) 
    { 
    } 
} 

然后一个动物管理器类继承上面的列表管理器类:

public class AnimalManager : ListManager<Animal> 
{ 
    private List<Animal> AnimalList; //list 

    public AnimalManager() 
    { 
     AnimalList = new List<Animal>(); 
    } 

    public void Add(Animal animal) // Exactly the same as base class method but with different method "content". 
    { 
     animal.Id = ++animalCounter; //increases the id by one. 
     AnimalList.Add(animal); 
    } 

    public bool IsIndexValid(int index) 
    { 
     return ((index >= 0) && (index < AnimalList.Count)); 
    } 

    public Animal GetElementAtPosition(int index) 
    { 
     if (IsIndexValid(index)) 
      return AnimalList[index]; 
     else 
      return null; 
    } 

    public int ElementCount 
    { 
     get { return AnimalList.Count; } 
    } 

    public bool DeleteAt(int anIndex) 
    { 
     return true; 
    } 
} 

我该怎么做继承ListManager的方法?它需要继承的方法,但在方法内部具有不同的“内容”。我知道只要一个类继承了另一个类,它们就继承了它们的基类,但是如果每个管理类都处理不同的列表框,但是具有完全相同的方法头,我该怎么办。

例如,每一个管理器类有Add

public void Add(T aType) 
{ 
} 

但我的动物管理员需要添加到动物名单:

public void Add(Animal animal) 
{ 
    AnimalList.Add(animal); 
} 

我的从业人员管理者需要添加到EmployeeList的:

public void Add(Employee employee) 
{ 
    Employeelist.Add(employee); 
} 

他们都有刚好是相同的方法,但具有不同的“内容”。

+0

你的标题很差。 _about_“泛型继承方法”是什么? –

回答

2

你可以使基类的虚拟方法,然后在派生类中重写它们:

public class ListManager<T> 
{ 
    private List<T> m_list; 

    public ListManager() 
    { 
     m_list = new List<T>(); 
    } 

    public virtual void Add(T aType) 
    { 
    } 
} 

public class AnimalManager : ListManager<Animal> 
{ 
    private List<Animal> AnimalList; //list 
    private int animalCounter; //variable 

    public AnimalManager() 
    { 
     AnimalList = new List<Animal>(); 
    } 

    public override void Add(Animal animal) 
    { 
     animal.Id = ++animalCounter; //increases the id by one. 
     AnimalList.Add(animal); 
    } 
} 

public class Animal 
{ 
    public int Id { get; set; } 
} 

你可能只想扩大现有List<T>,如果你的基类是没有做什么太特别的:

public class AnimalManager : List<Animal> 
{ 
    private int animalCounter; 

    public new void Add(Animal animal) 
    { 
     animal.Id = ++animalCounter; 

     base.Add(animal); 
    } 
} 
+0

非常感谢! –

1

为什么你到底有两个列表?使用基类的通用m_list,使其受到保护,而且不必重新实现任何内容。在派生类中(假设他们除了仅仅实现一个特定的通用专业化之外还有其他的工作),使用m_list而不是他们当前的AnimalList等等。

+0

像这样在基类中:'List m_list = new List ();'? –

+0

@ Kevin.A:是的。将'private'访问修饰符切换到'protected'。 –

+0

好的。非常感谢你。 –

1

你完成时,不正是你想要的一流:

public class ListManager<T> 
{ 
    private List<T> m_list; 

    public ListManager() 
    { 
     m_list = new List<T>(); 
    } 

    public int Count 
    { 
     get {return m_list.Count; } 
    } 

    public void Add(T aType) 
    { 
     m_list.add (aType); 
    } 

    public bool DeleteAt(int anIndex) 
    { 
     m_list.DeleteAt (anIndex); 
    } 

    public string[] ToStringArray() 
    { 

    } 

    public List<string> ToStringList() 
    { 

    } 

    public bool CheckIndex(int index) 
    { 
    } 

    } 
} 

现在你可以使用这个类,如下所示:

ListManager <Person> plm = new ListManager <Person>(); 
plm.add (new Person()); 
plm.Count(); 
// and so on... 

动物类同样的事情。

事实上,如果你不需要任何已经在列表中的功能,那么List<Animal>List<Person>会做。

+0

非常感谢 –