gpt4 book ai didi

java - 使用 Jython 调用 Java 子类的方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:19 25 4
gpt4 key购买 nike

我有这个 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,我想调用 SubClass 类的 foo2。我最终得到了以下 Python 代码,

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

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

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

我还想打印一个类的父类(super class)及其签名,包括 public、abstract 等属性。

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

最佳答案

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

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

您可以阅读更多here

关于java - 使用 Jython 调用 Java 子类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21114760/

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