泛型的使用
Java在實(shí)際的使用過程中,會有類型轉(zhuǎn)換的問題,也就存在類型轉(zhuǎn)化異常的安全問題;所以Java提供了泛型來解決這個安全問題。
泛型類
把泛型定義在類型
格式
public class 類名<泛型類型1,...> {}
注意
泛型類型必須是引用類型
測試
public class TestClass {
public static void main(String[] args) {
// 創(chuàng)建類時指定什么類型查邢,屬性就是什么類型。
// 把錯誤提前到編譯期顯示
ObjectTest<String> o1 = new ObjectTest<>();
o1.setT("張三");
System.out.println(o1.getT());
ObjectTest<Integer> o2 = new ObjectTest<>();
o2.setT(15);
System.out.println(o2.getT());
}
}
// 泛型類
class ObjectTest<T> {
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
泛型方法
把泛型定義在方法上
格式
public <泛型類型> 返回值類型 方法名(泛型類型1 args1,...) {}
測試
public class TestMethod {
public static void main(String[] args) {
// 泛型方法與類解綁
MethodTest m = new MethodTest();
System.out.println(m.show("張三"));
System.out.println(m.show(13));
}
}
class MethodTest {
// 泛型方法
public <T> T show(T t) {
return t;
}
}
public interface Validator {
default <T> T parseJson(String param, Class<T> clz) {
if (StringUtils.isEmpty(param)) {
System.out.println("param is empty");
return null;
}
T t = new Gson().fromJson(param, clz);
if (Objects.isNull(t)) {
System.out.println("parse param is null");
return null;
}
return t;
}
}
泛型接口
把泛型定義在接口上
格式
public interface <泛型類型1,...> {}
測試
-
實(shí)現(xiàn)類知道泛型的類型
public class TestInter01<T> { public static void main(String[] args) { Inter inter = new InterImpl(); inter.show("張三"); } } interface Inter<T> { void show(T t); } class InterImpl implements Inter<String> { @Override public void show(String s) { System.out.println(s); } }
-
實(shí)現(xiàn)類不知道泛型的類型(常用)
public class TestInter02 { public static void main(String[] args) { Inter<String> inter = new InterImpl<>(); inter.show("張三"); } } interface Inter<T> { void show(T t); } class InterImpl<T> implements Inter<T> { @Override public void show(T t) { System.out.println(t); } }
泛型通配符
class Animal {}
class Dog extends Animal {}
class Pig extends Animal {}
-
?
表示任意類型酵幕,如果類型明確扰藕,前后類型一致;如果沒有明確芳撒,就是Object以及任意子類
// 類型明確 public class TestTong { public static void main(String[] args) { Collection<Object> o1 = new ArrayList<Object>(); // 錯誤 // Collection<Object> o2 = new ArrayList<Animal>(); } }
// 類型不明確 public class TestTong { public static void main(String[] args) { Collection<?> o1 = new ArrayList<Object>(); Collection<?> o2 = new ArrayList<Animal>(); Collection<?> o3 = new ArrayList<Dog>(); } }
-
? extends E
向下限定邓深,E及其子類
public class TestTong { public static void main(String[] args) { // 錯誤 // Collection<? extends Animal> o1 = new ArrayList<Object>(); Collection<? extends Animal> o2 = new ArrayList<Animal>(); Collection<? extends Animal> o3 = new ArrayList<Dog>(); Collection<? extends Animal> o4 = new ArrayList<Pig>(); } }
-
? super E
向上限定,E及其父類
public class TestTong { public static void main(String[] args) { Collection<? super Animal> o1 = new ArrayList<Object>(); Collection<? super Animal> o2 = new ArrayList<Animal>(); // 錯誤 // Collection<? super Animal> o3 = new ArrayList<Dog>(); // Collection<? super Animal> o4 = new ArrayList<Pig>(); } }