我试图理解java中的泛型,并尝试这个简单的例子,但不能使它工作; 它回来时有错误
线程"main"中的异常java.lang.Error:未解决的编译问题:javaTest.Main.main(Main.java)javaTest.Main.testShape(Main.java:21)中的类型T未定义方法base() :25)
下面是代码
class Shape{
int id=1;
void base(){
System.out.println("Shape.base()");
}
}
// unrelated class, but same func name
class OtherShape{
float id=1.1f;
void base(){
System.out.println("OtherShape.base()");
}
}
public class Main {
static <T>void testShape(T shape){
shape.base();
}
public static void main(String[] args) {
testShape(new Shape() );
testShape(new OtherShape());
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何让它发挥作用的任何想法?