定义:
/*
泛型类的定义:
泛型不能使用在静态属性上;
指定类型不能是基本类型,只能是引用类型;
*/
class mystu<T>{
T score;
public mystu() {
}
public mystu(T score) {
super();
this.score = score;
}
public T getScore() {
return score;
}
public void setScore(T score) {
this.score = score;
}
}测试使用,和ArrayList<E>一样,看源码:
package com.ws.generic;
import java.util.ArrayList;
import java.util.List;
public class generic {
public static void main(String[] args) {
mystu<Integer> stu = new mystu<Integer>();
stu.setScore(80);
System.out.println(stu.getScore());
mystu<String> stu2 = new mystu<String>();
stu2.setScore("及格");
System.out.println(stu2.getScore());
}
}非常简单,把复杂问题分解成一个个简单的小问题,就容易处理了。