小编And*_*ani的帖子

Tricky Java Generics:使用泛型方法实现非泛型接口的泛型类

在ideone.com中看到以下代码:

import java.util.*;

class Test{
   interface Visitor{
        public <T> void visit(T Value);
   }

   class MyVisitor<T> implements Visitor{
        List<T> list = new  ArrayList<T>();

        public <T> void visit(T value){
           list.add(value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译时,此代码将产生以下+错误:

 Main.java:12: error: no suitable method found for add(T#1)
             list.add(value);
                 ^
     method List.add(int,T#2) is not applicable
       (actual and formal argument lists differ in length)
     method List.add(T#2) is not applicable
       (actual argument T#1 cannot be converted to T#2 by method invocation conversion)   where T#1,T#2 are type-variables:
     T#1 extends …

java generics

0
推荐指数
1
解决办法
6420
查看次数

标签 统计

generics ×1

java ×1