可堆叠特征模式可以与单例对象一起使用吗?

编程入门 行业动态 更新时间:2024-10-28 18:33:29
本文介绍了可堆叠特征模式可以与单例对象一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想对单例对象使用 可堆叠特征模式,但我不能似乎找到了让编译器满意的方法:

I'd like to use the stackable trait pattern with singleton objects, but i can't seem to find how to make the compiler happy:

abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } object Foo extends Pr with PrePostPr { def pr() = println("Foo") }

尝试在 repl 中对此进行评估会产生以下错误:

Trying to evaluate this in the repl produces the following error:

<console>:10: error: overriding method pr in trait PrePostPr of type ()Unit; method pr needs `override' modifier def pr() = println("Foo")

推荐答案

可以,但是像这样:

abstract class Pr { def pr() } trait PrePostPr extends Pr { abstract override def pr() { println("prepr") super.pr() println("postpr") } } class ImplPr extends Pr { def pr() = println("Foo") } object Foo extends ImplPr with PrePostPr

实现必须存在于超类/超特征之一中.抽象修改特征必须在继承列表中具有实现的类/特征之后.

The implementation has to be present in one of the superclasses/supertraits. The abstract modification trait has to come after the class/trait with the implementation in the inheritance list.

更多推荐

可堆叠特征模式可以与单例对象一起使用吗?

本文发布于:2023-11-25 05:01:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特征   对象   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!