【发布时间】:2014-07-10 17:16:43
【问题描述】:
我想在一个类中实现一个通用接口。 考虑实现这个通用接口:
public interface Lookup<T>{
public T find(String name);
}
这是实现查找的非通用类:
public class IntegerLookup implements Lookup<Integer>{
private Integer[] values;
private String[] names;
public IntegerLookup(String[] names, Integer[] values) {......}
//now I want to write the implemented method - find
我的问题是: 我需要如何编写这个实现的方法? 我需要覆盖它吗?是吗?
public Object find(String name){...}
会好吗?或:
public Integer find(String name){...}
【问题讨论】:
-
在超类型中使用
T的任何地方,在使用Integer作为T的类型参数的子类中应使用Integer。 -
你为什么不能用@Override注解试试呢?
-
@SotiriosDelimanolis 但它是在超级类型中写的 Object 而不是 T ......或者我错了?
-
超类型
Lookup已将方法的返回类型声明为T。 -
你想要一个实用的方法来查找字符串但根据它的调用方式返回一个灵活的类型吗?