2015-11-04 35 views
2

我知道我可以像這樣的Groovy類創建一個電話運營商:有沒有一個靜態的Groovy調用操作符?

class MyCallable { 
    int call(int x) {   
     2*x 
    } 
} 

def mc = new MyCallable() 
assert mc(2) == 4 

但我可以創造一個靜態調用的方法?

class MyStaticCallable { 
    static int call(int x) {   
     2*x 
    } 
} 

assert MyStaticCallable(2) == 4 

非常感謝您的幫助!

回答

1

不,你必須做

MyStaticCallable.call(2) 
+0

非常感謝@tim_yates!我最終用'import static'僞裝了它。 –

相關問題