2016-04-22 70 views
0

我想重寫Groovy中的靜態getAt運算符。它運行良好,但我被IntelliJ中的惡意警告難住了。Groovy重載靜態getAt(下標)運算符

class SubscriptTest { 

    static def map = [ 
     (SubscriptKey.ONE) : 'one', 
     (SubscriptKey.TWO) : 'two', 
     (SubscriptKey.THREE): 'three', 
    ] 

    static Object getAt(SubscriptKey key) { 
     map[key] 
    } 
} 

enum SubscriptKey { 
    ONE, 
    TWO, 
    THREE 
} 

當我運行

它打印one。但是我也在IntelliJ中得到了下面的警告。在 'org.codehaus.groovy.runtime.DefaultGroovyMethods'

'getAt' 不能應用於 '(SubscriptKey)'

不知道如何解決/禁止這種警告。

回答

2

我試圖重寫靜態getAt運營商在Groovy

沒有靜態getAt方法,有一個not-static getAt method。下面是如何重寫非靜態getAt方法

// run this code in the Groovy console 
class SubscriptTest { 

    static def map = [ 
     (SubscriptKey.ONE) : 'one', 
     (SubscriptKey.TWO) : 'two', 
     (SubscriptKey.THREE): 'three', 
    ] 

    Object getAt(String key) { 
     'overriden getAt invoked' 
    } 
} 

enum SubscriptKey { 
    ONE, 
    TWO, 
    THREE 
} 

assert new SubscriptTest()['key'] == 'overriden getAt invoked' 
+1

我認爲這只是的IntelliJ犯錯 –

+0

我也認爲這是一個方式的IntelliJ分析你的代碼,並試圖匹配'與getAt'爲例[DefaultGroovyMethods] (http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html)作爲一種解決方法,您可以嘗試在編輯器中查找和禁用檢查規則。 – luka5z