/*泛型接口的实现方式一:在子类的定义上声明泛型类型*/
interface Info6<T>{
public T getVar();
}
class Infoimpl<T> implements Info6<T>{ //在子类的定义上声明泛型类型
private T var;
public Infoimpl(T var){
this.setVar(var);
}
public void setVar(T var){
this.var = var;
}
public T getVar(){
return this.var;
}
}
public class GenericsInterfaceDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Info6<String> i = null;
i = new Infoimpl<String>("张三");
System.out.println("内容:"+i.getVar());
}
}
interface Info6<T>{
public T getVar();
}
class Infoimpl<T> implements Info6<T>{ //在子类的定义上声明泛型类型
private T var;
public Infoimpl(T var){
this.setVar(var);
}
public void setVar(T var){
this.var = var;
}
public T getVar(){
return this.var;
}
}
public class GenericsInterfaceDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Info6<String> i = null;
i = new Infoimpl<String>("张三");
System.out.println("内容:"+i.getVar());
}
}

本文介绍了一种使用Java实现泛型接口的方法。通过在子类定义中声明泛型类型,可以灵活地处理不同类型的对象。示例代码展示了如何创建一个泛型接口并实现它,同时提供了一个具体的字符串实例来说明其用法。
2336

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



