scala `isInstanceOf` 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11290169/
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
How does `isInstanceOf` work?
提问by John Threepwood
Assume, we have:
假设,我们有:
class B
class A extends B
trait T
Then it holds:
那么它成立:
val a: A with T = new A with T
a.isInstanceOf[B] // result is true !
Is it right to say, the isInstanceOfmethod checks, if there is at least one type (not all types) which matches the right hand side in a subtype relationship?
是否正确地说,该isInstanceOf方法会检查是否至少有一种类型(不是所有类型)与子类型关系中的右侧匹配?
At first look, I thought a value with type A with Tcan not be a subtype of B, because AandTare not both subtypes of B. But it is AorTis a subtype of B-- is that right ?
乍一看,我认为具有 type 的值A with T不能是 的子类型B,因为A和T不是B. 但它是A或者T是 - 的一个子类型B,对吗?
回答by drexin
isInstanceOflooks if there is a corresponding entry in the inheritance chain. The chain of A with Tincludes A, Band T, so a.isInstanceOf[B]must be true.
isInstanceOf查看继承链中是否有对应的条目。A with T包括A,B和的链T,所以a.isInstanceOf[B]一定是真的。
edit:
编辑:
Actually the generated byte code calls javas instanceof, so it would be a instanceof Bin java. A little more complex call like a.isInstanceOf[A with T]would be (a instanceof A) && (a instanceof T).
实际上生成的字节码调用 javas instanceof,所以它会a instanceof B在 java. 像一个小更复杂的呼叫a.isInstanceOf[A with T]会(a instanceof A) && (a instanceof T)。
回答by Daniel C. Sobral
At first look, I thought a value with type A with T can not be a subtype of B
乍一看,我认为类型为 A 和 T 的值不能是 B 的子类型
There's are two misconceptions here. First, that the static type of an instance has any bearing on the result of isInstanceOf: there is none. To be clear, when doing a.isInstanceOf[B], the fact that ais of type A with Tis not relevant.
这里有两个误解。首先,实例的静态类型对以下结果有任何影响isInstanceOf:没有。需要明确的是,在做a.isInstanceOf[B]的时候,a类型的事实A with T是不相关的。
The method isInstanceOfis implemented at the bytecode level by the JVM. It looks at the class information every instance carries, and checks whether Bone of the classes (the class of the instance itself and its ancestors), or one of the implemented interfaces. That's the "is-a" relationship: "a is a B".
该方法isInstanceOf由 JVM 在字节码级别实现。它查看每个实例携带的类信息,并检查是B类之一(实例本身及其祖先的类),还是已实现的接口之一。这就是“is-a”关系:“a is a B”。
Technically, isInstanceOfis part of Java's reflection, where it is known as instanceof.
从技术上讲,isInstanceOf它是 Java 反射的一部分,称为instanceof.
The second misconception is the inheritance can somehow removea parent type. That never happens: inheritance only adds types, never removes them. The type A with Tis an A, a B, a T, an AnyValand an Any. So even if isInstanceOfdid look at the type A with T, it would still return true.
第二个误解是继承可以以某种方式删除父类型。那永远不会发生:继承只添加类型,从不删除它们。类型A with T为 an A、 a B、 a T、 anAnyVal和 an Any。因此,即使isInstanceOf确实查看了 type A with T,它仍然会返回 true。

