Java教程 - 什么是Java中的泛型类型
术语泛型意味着参数化类型。使用泛型,可以创建与不同类型的数据一起使用的单个类。在参数化类型上操作的类,接口或方法称为通用。
语法
以下是声明通用类的语法:
class className<type-param-list> {}
下面是声明对一个泛型类的引用的语法:
className<type-arg-list> varName = new className<type-arg-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
是类型参数的名称。 T
用于声明一个对象。泛型只与对象一起工作通用类型根据其类型参数而有所不同。
上面的代码生成以下结果。
Type of T is java.lang.Integer
value: 88
Type of is java.lang.String
value: Generics Test
例2
您可以在泛型类型中声明多个类型参数。
// 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);
}
}
上面的代码生成以下结果。
Type of T is java.lang.Integer
Type of V is java.lang.String
value: 88
value: Generics
例3
以下代码声明并使用队列<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());
}
}
输出:
Empty: true
Full: false
Empty: false
Full: true
Removing A
Empty: false
Full: false
Adding F
Removing B
Removing C
处理旧代码
为了处理向泛型的转换,Java允许使用没有任何类的通用类类型参数。
下面是一个显示原始类型的示例:
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);
}
}
输出:
value: 98.6