使用 Jython 调用 Java 子类的方法



我有这个Java类,

public class sample {
        public Integer foo1(Integer x){
            return x+5;
        }
    }
class SubClass extends sample{
    public Integer foo2(Integer x){
        return x+100;
    }
}

对于Jython,我想称foo2SubClass.我最终得到了以下 Python 代码,

import SubClass, sample
java_file = SubClass()
print java_file.foo2(3)

但是运行 Python 代码会返回此错误,

AttributeError: 'SubClass' object has no attribute 'foo2'

我还想打印一个类的超类及其签名,包括公共、抽象等属性。

有没有办法做到这一点?谢谢!

您必须首先创建一个实例...调用方法...如以下示例:

Beach.java
public class Beach {
    private String name;
    private String city;

    public Beach(String name, String city){
        this.name = name;
        this.city = city;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
}
Using Beach.java in Jython
>>> import Beach
>>> beach = Beach("Cocoa Beach","Cocoa Beach")
>>> beach.getName()
u'Cocoa Beach'
>>> print beach.getName()
Cocoa Beach

您可以在此处阅读更多内容

最新更新