为什么界面方法没有正文 [英] Why interface methods have no body

查看:161
本文介绍了为什么界面方法没有正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要实现多重继承,我们必须使用接口,但为什么接口方法没有实体,为什么必须在派生类中重写它们?

To achieve multiple inheritance, we must use interfaces, but why don't interface methods have bodies and why do they have to be overridden in the derived class?

我真的想要一个清醒的答案,不涉及太多的计算机术语,我似乎无法理解这一点,我已经提到了各种参考资料

I really want a lucid answer , not involving too much computer jargon , i cant seem to understand this , i have referred various references

推荐答案

因为与C ++或Eiffel等语言相比,Java只有类型的多重继承(即接口和一个类),而不是 state 和<的多重继承EM>行为。后者增加了巨大的复杂性(特别是状态)。

Because Java, in contrast to languages like C++ or Eiffel, only has multiple inheritance of types (i.e. interfaces as well as one class), not multiple inheritance of state and behaviour. The latter of which add enormous complexity (especially state).

Java设计者(和C#,就此而言)选择不包括它,因为它经常提供给C ++程序员很难调试问题。您可以通过实现多个接口来解决几乎所有需要真正多重继承的问题,因此权衡被认为是值得的。

The Java designers (and C#, for that matter) opted to not include it as it presented C++ programmers often with very hard to debug issues. You can solve pretty much most problems that require true multiple inheritance with implementing multiple interfaces, so the tradeoff was deemed worth it.

请注意行为的多重继承(not state)可能以虚拟扩展方法的形式来到Java 8(除非它们像其他许多东西一样推迟它),其中接口可以声明一个委托给一个的方法在另一个类中,然后存在于实现该接口的所有类型上。

Note that multiple inheritance of behaviour (not state) might come to Java 8 (unless they postpone it again like one of the many other things) in form of virtual extension methods where an interface can declare a method that delegates to one in another class, which then exists on all types that implement that interface.

这篇关于为什么界面方法没有正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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