为什么 Scala 类中没有静态成员?
我知道你可以间接定义它们 通过伴随对象实现类似的东西,但我想知道为什么作为一种语言设计,静态从类定义中删除。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我知道你可以间接定义它们 通过伴随对象实现类似的东西,但我想知道为什么作为一种语言设计,静态从类定义中删除。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
OO 中的 O 代表“对象”,而不是类。面向对象就是关于对象或实例(如果您愿意的话)
静态不属于对象,它们不能被继承,它们不参与多态性。简而言之,静态不是面向对象的。
另一方面,Scala 是面向对象的。远比 Java 更重要,Java 特别努力地表现得像 C++,以吸引该语言的开发人员。
它们是由 C++ 发明的 hack,它寻求在过程和 OO 编程的世界之间架起桥梁,并且需要与 C 向后兼容。出于类似的原因,它也承认原语。
Scala 放弃了静态和原语,因为它们是前过程开发人员需要安抚的时代的遗物。这些东西在任何设计良好、希望将自己描述为面向对象的语言中都没有地位。
关于为什么真正的面向对象很重要,我将无耻地复制 Bill Venners 的这段代码并将其粘贴到邮件列表中:
我自己简直不能说得更好了!
因此,如果您只想创建一个东西,那么静态和单例都可以完成这项工作。但是,如果您希望这一事物从某个地方继承行为,那么静态将无济于事。
根据我的经验,您对这种能力的使用往往比您最初想象的要多得多,尤其是在您使用 Scala 一段时间之后。
The O in OO stands for "Object", not class. Being object-oriented is all about the objects, or the instances (if you prefer)
Statics don't belong to an object, they can't be inherited, they don't take part in polymorphism. Simply put, statics aren't object-oriented.
Scala, on the other hand, is object oriented. Far more so than Java, which tried particularly hard to behave like C++, in order to attract developers from that language.
They are a hack, invented by C++, which was seeking to bridge the worlds of procedural and OO programming, and which needed to be backwardly compatible with C. It also admitted primitives for similar reasons.
Scala drops statics, and primitives, because they're a relic from a time when ex-procedural developers needed to be placated. These things have no place in any well-designed language that wishes to describe itself as object-oriented.
Concerning why it's important to by truly OO, I'm going to shamelessly copy and paste this snippet from Bill Venners on the mailing list:
Couldn't have put it better myself!
So if you want to create just one of something, then both statics and singletons can do the job. But if you want that one thing to inherit behaviour from somewhere, then statics won't help you.
In my experience, you tend to use that ability far more than you'd have originally thought, especially after you've used Scala for a while.
我还在 scala 用户谷歌组上发布了这个问题,“Programming in scala”回复的作者之一 Bill Venners 也有一些见解。
看看这个:https://groups.google.com/d /msg/scala-user/5jZZrJADbsc/6vZJgi42TIMJ 和 https://groups.google.com/d/msg/scala-user/5jZZrJADbsc /oTrLFtwGjpEJ
这是摘录:
另请参阅 Martin Odersky 的采访(向下滚动到 Scala 部分中的面向对象创新)http://www.artima.com/scalazine/articles/goals_of_scala.html
这是摘录:
总结:
从函数式编程的角度来看,静态成员通常被认为是不好的(请参阅此帖子,作者:Gilad Bracha——Java 泛型之父。它主要与全局状态的副作用有关)。但是 scala 必须找到一种与 Java 互操作的方法(因此它必须支持静态)并最小化(尽管不是完全避免)由于静态而创建的全局状态,scala 决定将它们隔离到伴随对象中。
伴随对象还具有可扩展的优点,即。利用继承和混合组合(与模拟互操作的静态功能分开)。
I also posted this question on scala users google group and Bill Venners one of the authors of "Programming in scala" reply had some insights.
Take a look at this: https://groups.google.com/d/msg/scala-user/5jZZrJADbsc/6vZJgi42TIMJ and https://groups.google.com/d/msg/scala-user/5jZZrJADbsc/oTrLFtwGjpEJ
Here is an excerpt:
Also take a look at this interview with Martin Odersky (scroll down to Object-oriented innovations in Scala section) http://www.artima.com/scalazine/articles/goals_of_scala.html
Here is an excerpt:
To Summarize:
From a functional programming perspective static members are generally considered bad (see this post by Gilad Bracha - the father of java generics. It mainly has to do with side effects because of global state). But scala had to find a way to be interoperable with Java (so it had to support statics) and to minimize (although not totally avoid) global states that is created because of statics, scala decided to isolate them into companion objects.
Companion objects also have the benefit of being extensible, ie. take advantage of inheritance and mixin composition (separate from emulating static functionality for interop).
当我想到静态如何使事情变得复杂时,这些是我脑海中浮现的事情:
1) 继承和多态性都需要特殊的规则。这是一个示例:
如果您 100% 确定打印出的内容,那么对您有好处。我们其他人可以安全地依赖像 @Override 注释这样的强大工具,这当然是可选的并且友好的“来自类型 A 的静态方法 f() 应该以静态方式访问方式”警告。这导致我们
2)访问内容的“静态方式”是一个进一步的特殊规则,它使事情变得复杂。
3) 静态成员不能是抽象的。我想你不可能拥有一切,对吧?
再说一遍,这些只是我思考了几分钟后想到的事情。我敢打赌还有很多其他原因导致静态不适合面向对象范式。
These are the things that pop into my head when I think about how statics could complicate things:
1) Inheritance as well as polymorphism would require special rules. Here is an example:
If you are 100% sure about what gets printed out, good for you. The rest of us can safely rely on mighty tools like
@Override
annotation, which is of course optional and the friendly "The static method f() from the type A should be accessed in a static way" warning. This leads us to2) The "static way" of accessing stuff is a further special rule, which complicates things.
3) Static members cannot be abstract. I guess you can't have everything, right?
And again, these are just things which came to my mind after I gave the matter some thought for a couple of minutes. I bet there are a bunch of other reasons, why statics just don't fit into the OO paradigm.
确实,静态成员不存在,但是,可以将单例对象与每个类关联:
以获得类似的结果
It's true, static member don't exists, BUT, it's possible to associate a singleton object to each class:
to obtain similar results
面向对象编程是关于对象及其状态的(不涉及 Java 中的有状态对象和无状态对象)。我试图强调“静态不属于物体”。静态字段不能用于表示对象的状态,因此从对象中提取是合理的。
Object oriented programming is all about objects and its states(Not touching state full and stateless objects in Java). I’m trying to stress “Static does not belong to objects”. Static fields cannot be used to represent a state of an object so it’s rational to pull off from objects.