您的位置:首页 > 编程语言 > Java开发

Java笔记之泛型接口

2011-11-25 11:32 267 查看
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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息