Java泛型基礎(chǔ)
1. 認(rèn)識(shí)泛型
- 泛型是在JDK1.5之后增加的新功能.
- 泛型可以解決數(shù)據(jù)的安全性問題, 主要的原理是在類聲明的時(shí)候通過一個(gè)標(biāo)識(shí)表示類中某個(gè)屬性的類型或者是某個(gè)方法的返回值及參數(shù)類型.
- 格式:
訪問權(quán)限 class 類名稱<泛型, 泛型...>{
屬性
方法
}
對(duì)象的創(chuàng)建:
類名稱<具體類型> 對(duì)象名稱 = new 類名稱<具體類型>();
示例
/**
* 經(jīng)緯度
*
* @author dixin
*
*/
class Point<T> {
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
public class GenericDemo01 {
public static void main(String[] args) {
Point<String> p1 = new Point<String>();
p1.setX("經(jīng)度為10");
p1.setY("緯度為100");
Point<Integer> p2 = new Point<Integer>();
p2.setX(10);
p2.setY(100);
System.out.println(p1.getX() + ", " + p1.getY());
System.out.println(p2.getX() + ", " + p2.getY());
}
}
// 執(zhí)行結(jié)果
經(jīng)度為10, 緯度為100
10, 100
2. 構(gòu)造方法中使用泛型
class Con<T> {
private T value;
// 類定義中已經(jīng)定義泛型T, 方法中可以直接使用, 不用加<>
public Con(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
public class GenericDemo02 {
public static void main(String[] args) {
Con<String> c = new Con<String>("構(gòu)造方法中使用泛型");
System.out.println(c.getValue());
}
}
3. 設(shè)置多個(gè)泛型
- 兩個(gè)泛型的例子:
class Gen<K, T> {
private K key;
private T value;
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
public class GenericDemo03 {
public static void main(String[] args) {
Gen<String, Integer> gen = new Gen<String, Integer>();
gen.setKey("key");
gen.setValue(10);
System.out.println(gen.getKey() + ", " + gen.getValue());
}
}
4. 通配符
- 類型不統(tǒng)一問題
class Info<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
@Override
public String toString() {
return this.getValue().toString();
}
}
public class GenericDemo04 {
public static void main(String[] args) {
Info<String> i = new Info<String>();
i.setValue("類型不統(tǒng)一");
tell(i); // 編譯報(bào)錯(cuò)
// The method tell(Info<Object>) in the type GenericDemo04 is not applicable for the arguments
(Info<String>)
}
public static void tell(Info<Object> i) {
System.out.println(i);
}
}
原因:
泛型是不可變的, 對(duì)于任意兩個(gè)不同的類型Type1和Type2, List<Type1>既不是List<Type2>的子類型, 也不是List<Type2>的父類型. 所以這里不能將<String>轉(zhuǎn)換成<Object>.
解決方式:
-
public static void tell(Info<Object> i)
中去掉<Object>
, 使用raw類型, 但這樣就失去了泛型的安全性檢查意義. - 更好的方式, 采用通配符.
修改為public static void tell(Info<?> i)
5. 泛型接口
聲明泛型接口和聲明泛型類的語法類似, 也是在接口名稱后面加上<T>.
格式:
interface 接口名稱<泛型標(biāo)識(shí)>
示例:
interface IGen<T> {
public void say();
}
class GenImpl<T> implements IGen<T> {
private String info;
public GenImpl(String info) {
this.info = info;
}
public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
@Override
public void say() {
System.out.println(this.info);
}
}
public class GenericDemo05 {
public static void main(String[] args) {
IGen<String> g = new GenImpl<String>("泛型接口");
g.say();
}
}
6. 泛型方法
- 泛型方法中可以定義泛型參數(shù), 此時(shí), 參數(shù)的類型就是傳入數(shù)據(jù)類型.
- 格式:
訪問權(quán)限 <泛型標(biāo)識(shí)> 泛型標(biāo)識(shí) 方法名稱([泛型標(biāo)識(shí) 參數(shù)名稱])
- 示例:
public class GenericDemo06 {
public static void main(String[] args) {
String str = tell("Hello");
System.out.println(str);
int i = tell(10);
System.out.println(i);
}
// <T>是第一泛型參數(shù), 寫在訪問權(quán)限和static后面
public static <T> T tell(T t) {
return t;
}
}
// 執(zhí)行結(jié)果
Hello
10
7. 泛型數(shù)組
泛型數(shù)組的使用要和泛型方法搭配使用.
- 在使用泛型方法的時(shí)候, 也可以傳遞或返回一個(gè)泛型數(shù)組.
public class GenericDemo07 {
public static void main(String[] args) {
String arrStr[] = { "A", "B", "C" };
tell(arrStr);
Integer arrInt[] = { 1, 2, 3 };
tell(arrInt);
}
public static <T> void tell(T arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}