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

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);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值