2017-08-03 13 views
2
public class Foo<T> { 
    public static <T> Foo<T> newFoo() { 
     return new Foo<>(); 
    } 

    public Bar<T, T> toBar() { 
     return new Bar<>(this, new ArrayList<T>()); 
    } 
} 

public class Bar<S, T> { 
    public Bar(Foo<T> Foo, List<S> list) { 

    } 

    public static void main(String[] args) { 
     Foo<Integer> newFoo = Foo.newFoo(); 
     Bar<Integer, Integer> s = newFoo.toBar(); 
     Bar<Integer, Integer> s2 = Foo.newFoo().toBar(); 
    } 
} 

メインメソッドの最初の2行は問題ありません。最後の行(Foo.newFoo().toBar())は私にエラー:Type mismatch: cannot convert from Bar<Object,Object> to Bar<Integer,Integer>を与えます。エラーを出さずにこれを1行に表示する方法はありますか? Bar<Integer, Integer>へのキャストは機能しません。この作品連鎖メソッド呼び出しの汎用仕様

答えて

1

必要以上に好奇心のうちをもっと...:

Bar<Integer, Integer> s2 = Foo.<Integer>newFoo().toBar(); 
関連する問題