什么是可堆叠修改?

编程入门 行业动态 更新时间:2024-10-27 11:21:27
本文介绍了什么是可堆叠修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在阅读一本关于Scala的书,并提及使用 traits 的可堆叠修改。

I've been reading a book about Scala and there's mention of stackable modifications using traits. What are stackable modifications and for what purposes are they meant to be used?

推荐答案

基本素质区分可堆叠的修改(就像Scala中使用的术语一样)是超级是基于特征如何混合而动态地影响,而一般来说,超级是静态确定的目标。

The fundamental quality which distinguishes stackable modifications (as the terminology is used in scala anyway) is that "super" is influenced dynamically based on how the trait is mixed in, whereas in general super is a statically determined target.

如果你写

abstract class Bar { def bar(x: Int): Int } class Foo extends Bar { def bar(x: Int) = x }

然后为Foosuper 将永远是酒吧。

then for Foo "super" will always be Bar.

如果你写

trait Foo1 extends Foo { abstract override def bar(x: Int) = x + super.bar(x) }

那么对于那个方法,超级仍然是未知的,直到创建类。

Then for that method super remains unknown until the class is made.

trait Foo2 extends Foo { abstract override def bar(x: Int) = x * super.bar(x) } scala> (new Foo with Foo2 with Foo1).bar(5) res0: Int = 30 scala> (new Foo with Foo1 with Foo2).bar(5) res1: Int = 50

Why is this interesting? An illustrative example might be some data which you want to compress, encrypt, and digitally sign. You might want to compress then encrypt then sign, or you might want to encrypt then sign then compress, etc. If you design your components in this way, you can instantiate a customized object with exactly the bits you want organized the way you want.

更多推荐

什么是可堆叠修改?

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

发布评论

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

>www.elefans.com

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