您的位置:首页 > 其它

集合框架_泛型方法的概述和使用

2016-12-25 13:01 267 查看
package cn.itcast_05;

public class ObjectToolDemo {
public static void main(String[] args) {
// ObjectTool ot = new ObjectTool();
// ot.show("hello");
// ot.show(100);
// ot.show(true);

// ObjectTool<String> ot = new ObjectTool<String>();
// ot.show("hello");
//
// ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
// ot2.show(100);
//
// ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
// ot3.show(true);

// 如果能使用,那就说明泛型类是没有问题的
// 但是呢,谁说了我的方法要和类的类型一致呢?
// 我要是类上没有泛型的话,方法还能不能接收任意类型的参数了呢?
ObjectTool ot = new ObjectTool();
ot.show("hello");
ot.show(100);
ot.show(true);
}
}


package cn.itcast_05;

//public class ObjectTool<T> {
//	// public void show(String s){
//	// System.out.println(s);
//	// }
//	//
//	// public void show(Integer i){
//	// System.out.println(i);
//	// }
//	//
//	// public void show(Boolean b){
//	// System.out.println(b);
//	// }
//
//	public void show(T t){
//		System.out.println(t);
//	}
//}

/*
* 泛型方法:把泛型定义在方法上
*/
public class ObjectTool {
public <T> void show(T t) {
System.out.println(t);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: