泛型与自定义类型

作用:为了限制输入集合或数组的数据类型,避免因类型不一致发生错误。

泛型:库中原有的

ArrayList<String>list=new ArrayList<String>();

自定义泛型:就是在定义类时不直接声明参数类型,用<T>来代替

package com.powerlbs;

class CachePool<T>{
	T temp;
	public void save(T temp){
		this.temp=temp;
	}
	public T get(){
		return temp;
	}
}
public class test {

	public static void main(String args[]){
		
		CachePool <Integer> pool=new CachePool <Integer> ();
		pool.save(new Integer(1));
		Integer temp=pool.get();
		System.out.println(temp);
		
		
	}
}

类时不指定具体的数据类型,用<T>来代替

猜你喜欢

转载自blog.csdn.net/qiuyushuofeng/article/details/81457275