在 Scala 中,Any 和 Object 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7160984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Scala, what is the difference between Any and Object?
提问by deltanovember
Suppose I have the following java method
假设我有以下 java 方法
protected void onEvent(Object obj) {
}
The Scala compiler accepts
Scala 编译器接受
protected override def onEvent(event: Any)
and
和
protected override def onEvent(event: Object)
Is there any difference between the two?
两者之间有什么区别吗?
采纳答案by 4e6
There is an articleon scala-lang with great diagram (I even put it on the wall). And also need to be mentioned:
有一篇关于 scala-lang的文章有很棒的图表(我什至把它贴在墙上)。并且还需要提到的是:
If Scala is used in the context of a Java runtime environment, then scala.AnyRef corresponds to java.lang.Object.
如果在 Java 运行时环境的上下文中使用 Scala,则 scala.AnyRef 对应于 java.lang.Object。
回答by Ernest Friedman-Hill
Anyincludes things that are not Objects in Java; it includes primitive types and also Nothing. Objectis the same class as in Java, so it definitely excludes primitives.
Any包括Object在 Java中不是s 的东西;它包括原始类型和Nothing. Object与 Java 中的类相同,因此它绝对不包括原语。

