Java编程之范型的使用

package learn.java.cn.collection;
/*
 * 范型的使用
 * 1范型不能用于静态属性
 * 2.范型不能用于基本数据类型,用于引用类型
 */
public class TestGeneric {
	
	public static void main(String []args) 
	{
		int score=32;
		//Students<int> stu=new Students<int>( score);//error 
		//不能用于基本数据类型
		Students2<Integer> stu =new Students2<Integer> (score);
		Students2<String> stu2=new Students2<String>("良好");
		int sco=stu.getScore();
		System.out.println(sco);
		System.out.println(stu2.getScore());
	}

}



class Students2<T>
{
	public T score;
	public Students2(T score ) {
		this.score=score;
	}
	public T getScore() {
		return this.score;
	}
}

范型接口中,只能用在抽象方法上

猜你喜欢

转载自blog.csdn.net/qq_38662930/article/details/84180559