簡體   English   中英

Java和Jython與方法名稱的集成問題

[英]Java and Jython integration issue with method names

我正在遵循Jython和Java Integration中的指令。

這個想法很簡單; make Java接口,並匹配python類。 問題是,使用接口函數setX()和setY()時,我總是在執行文件時出錯。 我必須修改名稱,如setXvalue()或setYvalue(),以避免錯誤。

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class    
org.python.core.PyTraceback
at org.python.core.PyException.tracebackHere(PyException.java:158)

at org.python.core.PyObject._jcall(PyObject.java:3587)
at org.python.proxies.Arith$Arith$0.setX(Unknown Source) <-- ERROR???
at Main.main(Main.java:14)

package org.jython.book.interfaces;

這是一個Java接口。

public interface ArithType {

    public void setX(int x); // <-- Error
    public void setYa(int x);
    public int getXa();
    public int getYa();
    public int add();
}

這是部分python類。

class Arith(ArithType):
    ''' Class to hold building objects '''

    def setX(self, x): # << Error
        self.x = x

您可以在此站點找到要測試的源代碼 - https://dl.dropboxusercontent.com/u/10773282/2013/Archive.zip

這有什么問題? 為什么方法名稱setX()或setY()導致執行錯誤?

小心訪問Jython中對象的屬性; Jython使用隱式getter / setter,因此從self.x讀取會調用self.getX()等等。 在jython代碼中將self.x所有出現更改為self._x (同上為y )使其工作(對我而言)。 實際上,Python中的一個慣例是將非公共成員命名為_...

暫無
暫無

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

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