接口和多态 [英] Interfaces and Polymorphism

查看:94
本文介绍了接口和多态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;
我对接口有一个疑问.

大家都知道多态是
不久
从派生类创建对象作为基类,该类将在运行时通过后期绑定执行.

但是我不明白接口如何像基类一样工作?
我问接口是一类吗?或接口引用如何设置其他实现类的对象引用接口.我用Reflector看了MSIL代码,但找不到任何我想要的东西.

有人想到这个.
每个人都行得通

解决方案

alikaras写道:

从派生对象创建对象作为基类您可能会很容易注意到,这种机制(多态性)只需要基类的方法签名 (无需其他操作即可) ).因此,接口(合约",因为它们只是提供了此类方法签名)对于替代多态机制中的基类是完全有效的.


您对多态的定义是错的.您只是尝试使用术语后期绑定"来解释什么是后期绑定",这没有任何意义.

多态性在且仅当您具有一组对象 时才会发挥作用;每个对象都由某种父类编译时类型表示,该类型可以是抽象的或伪抽象的.集合对象的运行时类型不同.如果公共基本类型是抽象的,那么显然它们永远不会属于这种类型.

现在,表示集合中所有对象的基类可以是类或接口.从历史上看,接口出现的时间晚于抽象类,以表示纯实体.最初,接口是基于纯抽象"类(即除抽象方法之外没有成员的类)建模的.后来,人们以句法的方式将此类从接口中分离出来.此外,接口可以使用多重继承,而使用接口的类可以使用弱形式的多重继承,即,只有一个类可以作为基础,但接口数目也可以不受限制.通过其机制,接口的功能与上述纯抽象类"的功能非常相似.

您不应将接口视为运行时不存在的接口.接口是对虚拟消息表某些部分的引用.想一想如果您有一个类并将其转换为两个不同的(由于多重继承)接口会发生什么情况.您获得了两个物理上不同的引用,因为一个接口不知道另一个.考虑一下.

现在,具有接口的多态性比具有基类的多态性更强大.首先,在具有多重继承的情况下,同一对象可以参与不止一种不同基础类型(在这种情况下为接口)的多态集合.而且,您不仅可以在类中而且可以在结构中实现接口.但是结构是一个值类型,甚至不能有虚方法!你能看到这有多深吗?您不仅可以对具体的运行时类型不了解,还可以从集合的成员是哪种类型的知识中抽象出来:引用类型(一个类,可以使用虚拟方法)或值类型(结构,没有虚拟方法机制).

—SA


接口不是类.您可以将接口视为指定类行为的协定.


Hi;
I have got a one question about interfaces.

Everyone knows that polymorphism is
shortly
creating object as base class from deriveted class which will be execute at runtime with late binding.

But I dont understand How do Interfaces do it like a base class ?
I asked is interface is a class ? or Interfaces references how can set other object references which class implement interface. I look the MSIL code with Reflector but I cant find anything which I m looking for.

Have someone with the idea of this.
Good works everyone

解决方案

alikaras wrote:

creating object as base class from deriveted class which will be execute at runtime with late binding

As you may easily note, this mechanism (polymorphism) only needs method signatures of the base classes (not else). Hence interfaces (''contracts'' because they just provide such methods signatures) are perfectly valid for substituting base classes in the polymorphism mechanism.


Your definition of polymorphism is wrong. You just tried to explain what is "late binding" using the term "late binding''. It makes no sense.

The polymorphism comes into play when and only when you have a set of objects; and a each object is represented by some parent compile-time type which can be abstract or pseudo-abstract. The run-time types of the objects of the set are different. If the common base type is abstract, they are never of this type, apparently.

Now, this base class representing all object in the set can either be a class or interface. Historically, interfaces appeared later then abstract classes to represent pure entities. Originally, interfaces were modeled based on the "pure abstract" class, that is, the class which has no members except abstract methods. Later on, people separated such classes from interfaces in syntactic way. Also, interfaces can use multiple inheritance, and the class using interfaces can use the weak form of multiple inheritance, that is, only one class can be a base, but also unlimited number of interfaces. By its mechanism, the functioning of interfaces is very similar to that of "pure abstract classes" described above.

You should not think of interfaces as something not existing during run-time. An interface is a reference to some part of virtual message table. Think what happens if you have a class and cast it in two different (due to multiple inheritance) interfaces. You obtain two physically different references, because one interface is unaware of another one. Think about it.

Now, polymorphism with interfaces is more powerful then that with base classes. First, with multiple inheritance, the same object can participate in more than one polymorphous set of different base types (interfaces, in such cases). What''s more, you can implement an interface not only in class, but also in a structure. But structure is a value type and cannot even have virtual methods! Can you see how deep this is? You can work in a way agnostic not just to a concrete run-time type, but also be abstracted from the knowledge of what kind of type is the member of a set: a reference type (a class, which can use virtual method) or a value type (a structure, no virtual method mechanism).

—SA


An interface is not a class. You can think of an interface as a contract that specifies the behavior of a class.


这篇关于接口和多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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