在 Java 中宣告泛型(型別)時的限制
泛型是在 Java 中的一個概念,您可以使用該概念來啟用一個類、介面和方法,將所有(引用)型別作為引數接受。換句話說,它是一個概念,使使用者能夠動態選擇方法或類建構函式接受的引用型別。透過將一個類定義為泛型,您可以使其型別安全,即它可以在任何資料型別上起作用。
泛型的限制
您無法在某些方式和某些場景中使用泛型,如下所列 −
- 您無法將基元資料型別與泛型一起使用。
class Student<T>{
T age;
Student(T age){
this.age = age;
}
}
public class GenericsExample {
public static void main(String args[]) {
Student<Float> std1 = new Student<Float>(25.5f);
Student<String> std2 = new Student<String>("25");
Student<int> std3 = new Student<int>(25);
}
}編譯時錯誤
GenericsExample.java:11: error: unexpected type Student<int> std3 = new Student<int>(25); ^ required: reference found: int GenericsExample.java:11: error: unexpected type Student<int> std3 = new Student<int>(25); ^ required: reference found: int 2 errors
- 您無法例項化泛型引數。
class Student<T>{
T age;
Student(T age){
this.age = age;
}
public void display() {
System.out.println("Value of age: "+this.age);
}
}
public class GenericsExample {
public static void main(String args[]) {
Student<Float> std1 = new Student<Float>(25.5f);
std1.display();
T obj = new T();
}
}編譯時錯誤
GenericsExample.java:15: error: cannot find symbol T obj = new T(); ^ symbol: class T location: class GenericsExample GenericsExample.java:15: error: cannot find symbol T obj = new T(); ^ symbol: class T location: class GenericsExample 2 errors
- 泛型型別引數不能是靜態的。
class Student<T>{
static T age;
Student(T age){
this.age = age;
}
public void display() {
System.out.println("Value of age: "+this.age);
}
}
public class GenericsExample {
public static void main(String args[]) {
Student<Float> std1 = new Student<Float>(25.5f);
std1.display();
}
}編譯時錯誤
GenericsExample.java:3: error: non-static type variable T cannot be referenced from a static context static T age; ^ 1 error
- 您不能將一個數據型別的引數化型別轉換為另一個數據型別。
import java.util.ArrayList;
public class Generics {
public static void main(String args[]) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(25);
list1.add(26);
ArrayList<Number> list2 = list1;
}
}編譯時錯誤
Generics.java:8: error: incompatible types: ArrayList<Integer> cannot be converted to ArrayList<Number> ArrayList<Number> list2 = list1; ^ 1 error
- 您無法建立一個泛型型別物件的陣列。
class Student<T>{
T age;
Student(T age){
this.age = age;
}
public void display() {
System.out.println("Value of age: "+this.age);
}
}
public class GenericsExample {
public static void main(String args[]) {
Student<Float>[] std1 = new Student<Float>[5];
}
}輸出
GenericsExample.java:12: error: generic array creation Student<Float>[] std1 = new Student<Float>[5]; ^ 1 error
一個泛型型別類不能擴充套件 Throwable 類,因此,您不能捕獲或丟擲這些物件。
class Student<T>extends Throwable{
T age;
Student(T age){
this.age = age;
}
public void display() {
System.out.println("Value of age: "+this.age);
}
}
public class GenericsExample {
public static void main(String args[]) {
}
}編譯時錯誤
GenericsExample.java:1: error: a generic class may not extend java.lang.Throwable
class Student<T>extends Throwable{
^
1 error
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP