簡體   English   中英

使用Jython調用Java子類的方法

[英]Calling methods of a Java subclass using Jython

我有這個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'

我還想打印一個類的超類,它的簽名包括諸如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

你可以在這里閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM