Never used Java before and trying to implement an interface in a class:
Interface:
public interface TestInterface {
public void doSomething(E elem);
}
Class:
public class TestClass implements TestInterface {
public static void main() {
// ...
}
public void doSomething(E elem) {
// ...
}
}
When I run the javac compiler. I get these errors:
TestClass.java:5: cannot find symbol
symbol: class E
public class TestClass implements TestInterface {
^
TestClass.java:11: cannot find symbol
symbol : class E
location: class TestClass
public void doSomething(E elem) {
^
If I replace all the "E"'s with i.e. "String", it will work, but I want it to use generic types. How do I do this?
解决方案
When creating TestClass you need to pass what is E, something like below
public class TestClass implements TestInterface {
public static void main() {
// ...
}
public void doSomething(String elem) {
// ...
}
}
or you need to make TestClass as generic
public class TestClass implements TestInterface {
public static void main() {
// ...
}
public void doSomething(E elem) {
// ...
}
}
在尝试使用Java实现一个接口时,遇到了编译错误,错误提示为找不到符号'E'。解决方法是在实现接口时指定'E'的具体类型,如`public class TestClass implements TestInterface<String>`,或者将TestClass声明为泛型类`public class TestClass<E> implements TestInterface<E>`。这样可以正确地使用泛型类型参数。
2385

被折叠的 条评论
为什么被折叠?



