简体   繁体   English

Java nashorn比较java对象是否属于某种java类型

[英]Java nashorn compare if a java objects is of a certain java type

I am using instanceof but it is currently not working as I expect it. 我正在使用instanceof但它目前无法正常工作。 I have a variable I retrieve from my java code inside my script. 我有一个变量,我从我的脚本中的java代码中检索。 Let's call this variable myObject which is the instance of a MyObject class, as you would expect. 让我们调用这个变量myObject,它就是MyObject类的实例,正如您所期望的那样。

if (myObject instanceof Java.type("MyObject")) {
    //The check doesn't pass; the code here doesn't execute
}

I could only find vague information on the net about this. 我只能在网上找到关于此的模糊信息。 What is the definite way of checking if myObject is an instance of the MyObject class as I would easily do in Java? 检查myObject是否是MyObject类的实例的确切方法是什么,就像我在Java中轻松做的那样?

Thanks! 谢谢!

What you have should work, (with fully qualified class names, of course). 你有什么应该工作,(当然,有完全合格的类名)。 Eg this definitely works: 这肯定有效:

jjs> var x = new java.util.BitSet()
jjs> x instanceof Java.type("java.util.BitSet") 
true

Note: java.util.BitSet and Java.type("java.util.BitSet") are interchangeable, I just used both to illustrate that no matter how you construct the object, the result should be the same. 注意: java.util.BitSetJava.type("java.util.BitSet")是可以互换的,我只是用它们来说明无论你如何构造对象,结果应该是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM