在接口中添加默认和静态方法的原因 [英] Reason for adding default and static methods in interfaces

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

问题描述

Java 8在接口上引入了默认和静态方法。所以现在你可以在你的界面中使用默认或静态方法的具体实现。

Java 8 introduced default and static methods on interfaces. So now you can have concrete implementations in your interface whether using default or static methods.

Java声称添加这两种新方法的原因是确保二进制兼容性为这些接口的旧版本编写的代码。

The reason Java claimed to add these two new kind of methods is "ensure binary compatibility with code written for older versions of those interfaces".

我的问题:


  • 为什么要扭曲界面原始概念,假设为
    完全抽象以支持现有的体系结构问题?

  • 使用抽象类和新版本之间有什么区别?除了扩展多个接口的能力之外的接口?

推荐答案


java声称添加这两种新方法的原因是确保与为这些接口的旧版本编写的代码的二进制兼容性。

The reason java claimed to add these 2 new kind of methods is "ensure binary compatibility with code written for older versions of those interfaces".

这仅适用于默认方法(不是静态方法),省略了一些上下文。来自 Goetz,Lambda状态

This applies only to default methods (not static methods) and omits some context. From Goetz, State of the Lambda:


默认方法的目的是使接口在首次发布后以兼容的方式发展。

The purpose of default methods ... is to enable interfaces to be evolved in a compatible manner after their initial publication.

主要目标是允许接口演化,即添加新方法。如果将新方法添加到接口,则实现该接口的现有类将缺少实现,这将是不兼容的。为了兼容,实现必须来自某个地方,因此它是由默认方法提供的。

The main goal is to allow interface evolution, that is, the addition of new methods. If a new method is added to an interface, existing classes that implement the interface would be missing an implementation, which would be incompatible. To be compatible, an implementation has to come from somewhere, so it is provided by default methods.


为什么要扭曲界面原始概念假设是完全抽象的,以支持现有的架构问题?

Why to distort the interface original concept that suppose to be fully abstract in order to support existing architectural problems?

Java接口的主要目的是指定合同,任何类都可以实现,而无需改变其在课堂中的位置层次结构。确实,在Java 8之前,接口纯粹是抽象的。但是,这不是接口的基本属性。即使包含默认方法,其核心界面仍然在实现类上指定合同。实现类可以覆盖默认方法,因此该类仍然可以完全控制其实现。 (另请注意,默认方法不能最终。)

The main intent of a Java interface is to specify a contract that any class can implement without having to alter its position in the class hierarchy. It's true that, prior to Java 8, interfaces were purely abstract. However, this is not an essential property of interfaces. Even when default methods are included, an interface at its heart still specifies a contract upon the implementing class. The implementing class can override default methods, so the class is still in complete control of its implementation. (Note also that default methods cannot be final.)


使用抽象类和接口的新版本之间的区别除了能力之外扩展多个接口的类?

What is the difference between using an abstract class and the new version of the interface other than the ability of a class to extend multiple interfaces?

类扩展多个接口的能力与接口和抽象类之间的另一个区别密切相关,即接口不能包含状态。这是允许多重继承的主要困难:如果一个超类在一个类的祖先中多次出现,那么这个超类的状态会出现一次还是几次? (这就是所谓的钻石问题。)

The ability of a class to extend multiple interfaces is closely related to another difference between interfaces and abstract classes, namely that interfaces cannot contain state. This is the primary difficulty with allowing multiple inheritance: if a superclass were to appear multiple times in the ancestry of a class, would that superclass' state appear just once or several times? (This is the so-called "diamond problem.")

另一个区别是抽象类可以定义要与子类共享的方法和字段,但不能与调用者共享,通过使用受保护和包私有访问级别。接口只能有公共方法。

Another difference is that abstract classes can define methods and fields to be shared with subclasses, but not with callers, by using protected and package-private access levels. Interfaces can have only public methods.

(在Java 9中,添加了对私有方法的支持。这对于接口的默认或静态方法之间的实现共享很有用。 )

(In Java 9, support for private methods has been added. This is useful for implementation sharing among default or static methods of an interface.)

最后,接口中的静态方法不会影响类继承,也不会成为接口契约的一部分。它们仅仅是以更方便的方式组织实用方法的一种方式。例如,接口中静态方法的常见用法是静态工厂方法。如果接口中不允许使用静态方法,则必须将静态工厂方法放在伴随类上。允许接口中的静态方法允许这样的方法与接口本身分组,这样做是合适的。

Finally, static methods in interfaces don't affect class inheritance, nor are they part of the interface's contract. They are merely a way of organizing utility methods in more convenient fashion. For example, a common use of static methods in an interface is for static factory methods. If static methods weren't allowed in interfaces, static factory methods would have to be put on a companion class. Allowing static methods in interfaces lets such methods be grouped with the interface itself, when doing so is appropriate.

这篇关于在接口中添加默认和静态方法的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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