2011-09-15 5 views
0
package com.openwaf.test.basic; 
public class MethodArgumentTest { 
    static interface Inf{} 
    static class One<E extends Inf > implements Inf{ 
     public <T extends One> T get(T k){ 
      return k; 
     } 
    } 
    static class Two<E extends Inf> extends One<E>{ } 
    public static void test(){ 
     One o=new One<Inf>(); 
     Two t=new Two<One>(); 
     o.<Two>get(t); 
    } 
} 

上記のコードはテスト用です。私見では問題なくコンパイルする必要がありますが、Javaコンパイラは言うエラージェネリックメソッドin java

MethodArgumentTest.java:15: com.openwaf.test.basic.MethodArgumentTestで のget(com.openwaf.test.basic.MethodArgumentTest.One) .1つは (com.openwaf.test.basic.MethodArgumentTest.Two)に適用できません。

o.get(t);

1エラー

誰かがここに私を助けてくださいことはできますか?あなたはこれが唯一の私は、このコードはのために良いもの要求しませんテストするためのものであると私はあなたが何をする必要があるかと思います

答えて

1

OKです。次のコンパイルでは警告が生成されます。あなたは十分にジェネリックを定義し、その結果としてはありませんでした:

public class MethodArgumentTest { 
    static interface Inf { 
    } 

    static class One<E extends Inf> implements Inf { 
     public <T extends One<E>> T get(T k) { 
      return k; 
     } 
    } 

    static class Two<E extends Inf> extends One<E> { 
    } 

    public static void test() { 
     One<Inf> o = new One<Inf>(); 
     Two<One<Inf>> t = new Two<One<Inf>>(); 

     o.<Two> get(t); /* unchecked warning */ 
    } 
} 
+0

ありがとうございました...問題がラインにありました o = new One ();コンパイラの処理方法を掘り下げました..とにかくこの問題は今解決されました..もう一度ありがとう.. :) –

+0

いいえ、それは興味深い練習でした:-) – home

0

は、

Two tPrime= o.get(t); 
+0

私が見ることができ、コンパイラが同じ言うよう..互換性のない見つけ種類 動作しません:必要なcom.openwaf.test.basic.MethodArgumentTest.One を:com.openwaf.test.basic.MethodArgumentTest.Two 2つのtPrime = o.get(t); –