gpt4 book ai didi

groovy - 'instanceof' 和 'in' 关键字有什么区别?

转载 作者:行者123 更新时间:2023-12-05 01:41:52 31 4
gpt4 key购买 nike

为了验证类成员身份ininstanceof 关键字的行为似乎相同。那么两者有什么区别呢?有什么区别吗? StackOverflow 上有几个问题(herehere),其中两个关键字都作为为此目的的解决方案给出,但没有提及两者之间的区别或何时更适合使用一个而不是另一个。此外,官方文档提到 the in keyword is equivalent to calling an object's isCase() method ,但没有详细说明 instanceof 关键字的作用。


这两个关键字在类继承和接口(interface)实现方面的行为似乎相同:

class MyMap extends LinkedHashMap { }

def foo = new LinkedHashMap()
def bar = new MyMap()

println("LinkedHashMap instance is 'in' LinkedHashMap: ${foo in LinkedHashMap}")
println("LinkedHashMap instance is 'instanceof' LinkedHashMap: ${foo instanceof LinkedHashMap}")
println("LinkedHashMap instance is 'in' Map: ${foo in Map}")
println("LinkedHashMap instance is 'instanceof' Map: ${foo instanceof Map}")
println("MyMap instance is 'in' LinkedHashMap: ${bar in LinkedHashMap}")
println("MyMap instance is 'instanceof' LinkedHashMap: ${bar instanceof LinkedHashMap}")
println("MyMap instance is 'in' Map: ${bar in Map}")
println("MyMap instance is 'instanceof' Map: ${bar instanceof Map}")

输出:

LinkedHashMap instance is 'in' LinkedHashMap: true
LinkedHashMap instance is 'instanceof' LinkedHashMap: true
LinkedHashMap instance is 'in' Map: true
LinkedHashMap instance is 'instanceof' Map: true
MyMap instance is 'in' LinkedHashMap: true
MyMap instance is 'instanceof' LinkedHashMap: true
MyMap instance is 'in' Map: true
MyMap instance is 'instanceof' Map: true

最佳答案

主要区别在于instanceof是Java关键字,而SomeClass中的obj相当于SomeClass.isCase(obj)方法按照你在问题中提到的那样打电话。

有一个主要含义:instanceof 不能被覆盖,正如 Oracle 文档所说:

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.


Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Class.isCase(obj)实现如下:

/**
* Special 'Case' implementation for Class, which allows testing
* for a certain class in a switch statement.
* For example:
* <pre>switch( obj ) {
* case List :
* // obj is a list
* break;
* case Set :
* // etc
* }</pre>
*
* @param caseValue the case value
* @param switchValue the switch value
* @return true if the switchValue is deemed to be assignable from the given class
* @since 1.0
*/
public static boolean isCase(Class caseValue, Object switchValue) {
if (switchValue instanceof Class) {
Class val = (Class) switchValue;
return caseValue.isAssignableFrom(val);
}
return caseValue.isInstance(switchValue);
}

来源:org/codehaus/groovy/runtime/DefaultGroovyMethods.java#L1121

根据源代码可以看出,Groovy 的 obj in SomeClass 并不是 instanceof 的别名,因为它做的更多一些。但是,有一件重要的事情值得一提 - 您可以覆盖 isCase() 实现,但您不能更改 instanceof Java 关键字的行为方式。如果您将 Class.isCase() 用作 Java 的 instanceof 关键字的替代项,那么覆盖它可能会对您的代码造成一些损害。

关于groovy - 'instanceof' 和 'in' 关键字有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090995/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com