2016-10-28 9 views
0

最近Groovyを学び始めて、自然な振る舞いを見つけ出しませんでした。Groovy intと整数関数の解決

class BasicRouter { 
    int method_(first){ 
     return 1; 
    } 
    int method_(int first){ 
     return 2; 
    } 
    int method_(short second){ 
     return 3; 
    } 
    int method_(Integer first){ 
     return 4; 
    } 
} 
br = new BasicRouter() 
int x = 1 
assert br.method_(x) == 4 
assert br.method_((int)29) == 2 

なぜ我々はint型の変数を渡している最初のケースでは、我々は4ではなく2を取得しますか? メソッドint method_(int)が呼び出されると思います。

ありがとうございました。

答えて

3

答えはここにある - Groovyのはすべて

のためのオブジェクトを使用していますhttp://docs.groovy-lang.org/latest/html/documentation/core-differences-java.html#_primitives_and_wrappers

これは、あなたがCompileStatic

を使用する場合
@groovy.transform.CompileStatic 
def fInt(int x) {new BasicRouter().method_(x)} 
assert fInt(1) == 2 
+0

ありがとう変わります。 int型とInteger型は、CompileStaticにのみ必要です。通常のGroovyの場合、彼らは常に同じですか? –

+1

@BobSmelton、これはあなたに感謝の代わりに役立つ場合は、これを回答として受け入れることを検討してください。 – Rao

関連する問題