Scala 类可以扩展多个类吗?

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

Can a Scala class extend multiple classes?

scala

提问by Maksim

Is it possible to extend multiple classes in Scala.

是否可以在 Scala 中扩展多个类。

For example if I have ClassA and ClassB then can ClassC extend ClassA and ClassB (like in C++).

例如,如果我有 ClassA 和 ClassB,那么 ClassC 可以扩展 ClassA 和 ClassB(就像在 C++ 中一样)。

采纳答案by loki

No, ClassCjust can extend one of those, but you can mixin multiple traits.

不,ClassC只能扩展其中之一,但您可以混合多个特征。

回答by Tal Pressman

You can't extend multiple classes, but you can extend several traits. Unlike Java interfaces, traits can also include implementation (method definitions, data members, etc.). There is still a difference in that you can't instantiate a trait directly (similar to abstract classes in a way).

您不能扩展多个类,但可以扩展多个特征。与 Java 接口不同,特征还可以包括实现(方法定义、数据成员等)。仍然有一个区别,你不能直接实例化一个特征(在某种程度上类似于抽象类)。

trait T1
trait T2
trait T3
class C extends T1 with T2 with T3