泛型与继承(当不包含集合类时) [英] Generics vs inheritance (when no collection classes are involved)

查看:85
本文介绍了泛型与继承(当不包含集合类时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的扩展甚至可能是其他问题的重复(如果是这样,请原谅我)。我从MSDN上看泛型通常用于集合


通用
类最常见的用途是集合如
链表,哈希表,堆栈,
队列,树等等,其中
操作(例如从集合中添加和删除
的项目是
)执行的方式与
非常相似,无论类型如何的数据是
存储的。

我见过的例子也验证了上述说法。



有人可以在不涉及任何集合的真实场景中有效使用泛型吗?

迂回地,我正在考虑做一个不涉及集合的例子。

  public class Animal< T> 
{
public void Speak()
{
Console.WriteLine(我是动物,我的类型是+ typeof(T).ToString());
}

public void Eat()
{
//吃点食物
}
}

public class Dog
{
public void WhoAmI()
{
Console.WriteLine(this.GetType()。ToString());


$ b code
$ b

和<动物类型狗将是

动物&狗; magic = new Animal< Dog>();



完全可以让 Dog 动物(假设非$ 动物的非通用版本) Dog:动物因此 动物



我在想的另一个例子是 BankAccount 。它可以是 BankAccount< Checking> BankAccount< Savings> 。这很可能是 Checking:BankAccount 储蓄:BankAccount



是否有任何最佳实践来确定我们应该使用泛型还是继承?

解决方案

您可能会得到一些更好的答案,但同时考虑这一点:$ b​​ $ b泛型类意味着所有的狗都是动物,所以它们与其他所有的动物共享某些属性/品质。如果你使用继承,那么在动物类中实现这些常见的素质是很容易的,并且可以在死亡类中增加品质。但是,如果你使用 Animal(Of Dog)来实现它,那么:


  1. 狗类本身并不是一个完全合格的狗/动物,因为它的动物品质包含在 Animal(Of T)类中。狗必须与父母和子女关系中的动物绑在一起。

  2. 你的动物类不是真正的动物:你不能创建一些接受动物作为参数的方法,因为你不能引用全局化的 Animal的T)类。它实际上是一个具有共同行为的类,而不是超类。您失去了在 Animal(Of T)类之外处理动物的好处。



恕我直言,你应该想到泛型:
想一想 MedicalDoctor(Of T) class。
兽医(Of T as Animal)将继承自 MedicalDoctor(Of Animal)
DogVeterinarian 会从兽医(Of Dog)继承。



关键点:泛型类和参数类不紧密耦合并且相互依赖,它们并存。

顺便说一句,如果你想看到一些非泛型的非集合用法,请注意我们的代表:Action(Of T),Func(Of TResult),比较(T),EventHandler(Of TEventArgs),转换器(TSource,TResult)...以及接口:IEqualityComparer(Of T),IComparer(Of T)...

This is an extension of this questionand probably might even be a duplicate of some other question(If so, please forgive me). I see from MSDN that generics are usually used with collections

The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees and so on where operations such as adding and removing items from the collection are performed in much the same way regardless of the type of data being stored.

The examples I have seen also validate the above statement.

Can someone give a valid use of generics in a real-life scenario which does not involve any collections ?

Pedantically, I was thinking about making an example which does not involve collections

public class Animal<T>
{
    public void Speak()
    {
       Console.WriteLine("I am an Animal and my type is " + typeof(T).ToString());
    }

    public void Eat()
    {
        //Eat food
    }
}

public class Dog
{
    public void WhoAmI()
    {
        Console.WriteLine(this.GetType().ToString());

    }
}         

and "An Animal of type Dog" will be

Animal<Dog> magic = new Animal<Dog>();

It is entirely possible to have Dog getting inherited from Animal (Assuming a non-generic version of Animal)Dog:Animal Therefore Dog is an Animal

Another example I was thinking was a BankAccount. It can be BankAccount<Checking>,BankAccount<Savings>. This can very well be Checking:BankAccount and Savings:BankAccount.

Are there any best practices to determine if we should go with generics or with inheritance ?

解决方案

You'll probably get some better answers, but meanwhile consider this: Generic classes imply an "of" relation between the generic class and the parameter class.

All dogs are animals, so they share certain attributes/qualities with all other animals. If you use inheritance, then it is very easy to implement those common qualities in the Animal class, and add qualities at decendant classes. But if you implement it using Animal (Of Dog), then:

  1. Your Dog class isn't really a fully-qualified dog/animal by itself, as its "animal qualities" are contained in the Animal (Of T) class. The Dog must be tied with the Animal in a parent-child relationship.
  2. Your Animal class isn't really an animal: you cannot create some method which accepts Animals as an argument, because you can't refer to the globalized Animal (Of T) class. It is actually a family of classes with a common behavior, not a superclass. You lose the benefit of dealing with Animals outside the Animal (Of T) class.

But here is how, IMHO, you should think of generics: Think about a MedicalDoctor(Of T) class. Veterinarian(Of T as Animal) would be inheriting from MedicalDoctor(Of Animal). DogVeterinarian would inherit from Veterinarian(Of Dog).

Key point: the generic class and the parameter class are not tightly-coupled and co-dependant, they co-exist.

BTW, if you want to see some good non-collection usage of generics, just notice the delegates we have: Action(Of T), Func(Of TResult), Comparison(Of T), EventHandler(Of TEventArgs), Converter(Of TSource, TResult)... and the interfaces: IEqualityComparer(Of T), IComparer(Of T)...

这篇关于泛型与继承(当不包含集合类时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆