首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么Scala特征可以扩展类?

为什么Scala特征可以扩展类?
EN

Stack Overflow用户
提问于 2012-10-12 16:20:01
回答 1查看 14.3K关注 0票数 57

我发现Scala中的特征类似于Java中的接口(但Java中的接口扩展了其他接口,它们不会扩展类)。我见过an example on SO about traits usage,其中一个特征扩展了一个类。

这样做的目的是什么?为什么特征可以扩展类?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-12 16:23:38

是的,扩展classtrait限制了哪些classes可以扩展该trait -即所有混合的classes - trait必须扩展该class

代码语言:javascript
复制
scala> class Foo
defined class Foo

scala> trait FooTrait extends Foo
defined trait FooTrait

scala> val good = new Foo with FooTrait
good: Foo with FooTrait = $anon$1@773d3f62

scala> class Bar
defined class Bar

scala> val bad = new Bar with FooTrait
<console>:10: error: illegal inheritance; superclass Bar
 is not a subclass of the superclass Foo
 of the mixin trait FooTrait
       val bad = new Bar with FooTrait
                              ^
票数 71
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12854941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档