Java泛型相关知识
Java里面的泛型在实际开发中运用的很多,比如分页插件,List接口的实现类ArrayList。
public class ArrayList<E> extends AbstractList<E> implements List<E>
public static <T> void main(String[] args) {
List<T> mylist=new ArrayList<T>();
}
}
一个简单的泛型
public class MyObject<T> extends BaseData { private T valueOne; private T valueTwo; public MyObject(T valueOne, T valueTwo) { this.valueOne = valueOne; this.valueTwo = valueTwo; } public T getValueOne() { return valueOne; } public void setValueOne(T valueOne) { this.valueOne = valueOne; } public T getValueTwo() { return valueTwo; } public void setValueTwo(T valueTwo) { this.valueTwo = valueTwo; } public static void main(String[] args) { MyObject<String> object = new MyObject<>("String one!", "String two"); System.out.println("value one " + object.valueOne + " value two " + object.valueTwo); } }