術(shù)語泛型意味著參數(shù)化類型。使用泛型,可以創(chuàng)建與不同類型的數(shù)據(jù)一起使用的單個類。在參數(shù)化類型上操作的類,接口或方法稱為通用。
以下是聲明通用類的語法:
class className<type-param-list> {}
下面是聲明對一個泛型類的引用的語法:
簡單泛型示例
// T is a type parameter that will be replaced by a real type // when an object of type Gen is created. class Gen<T> { T ob; // declare an object of type T Gen(T o) { ob = o; } // Return ob. T getob() { return ob; } // Show type of T. void showType() { System.out.println("Type of T is " + ob.getClass().getName()); } } public class Main { public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); iOb.showType(); int v = iOb.getob(); System.out.println("value: " + v); Gen<String> strOb = new Gen<String>("Generics Test"); strOb.showType(); String str = strOb.getob(); System.out.println("value: " + str); } }
T
是類型參數(shù)的名稱。 T
用于聲明一個對象。泛型只與對象一起工作通用類型根據(jù)其類型參數(shù)而有所不同。
上面的代碼生成以下結(jié)果。
您可以在泛型類型中聲明多個類型參數(shù)。
// A simple generic class with two type parameters: T and V. class TwoGen<T, V> { T ob1; V ob2; TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; } void showTypes() { System.out.println("Type of T is " + ob1.getClass().getName()); System.out.println("Type of V is " + ob2.getClass().getName()); } T getob1() { return ob1; } V getob2() { return ob2; } } public class Main { public static void main(String args[]) { TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics"); tgObj.showTypes(); int v = tgObj.getob1(); System.out.println("value: " + v); String str = tgObj.getob2(); System.out.println("value: " + str); } }
上面的代碼生成以下結(jié)果。
以下代碼聲明并使用隊列<E> 通用類型。
class Queue<E> { private E[] elements; private int head=0, tail=0; Queue(int size) { elements = (E[]) new Object[size]; } void insert(E element) throws QueueFullException { if (isFull()) throw new QueueFullException(); elements[tail] = element; tail = (tail + 1) % elements.length; } E remove() throws QueueEmptyException { if (isEmpty()){ throw new QueueEmptyException(); } E element = elements[head]; head = (head + 1) % elements.length; return element; } boolean isEmpty() { return head == tail; } boolean isFull() { return (tail + 1) % elements.length == head; } } class QueueEmptyException extends Exception { } class QueueFullException extends Exception { } public class Main{ public static void main(String[] args) throws QueueFullException, QueueEmptyException { Queue<String> queue = new Queue<String>(6); System.out.println("Empty: " + queue.isEmpty()); System.out.println("Full: " + queue.isFull()); queue.insert("A"); queue.insert("B"); queue.insert("C"); queue.insert("D"); queue.insert("E"); System.out.println("Empty: " + queue.isEmpty()); System.out.println("Full: " + queue.isFull()); System.out.println("Removing " + queue.remove()); System.out.println("Empty: " + queue.isEmpty()); System.out.println("Full: " + queue.isFull()); System.out.println("Adding F"); queue.insert("F"); while (!queue.isEmpty()){ System.out.println("Removing " + queue.remove()); } System.out.println("Empty: " + queue.isEmpty()); System.out.println("Full: " + queue.isFull()); } }
輸出:
為了處理向泛型的轉(zhuǎn)換,Java允許使用沒有任何類的通用類類型參數(shù)。
下面是一個顯示原始類型的示例:
class MyClass<T> { T ob; MyClass(T o) { ob = o; } T getob() { return ob; } } public class Main { public static void main(String args[]) { MyClass raw = new MyClass(new Double(98.6)); double d = (Double) raw.getob(); System.out.println("value: " + d); } }
輸出:
更多建議: