从接口而不是具体类初始化对象 [英] Initalizing an object from an Interface instead of concrete class

查看:331
本文介绍了从接口而不是具体类初始化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在Unity框架的示例中注意到了此代码.

Hi all,

I noticed this code in an example on the Unity framework.

IUnityContainer container = new UnityContainer();



我以前从未见过对接口的引用.为什么这样做而不是执行以下操作?



I have never seen a reference to an interface before. Why is this done instead of having the following?

UnitContainer container = new UnitContainer();



我将对为什么使用这样的界面有一些见解.这里有什么优势?谢谢大家.



I would appreciate some insight on why an interface is used like this. What are the advantages here? Thanks guys.

推荐答案

您所看到的是界面的核心和OOP的核心(其真正的特征是 late绑定使用虚拟).理解这些东西是理解OOP的最基本条件的先决条件之一.

有一分钟,忘记了IUnityContainer是接口,让我们仅谈谈一些允许继承的类型.您有什么不寻常的地方吗?
左侧声明IUnityContainer中的类型是编译时类型;右侧的构造函数类型定义运行时类型类.您知道为什么我们需要不同的编译时运行时类吗?听说过抽象吗?隐藏过多的细节?即使我们忽略了其中一种类型是interface的事实,我们也可以将编译时类型视为对一组不同可能具体类的接口的抽象.当使用变量container时,我们不需要使不同的具体类有所不同的任何东西,我们希望使用抽象接口.类型UnityContainer从类型IUnityContainer派生;它使变量container 可分配为"="右边的值.

我只是在谈论上面的类型,而没有提到类或接口.我们只需要考虑继承性.此外,在类中,虚拟方法(和属性)和 late绑定支持对具体类的行为进行修改.

现在,将 interface 的概念添加到图片中.在我们的上下文中,请考虑将interface用作编译时类型的附加约束.基类可以是非抽象的,可以实例化,但是接口始终是抽象的.一个基类可以有数据,但是接口不能.想法是,仅在构建时定义具体的实现,并且所有对象的使用都只能通过接口引用来完成.

接口可以是与多态性相关的另一种独特功能.虚拟方法的机制仅适用于引用类型的类.值类型可能有多态性吗?是的,但是许多开发人员对此并不十分了解.问题是:接口甚至可以通过结构(struct)来实现.因此,对于抽象类型使用接口的多态性可以发展得如此之遥,以至于它不仅与具体的实现类型无关,甚至与关于什么类型是引用类型以及什么是值类型的知识都不可知.
同样,接口可以多重继承,而类则不能.更确切地说,一个接口可以具有无限数量的基本接口;并且一个类在继承列表中只能有一个基类,而基类接口的数量却是无限的.这称为多重继承的弱形式.

我要说的是,如果您对我上面提到的内容有任何理解上的问题,那么您将需要从一开始就开始学习OOP.无论如何,它非常有用;如果做得好,时间和精力将很快得到回报.

-SA
What you see is the very heart of interfaces and almost the very heart of OOP (the real hart of which is late binding using virtual). Understanding such things is one of prerequisites to understanding of the very basics of OOP.

For a minute, forget that IUnityContainer is interface, let''s just talk in terms of some types which allow inheritance. Is there anything unusual to you?
The type in declaration IUnityContainer on left is a compile-time type; the type of constructor on right defines run-time type class. Do you know why we need different compile-time and run-time classes? Ever heard of abstraction? Hiding of excessive detail? Even though we ignore the fact one of those types is interface, we can consider a compile-time type as an abstract interface to a set of different possible concrete classes. When as work with the variable container, we don''t need anything that makes different concrete classes different, we want to work with abstract interface. The type UnityContainer is derived from the type IUnityContainer; it makes the variable container assignable to the value on the right of "=".

I was talking only about types above, not mentioning classes or interfaces. We only needed to idea of inheritance. Additionally, in classes, the modification of behavior of the concrete classes is supported by virtual methods (and properties) and late binding.

Now, adding the notion of interface to the picture. In our context, consider using interface as an additional constraint to our compile-time type. A base class could be non-abstract and could be instantiated, but interface is always abstract. A base class could have data, but interface cannot. The idea is that concrete implementation is only defined at the moment of construction, and all the usage of objects is done via interface references only.

Interfaces can one more distinct feature related to polymorphism. The mechanism of virtual methods is only applicable to classes, which are reference type. Is the polymorphism possible with value types? Yes, but this fact is not very well known by many developers. The thing is: interfaces could be implemented even by structures (struct). So, the polymorphism using interfaces for abstract types can go so far that it can be agnostic not only about concrete implementing types, but even about the knowledge what types are the reference types and what are the value types.

Also, multiple inheritance is possible with interfaces but not classes. More exactly, an interface can have unlimited number of base interfaces; and a class can have only one base class but unlimited number of base interfaces in inheritance list. This is called weak form of multiple inheritance.

I would say, it you have problem understanding of anything from what I mentioned above, you will need to start learning OOP from the very beginning. It''s very useful anyway; if you do it well, the time and effort will pay off pretty soon.

—SA


IUnityContainer container = new UnityContainer();

我将代码读取为:创建UnityContainer对象的新实例:该对象实现IUnityContainer.根据定义,您不能创建接口的实例.因此,在这种情况下,您不是从接口初始化对象".

I read this code as: create a new instance of the UnityContainer object: that object implements IUnityContainer. By definition, you cannot create an instance of an interface. So, in this case, you are not "initializing an object from an interface."

UnitContainer container = new UnitContainer();

要了解此调用与第一次调用有何不同,我需要知道:UnityContainer和UnitContainer之间有什么区别?我推测UnitContainer没有实现IUnityContainer.

To understand how this invocation differs from the first, I''d need to know: what are the differences between UnityContainer and UnitContainer ? I''d speculate that UnitContainer does not implement IUnityContainer.


这篇关于从接口而不是具体类初始化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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