Java 9接口与类 [英] Java 9 Interface vs Class

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

问题描述

由于Java 9将允许我们在接口中定义 private private static 方法,是界面的剩余差异?
而且,Java是否正在慢慢走向多重继承?

As Java 9 is going to allow us to define private and private static methods too in interfaces, what would be the remaining difference in interface and class? Moreover, is Java moving towards multiple inheritance slowly?

推荐答案

Java 9中的私有接口方法与其他私有方法完全相同:它们必须具有正文(即使在抽象类中)既不能被子类调用也不能被子类覆盖。因此,它们并不真正与继承相互作用。说到哪个(特别是多重继承),有(至少?)三种类型:

Private interface methods in Java 9 behave exactly like other private methods: They must have a body (even in abstract classes) and can neither be called nor overridden by subclasses. As such they do not really interact with inheritance. Talking of which (and particularly multiple inheritance), there are (at least?) three kinds of it:


  • 类型的继承表示一种类型可以 另一种类型,例如字符串 对象。 Java允许从第一天开始多次继承类型(通过接口)。

  • 行为的继承意味着一种类型可以继承另一种类型的行为。在Java 8之前,只有类可以实现方法,所以只有这种类型的单继承。 Java 8提供了默认方法,它允许接口实现方法,从而为Java提供多重行为继承。

  • 继承状态意味着类型继承了另一种类型内部状态(即字段)。就目前而言(Java 9和目前为未来Java版本提出的所有内容),只有类可以有状态,所以只有这种类型的单继承。

  • Inheritance of types means that one type can be another type, e.g. String is an Object. Java allowed multiple inheritance of types from day one (via interfaces).
  • Inheritance of behavior means that one type can inherit the behavior of another type. Before Java 8, only classes could implement methods, so there was only single inheritance of this kind. With Java 8 came default methods, which allowed interfaces to implement methods, thus giving Java multiple inheritance of behavior.
  • Inheritance of state means that a type inherits another type's internal state (i.e. fields). As it stands (Java 9 and everything currently proposed for future Java versions), only classes can have state, so there is only single inheritance of this kind.

正如你所看到的,私有接口方法不会在这里添加任何内容。

As you can see private interface methods do not add anything here.

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

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