Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以使用Jython*从Java实例化Python类,而不必*使该Python类扩展Java接口?_Java_Python_Jython_Jython 2.5 - Fatal编程技术网

有没有一种方法可以使用Jython*从Java实例化Python类,而不必*使该Python类扩展Java接口?

有没有一种方法可以使用Jython*从Java实例化Python类,而不必*使该Python类扩展Java接口?,java,python,jython,jython-2.5,Java,Python,Jython,Jython 2.5,Jython文档中的所有示例都要求Python类导入相应的Java接口,例如: # A python module that implements a Java interface to # create a building object from org.jython.book.interfaces import BuildingType class Building(BuildingType): def __init__(self, name, address, id):

Jython文档中的所有示例都要求Python类导入相应的Java接口,例如:

# A python module that implements a Java interface to
# create a building object
from org.jython.book.interfaces import BuildingType

class Building(BuildingType):
    def __init__(self, name, address, id):
        self.name = name
        self.address = address
        self.id = id
        ...

我宁愿不要将Python代码与Java代码耦合。有没有一种方法可以从Java实例化Python类而不用修改它以指向Java接口?

到目前为止,我能想到的最好答案是围绕我要实例化的每个Python类实现Java包装器。每个Java包装器都持有一个PyObject,但提供对其字段的静态类型访问,例如

public class PeanutWrapper{
  private PyObject pyObj;

  public String getKind(){
    return pyObj.__findattr__("kind");
  }

  ...
}
这是可行的,但它并不像我希望找到的那样优雅