什么是可堆叠修改?
我一直在读一本关于 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
区分可堆叠修改(无论如何在 scala 中使用该术语)的基本品质是“super”根据特征的混合方式动态受到影响,而一般来说 super 是静态确定的目标。
如果你
这样写,那么 Foo 的“super”将永远是 Bar。
如果您
为该方法编写 Then,则在创建类之前, super 仍然未知。
为什么这很有趣? 一个说明性示例可能是您想要压缩、加密和数字签名的一些数据。 您可能想要压缩然后加密然后签名,或者您可能想要加密然后签名然后压缩,等等。如果您以这种方式设计组件,您可以实例化一个自定义对象,其中包含您想要按照您想要的方式组织的位。
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.
If you write
then for Foo "super" will always be Bar.
If you write
Then for that method super remains unknown until the class is made.
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.
我查看了真实世界的 Scala 演示文稿其中还使用了术语“可堆叠修改”。 显然,这是在重写时调用 super 方法的特征,本质上是添加功能而不是替换它。 因此,您可以使用特征来积累功能,并且可以在 Java 中我们经常使用方面的地方使用它。 Trait 扮演一个方面的角色,覆盖“有趣”的方法并添加特定功能,例如日志记录等,然后调用 super 并将球“传球”到链中的下一个特征。 HTH。
I looked at Real-World Scala presentation where the term stackable modifications is also used. Apparently it's traits that call the super method when overriding, essentially adding functionality and not replacing it. So you accumulate functionality with traits, and it can be used where in Java we often use aspects. Trait plays the role of an aspect, overriding the "interesting" methods and adding the specific functionality such as logging etc. and then calling super and "passing the ball" to the next trait in the chain. HTH.