泛型类型与抽象类/接口
假设我们正在 .NET 中创建一个通用控件。 例如一棵树。 我不明白为什么人们
Control<T>
在面向对象编程中使用这个泛型类型定义时我可以使用抽象类或接口:
Control<IItem> or Control<BaseClass>
所以唯一要做的就是,他们的类型必须从该基类派生或实现 界面。 这是否意味着,泛型类型更方便,因为你不需要实现或继承任何东西?
Suppose we are creating a generic control in .NET. E.g. a tree.
I don't understand why people use this generic type definition
Control<T>
when in Object Oriented Programming I can use an abstract class or an interface:
Control<IItem> or Control<BaseClass>
So the only thing to do is that, their types must derive from that base class or implement the
interface.
Does it mean, that generic types are more convenient, because you I don't have to implement or inherit anything?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我觉得你有点困惑。 通用类型和抽象类/接口为应用程序设计中的不同方法提供不同的目标。 抽象类/接口用于泛化一组实体的通用功能。 稍后这个 API 可以以不同的方式实现,但由于涉及多态性,因此不会影响任何人。
另一方面,有时您的某些实现非常相似,唯一的区别是您正在使用的对象的类型。 在这里你需要一个通用的。 您可以使用多态性,但没有必要。 为此,只需定义一个接口、进行实现并让最终用户决定使用哪种类型的对象就更清楚了。
最好的例子是List,其中list的主要用途是存储元素。 List 实现者不应该关心您要使用哪种类型的对象,因此稍后您将能够只定义 List 并使用整数列表。
I think you a little bit confused. Generic types and abstract classes/interfaces serve different goals for different approaches in application design. Abstract classes/interfaces are for generalization of common functionality of a group of entities. Later on this API could be implemented differently, but since polymorphism is involved it will not impact anyone.
On other hand, sometimes you have a very similar implementation of something and the only difference is the type of the objects you are working with. Here you will need a generic. You could use polymorphism, but there is no need. For that purpose it's much clearer just to define an interface, make an implementation, and let the end user decide which kind of object to use.
The best example is List, where the main purpose of list is to store elements. The List implementor should not care which type of objects you are going to use, so later you will be able just define List and make use of integer list.
因为在树控件的情况下,定义通用树控件意味着树项可以是任何类型(您也可以添加某些约束)。
实例化控件时,您当然必须声明项目类型(就像在第二个代码示例中使用
IItem
和BaseClass
一样)。如果您的树控件不是通用类型,则必须为每个项目类型创建多个控件。
为什么不只使用interface/abstractBase类型?
如果您只使用接口/抽象作为您的 Item 具体类,您将受到它的定义的限制。 您只会看到它的属性和方法。 使用通用树控件和任何项目类型,您仍然可以访问所有项目的属性和方法,无论其接口实现或父类继承如何...
Because in your case of a tree control, defining a generic Tree control would mean that Tree items can be of any type (you can also add certain constrains).
When instantiating a control, you would of course have to declare your item type (like in your second code examples with
IItem
andBaseClass
).If your Tree control wouldn't be a generic type, you would have to create several controls for each item type.
Why not just use interface/abstractBase type?
If you would just use an interface/abstract as your Item concrete class, you would be constrained by it's definition. You'd only see it's properties and methods. With generic Tree control and whatever item type, you're still able to access all item's properties and methods regardless of its interface implementation or parent class inheritance...