简体   繁体   中英

Casting to a java object in Mozilla Rhino

I'm wondering is there a way of casting a Java object in Rhino? I would usually do this in Java by doing the following:

List<Object[]> mylist = myServiceClass.getList(id);
for (Object[] object : myList) {

  Apple a = (Apple) object[0];
  // do something    
}

But in Rhino, I'm not sure. Is there a way to do this?

I've tried:

apple = myList.get(i)

And:

apple = (Packages.com.package.fruits.Apple) myList.get(i);

I keep getting the error

Java class "[Ljava.lang.Object;" has no public instance field or method named "getColour" when trying to access the apple object.

You don't need to cast in javascript. But you need to call the method on the correct object. The error message states that you call the method on an Object[] (The [ before L gives that away).

To get the first object from Object[] before you call getColour() on it

myList.get(i)[0].getColour()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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