C# 当我们创建接口对象时会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17290336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happens when we create an object of interface?
提问by B-Abbasi
I am new to interfaces in C#. So can somebody please explain what actually happens when we create an object of interface?
我是 C# 接口的新手。那么有人可以解释一下当我们创建一个接口对象时实际发生了什么吗?
I know why we have interfaces in other languages but can't really grasp the logic of Why C# allows creation of an object(instance) of interface? If interfaces do not have function definitions or variables then how can an object be created?
我知道为什么我们有其他语言的接口,但无法真正掌握为什么 C# 允许创建接口的对象(实例)的逻辑?如果接口没有函数定义或变量,那么如何创建对象?
I have been searching on forums but couldn't get the point. here is a tutorial i found on interfaces http://www.c-sharpcorner.com/uploadfile/6897bc/interfaces-in-C-Sharp/if you visit the link you can see that first the writer makes object of a class and than an object of inteface. When he writes
我一直在论坛上搜索,但找不到重点。这是我在接口http://www.c-sharpcorner.com/uploadfile/6897bc/interfaces-in-C-Sharp/上找到的教程, 如果您访问该链接,您可以看到作者首先创建了一个类的对象,然后而不是一个界面对象。当他写
Interface_object = class_object;
interface object extracts the features from class object...
How and why does it happen if there is no implementation and variable in the interface ?
接口对象从类对象中提取特征......
如果接口中没有实现和变量,它如何以及为什么会发生?
采纳答案by horgh
Actually you cannot create an instance of an interface.
实际上,您无法创建接口的实例。
You create an instance of some class, implementing the interface. Actually there can be dozens of classes, implementing one interface. So when you use a variable of interface type, the only thing you are guaranteed is that the object, which is in fact referenced by the variable, implements the interface and you can use any of the interface methods, properties, etc.
您创建某个类的实例,实现该接口。实际上可以有几十个类,实现一个接口。因此,当您使用接口类型的变量时,唯一可以保证的是实际上由变量引用的对象实现了接口,并且您可以使用任何接口方法、属性等。
interface IFoo
{
void DoFoo();
}
class Foo1: IFoo
{
public DoFoo()
{
//one implementation
}
}
class Foo2: IFoo
{
public DoFoo()
{
//the other implementation
}
}
IFoo tmp = new Foo1();
tmp = new Foo2();
You may see a deep explanation in SO: Using Interface variables
您可能会在SO: Using Interface variables 中看到深入的解释
回答by Azad Chouhan
You can't create an object of an interface, but you can create an object of a class and let the class use the interface.
不能创建接口的对象,但可以创建类的对象,让类使用接口。
回答by Andre
You cannot instantiate interfaces. But you can create a new reference of the interface, if the class is implementing the interface.
您不能实例化接口。但是,如果该类正在实现该接口,则可以创建该接口的新引用。
This just creates a new reference of the object MyNokiaOldPhoneINokiaOld iface = phone;which implements the interface INokiaOld.
这只是创建了一个MyNokiaOldPhoneINokiaOld iface = phone;实现接口的对象的新引用INokiaOld。
回答by Tigran
You don't createan instance of interface, but assign your derived object instance reference to it, so it wrapped up into that interface type. This is a polymorphysm.
您不创建接口的实例,而是将您的派生对象实例引用分配给它,因此它包含在该接口类型中。这是一种多态性。
回答by Mogli
As far as i know You cannot create an instance of an interface and even if we do so it would be of no use as none of the members in that class are implemented. Same is the case with the abstract class. This is because they are incomplete (i.e., they act as templates) and creation of an object is not meaningful for incomplete classes.
据我所知,您不能创建接口的实例,即使我们这样做也没有用,因为该类中的任何成员都没有实现。抽象类也是如此。这是因为它们是不完整的(即,它们充当模板)并且对象的创建对于不完整的类没有意义。
回答by slugster
can somebody please explain what actually happens when we create an object of interface?
有人可以解释一下当我们创建一个接口对象时实际发生了什么吗?
An analogy I like to use is that an interface is like a mask - when you create an instance of a class that implements an interface (e.g. IFoo), then treat the class as the interface then the interface is like a mask that an actor would wear - the class appears to be and acts as the interface (mask) even though there is a whole bunch of stuff under the mask (interface).
我喜欢使用的一个类比是接口就像一个掩码——当你创建一个实现接口的类的实例(例如IFoo),然后将该类视为接口,那么接口就像一个掩码,演员会磨损 - 即使面具(界面)下有一大堆东西,该类似乎也是并充当界面(面具)。
but can't really grasp the logic of Why C# allows creation of an object(instance) of interface?
但不能真正掌握为什么 C# 允许创建接口的对象(实例)的逻辑?
The other thing to mention is that an interface is a contract - whatever methods / properties / etc are defined on the interface are guaranteed to be implemented on the class. Classes that are completely and utterly different to each other can implement the same interface - when you have an instance of each class and treat it (cast it) as the interface then the same methods can be called on each class, although the implementation (actual code) in each class can be wildly different.
另一件事要提到的是,接口是一种契约——在接口上定义的任何方法/属性/等都保证在类上实现。彼此完全不同的类可以实现相同的接口 - 当您拥有每个类的实例并将其(强制转换)视为接口时,可以在每个类上调用相同的方法,尽管实现(实际代码)在每个类中都可能大不相同。
Example:
The classes Carand Humanboth implement the interface IMove, and IMovedefines the method void Move();. They are both totally unrelated classes with no common ancestry, but if you have an instance of Car and Human, then they are both guaranteed to have the method void Move(), although the code in that method will differ between then them as they move in totally different ways. If you then cast these different objects to the same interface (var x = (IMove) someCaror var x = someHuman as IMove) then you can treat the object reference identically.
示例:
类Car和Human都实现了接口IMove,并IMove定义了方法void Move();。它们都是完全不相关的类,没有共同的祖先,但是如果你有一个 Car 和 Human 的实例,那么它们都保证有方法void Move(),尽管该方法中的代码在它们之间会有所不同,因为它们以完全不同的方式移动. 如果您随后将这些不同的对象转换为相同的接口(var x = (IMove) someCar或var x = someHuman as IMove),则您可以将对象引用视为相同。
Interfaces and abstract classes have some similarities - they both specify things in a contractual way (the extender or implementor has to implement specific things), and they both cannot be created directly (the difference is that abstract classes can provide some implementation whereas interfaces cannot, and interfaces can only be implemented, never inherited).
接口和抽象类有一些相似之处——它们都以契约方式指定事物(扩展者或实现者必须实现特定事物),并且它们都不能直接创建(区别在于抽象类可以提供一些实现而接口不能,和接口只能实现,不能继承)。
If you are wondering what happens under the hood at MSIL level then there are other far more qualified people around here who can elaborate on that.
如果您想知道 MSIL 级别的幕后会发生什么,那么这里还有其他更有资格的人可以详细说明。
回答by Jay Patel
Interface is that, just an interface. You can not instantiate an interface. You can use it as a variable which points to a class which implements that interface. Interface is a public collection of methods/properties with a guarantee that all of its methods are implemented. Abstract classes are similar to interfaces but it does not provide such guarantee.
接口就是这样,只是一个接口。你不能实例化一个接口。您可以将它用作指向实现该接口的类的变量。接口是方法/属性的公共集合,并保证其所有方法都已实现。抽象类类似于接口,但它不提供这样的保证。
In OOP, this is a kind of polymorphism and there are other types of polymorphisms.
在 OOP 中,这是一种多态性,还有其他类型的多态性。

