package collection;
public class Demo4 {
/**
* @泛型可以定义在类上,也可以定义在方法上
*/
public static void main(String[] args) {
/*
Demo<String> d = new Demo<String>();
d.show("abc");
d.print("aaa");
*/
Demo_01 d = new Demo_01();
d.show("asdfdfs");
d.print(123456);
d.print(new Integer(4));
}
}
class Demo<T>{
public void show(T t){
System.out.println("show:"+ t);
}
public void print(T t){
System.out.println("print:"+ t);
}
}
class Demo_01{
public <T> void show(T t){
System.out.println("show:"+ t);
}
public <T> void print(T t){
System.out.println("print:"+ t);
}
}
本文通过具体示例展示了Java中泛型的使用方法,包括如何将泛型应用于类及方法中,并提供了完整的代码实现。
299

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



