interface Demo<T> { // 定义泛型接口
public void print(T param); // 定义抽象方法,此方法输出参数
}
// 实现泛型接口,方法一:
class Imple1<T> implements Demo<T> {
public void print(T param) {
System.out.println("param = " + param);
}
}
//实现泛型接口,方法二:
class Imple2 implements Demo<Imple2> {
public void print(Imple2 param) {
System.out.println("param = " + param);
}
}
public class GenericsDemo {
public static void main(String[] args) {
// 对以上程序进行测试
Imple1<String> demo = new Imple1<String>();
demo.print("Hello, Wrold!");
Imple2 im = new Imple2();
im.print(new Imple2());
}
}
Java笔记之泛型接口
最新推荐文章于 2021-11-21 21:14:06 发布