java之泛型(2)
接上一篇使用通配符(?)
通配符
在上一篇中沒有詳細(xì)講通配符的使用,蘿卜剛開始學(xué)這兒的時候覺得不好理解,后來一下子想開了。蘿卜覺得泛型最新重要的作用是解決了類型安全的問題,有些時候我們需要某種類型,同時不需要其他的類型,這個問題在泛型出現(xiàn)之前是很難解決的。
舉個很簡單的例子:容器類中都定義了泛型
ArrayList<String> strs = new ArrayList<String>
strs.add(13) //錯誤
有時候我們不確定我們具體要傳哪種類型,就可以使用<?>來匹配任意類型
例: 前面我們寫過一個求平均數(shù)的方法,現(xiàn)在我們的需求是比較兩個平均值是否相同。
這次我們定義一個類,接受一系列的數(shù),可以求平均值,比較平均值是否相同。
public class Nums<T extends Number>{ //泛型限制只能傳入數(shù)字類型
private T[] nums;
double aver;
public Nums(T... nums) {
this.nums = nums;
}
public double getAverage(){
double sum = 0;
for (T num : nums){
sum += num.doubleValue();
}
aver = sum/nums.length;
return aver;
}
public Boolean isSameAver(Nums<T> n2){
//這種方案適用范圍很窄 只能比較兩個類型參數(shù)一樣的對象
//但是我們想比較不同類型的例如: Nums<Double> 和 Nums<Short>
//這個時候我們可以使用<?>來匹配
//public boolean isSameAver(Nums<?> n2)
if(this.getAverage()==n2.getAverage()){
return true;
}
return false;
}
}
有界通配符
前面我們已經(jīng)用過有界的泛型:
<T extends 類名>
指定泛型上界,
類型只能為指定類或其子類;<T super 類名>
指定泛型的下界,類
型只能是指定類或其父類。
有界通配符的用法跟上面類似只要把T改成?就可以了,有時候我們需要某些類型但是不確定是具體哪種,尤其是存在多層次繼承結(jié)構(gòu),有界通配符就會顯得特別方便,最重要的是保證類型安全。
最典型例子就是顯示N維坐標(biāo):
class TwoD {//二維坐標(biāo)
int x;
int y;
public TwoD(int x, int y) {
this.x = x;
this.y = y;
}
}
class ThreeD extends TwoD{//三維坐標(biāo)
int z;
public ThreeD(int x, int y, int z) {
super(x, y);
this.z = z;
}
}
class FourD extends ThreeD{//四維坐標(biāo)
int t;
public FourD(int x, int y, int z, int t) {
super(x, y, z);
this.t = t;
}
}
class Points<T extends TwoD>{//只能接受坐標(biāo)類型
T[] points;
public Points(T...points) {
this.points = points;
}
}
class ShowPoint{
public static void showXY(Points<? extends TwoD> p){//TwoD或繼承TwoD的類都有x,y
for (int i=0; i<p.points.length; i++){
System.out.println("(" + "x=" + p.points[i].x + " y=" + p.points[i].y +")");
}
}
public static void showXYZ(Points<? extends ThreeD> p){//只有ThreeD或繼承ThreeD的類才有z
for (int i=0; i<p.points.length; i++){
System.out.println("(" + "x=" + p.points[i].x + " y=" + p.points[i].y + " z=" + p.points[i].z + ")");
}
}
public static void ShowXYZT(Points<? extends FourD> p) {//只有FourD或繼承FourD的類才有四個坐標(biāo)
for (int i=0; i<p.points.length; i++){
System.out.println("(" + "x=" + p.points[i].x + " y=" + p.points[i].y + " z=" + p.points[i].z + " t="+ p.points[i].t + ")");
}
}
}
public class Demo {
public static void main(String[] args) {
Points<TwoD> twoDPoints = new Points<TwoD>(new TwoD(1, 2), new TwoD(2, 3));//二維坐標(biāo)群
Points<ThreeD> threeDPoints = new Points<ThreeD>(new ThreeD(4, 5, 6), new ThreeD(5, 6, 7));//三維坐標(biāo)群
Points<FourD> fourDPoints = new Points<FourD>(new FourD(6, 7, 8, 9),new FourD(7, 8, 9, 10));//四維坐標(biāo)群
ShowPoint.showXY(twoDPoints); //ok
ShowPoint.showXY(threeDPoints);//ok
ShowPoint.showXY(fourDPoints);//ok
ShowPoint.showXYZ(threeDPoints);//ok
ShowPoint.showXYZ(fourDPoints);//ok
ShowPoint.ShowXYZT(fourDPoints);//ok
// ShowPoint.showXYZ(twoDPoints);//錯誤
// ShowPoint.showXYZT(ThreeDPoints);//錯誤;
}
}
結(jié)果: (x=1 y=2)
(x=2 y=3)
(x=4 y=5)
(x=5 y=6)
(x=6 y=7)
(x=7 y=8)
(x=4 y=5 z=6)
(x=5 y=6 z=7)
(x=6 y=7 z=8)
(x=7 y=8 z=9)
(x=6 y=7 z=8 t=9)
(x=7 y=8 z=9 t=10)
從上面的例子可以看出有界通配符用起來還是很方便的,可以按我們的意愿指定類型。
JDK1.7之后有了類型推斷:
比如前面我們要創(chuàng)建Points對象時要Points<TwoD> twoDs = new Points<Twod>(new TwoD(1,2))
有了類型推斷之后,我們可以
Points<TwoD> twoDs = new Points<>(new TwoD(1,2))
關(guān)于泛型還有幾點要注意
泛型不能被實例化,因為他只是一個占位符嘛。
class Demo<T>{
T t = new T;//錯誤...
}靜態(tài)方法不能使用類上定義的泛型,因為靜態(tài)方法優(yōu)先于對象存在,靜態(tài)方法只能使用定義在自己身上的泛型。
class Demo<T>{ public static void method(T t){}//錯誤}
使用泛型數(shù)組的時候不能實例化,而且不能創(chuàng)建具體類型的泛型數(shù)組
public class Demo <T>{ T[] ts;//ok ts = new T[10];//錯 誤,編譯器無法知道自己創(chuàng)建什么具體的數(shù)據(jù)類型的數(shù)組
Demo<String>[] strs = new Demo<String>[10];//錯誤
Demo<?>[] strs = new Demo<?>[10];//ok
}