Scala 特征与 Java 8 接口之间的区别和相似之处是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16410298/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:15:57  来源:igfitidea点击:

What are the differences and similarties between Scala traits vs. Java 8 interfaces?

javascala

提问by ams

I am new to Scala started learning the language for fun and I am still trying to get my head around it. My understanding of Scala traits is that they are like java interfaces except that some methods can have an implementation.

我是 Scala 的新手,开始学习这门语言是为了好玩,我仍在努力理解它。我对 Scala trait 的理解是,它们就像 java 接口,只是有些方法可以有一个实现。

Java 8 is adding interfaces that can have default methods where an implementation can be provided.

Java 8 正在添加可以具有默认方法的接口,其中可以提供实现。

What are the similarities and differences between Java 8 interfaces and Scala traits?

Java 8 接口和 Scala 特征之间有什么异同?

回答by Tvaroh

Motivations for Java 8' default methodsand Scala traitsdiffer.

Java 8 的默认方法Scala 特征的动机不同。

The former was introduced to support safe API evolution and a limited form of multiple inheritance. With leveraging functional programming idioms in Project Lambda it's been beneficial to add, for example, a forEach(lambda)method to java.util.Collectioninterface without altering all possible implementers (which is actually impossible to do without breaking backward compatibility). As a side effect this also offered a form of mixin composition.

引入前者是为了支持安全的 API 演进和有限形式的多重继承。通过利用 Project Lambda 中的函数式编程习语,例如添加一种无需更改所有可能实现者的接口forEach(lambda)方法java.util.Collection(这实际上不可能在不破坏向后兼容性的情况下进行)是有益的。作为副作用,这也提供了一种mixin 组合形式。

Scala traitswere designed from scratch as building blocks for modular components composition. They are multiple inheritance friendly and don't have diamond problemby having strict rules on evaluation order of mix-ins due to linearization. They also support state, can reference the implementing class and place restrictions on which type can mix-in them. Look at Scala collections library where traits are used thoroughly.

Scala trait是从头开始设计的,作为模块化组件组合的构建块。由于线性化,它们对混合的评估顺序有严格的规则,因此它们是多重继承友好的,并且没有菱形问题。它们还支持状态,可以引用实现类并对可以混合它们的类型进行限制。查看 Scala 集合库,其中彻底使用了特征。

回答by VonC

Note that with scala 2.12.0 RC1 (Sept. 2016), Traitnow compiles to an interface.
Scala 2.12 is all about making optimal use of Java 8's new features

请注意,使用scala 2.12.0 RC1 (Sept. 2016)Trait现在编译为接口。
Scala 2.12 旨在优化使用 Java 8 的新功能

With Java 8 allowing concrete methods in interfaces, Scala 2.12 is able to compile a trait to a single interface.

Before, a trait was represented as a class that held the method implementations and an interface.
Note that the compiler still has quite a bit of magic to perform behind the scenes, so that care must be taken if a trait is meant to be implemented in Java.
(Briefly, if a trait does any of the following its subclasses require synthetic code: defining fields, calling super, initializer statements in the body, extending a class, relying on linearization to find implementations in the right super trait.)

由于 Java 8 允许接口中的具体方法,Scala 2.12 能够将特征编译为单个接口。

之前,trait 被表示为一个类,其中包含方法实现和接口
请注意,编译器在幕后仍有相当多的魔法可以执行,因此如果要在 Java 中实现 trait,则必须小心。
(简而言之,如果 trait 执行以下任何操作,其子类需要合成代码:定义字段、调用 super、主体中的初始化语句、扩展类、依靠线性化在正确的 super trait 中找到实现。)

See scala PR 5003more the difference of implementation.

请参阅scala PR 5003,了解更多实现的差异。

回答by Ashok Waghmare

On similarity, both can have default methods,

在相似性上,两者都可以有默认方法,

Differences side, We can add traits to only instance, and not to entire class as well. Example:

差异方面,我们可以仅将特征添加到实例,而不是整个类。例子:

trait A { def m = ??? }
class B 

new B() with A 

Like wise we can keep adding trait layers on an instance.

同样,我们可以继续在实例上添加特征层。