不忘初心 砥礪前行, Tomorrow Is Another Day !java
本文概要:數組
本節概要:bash
泛型的優勢:ide
public class Location<T>{//標識符號能夠隨意寫
private T x ;
private T y ;
public void setX(T x){//做爲參數
this.x = x ;
}
public void setY(T y){
this.y = y ;
}
public T getX(){//做爲返回值
return this.x ;
}
public T getY(){
return this.y ;
}
}
複製代碼
以 , 進行分隔函數
public class MoreLocation<T,U> {//標識符號能夠隨意寫
private T x;
private U y;
public void setX(T x) {//做爲參數
this.x = x;
}
public void setY(U y) {
this.y = y;
}
public T getX() {//做爲返回值
return this.x;
}
public U getY() {
return this.y;
}
}
複製代碼
雖然標識符號能夠隨意取,但爲了提升可讀性,通常遵循如下規則.post
在接口上定義泛型與在類中定義泛型是同樣的學習
interface ILocation<T>{ // 在接口上定義泛型
T getZ() ;
void setZ(T z);
}
複製代碼
public class LocationImpl implements ILocation<String>{
private String z ;
public LocationImpl(String z){
this.setZ(z) ;
}
@Override
public void setZ(String z){
this.z = z ;
}
@Override
public String getZ(){
return this.z ;
}
}
複製代碼
publick class LocationImpl<T,K,U> implements ILocation<U>{ //把第三個泛型變量U用來填充接口
private U z ;
private T x;
private K y;
public LocationImpl(U z){
this.setZ(z) ;
}
@Override
public void setZ(U z){
this.z = z ;
}
@Override
public U getZ(){
return this.z ;
}
}
複製代碼
在方法的返回值前加上來表示泛型變量.ui
public class MethodLocation {
public static <T> void staticMethod(T text) {
System.out.println("staticMethod:" + text);
}
//返回值與形參中存在泛型
public <T> T normalMethod(T text) {
System.out.println("normalMethod:" + text);
return text;
}
//定義泛型數組, T[]至關於String[].
public static <T> T[] funArray(T...arg){ // 接收可變參數
return arg ; // 返回泛型數組
}
public static void main(String[] args) {
MethodLocation methodLocation = new MethodLocation();
String text1 = methodLocation.normalMethod("hello");//方法1
String text2 = methodLocation.<String>normalMethod("genericity");//方法2
System.out.println("from main method:"+text1);
System.out.println("--------------------------");
Integer[] intArray = MethodLocation.funArray(1, 2, 3, 4, 5, 6);
System.out.println(Arrays.toString(intArray));
}
}
//輸出結果
normalMethod:hello
normalMethod:genericity
from main method:hello
--------------------------
[1, 2, 3, 4, 5, 6]
複製代碼
注意:雖然方法2是推薦方式,但實際在IDEA中會提示使用方法1,這是由於它會實時檢測傳入類型是否一致,不一致直接提醒編譯不經過.this
本節概要:spa
在前一節中,T只能是Object的子類,因此在編譯時只能調用Object類的相關方法.假如如今有個dog類,咱們想在編譯時就調用他父類Animal的方法.這時就須要用到類型綁定.
定義:
< T extends BoundingType >
T和BoundingType能夠是類,也能夠是接口.
此處的==extends==僅僅表示子類型,不等同於繼承.
優勢:
//省略....建立dog,Animal,IAction相關代碼.
public class TypeBind {
public static void main(String[] args) {
Dog dog = new Dog("旺財");
String nameClass = getAnimalNameClass(dog);
System.out.println(nameClass);
System.out.println("---------------");
String actionInterface = getAnimalActionInterface(dog);
System.out.println(actionInterface);
System.out.println("---------------");
String nameAction = getAnimalNameAndAction(dog, dog);
System.out.println(nameAction);
}
//綁定類
private static <T extends Animal> String getAnimalNameClass(T sub) {
//這樣編譯時,就能調用Animal中的方法.
return sub.getName();
}
//綁定接口
private static <T extends IAction> String getAnimalActionInterface(T sub) {
return sub.howl();
}
//多個泛型綁定多個類型,經過&符號指定多個類型
private static <T extends Animal & Serializable, U extends IAction & Serializable> String
getAnimalNameAndAction(T sub1, U sub2) {
return sub1.getName() + sub2.howl();
}
}
複製代碼
通配符的意義就是它是一個未知的符號,能夠是表明==任意的類==,一般用?號表示.
做用:用於填充泛型變量T,表示表明任意類型!僅僅是填充方式的一種.
//使用位置有且只有,只能用來在生成泛型實例時使用.
Location<?> location;
location = new Location<Integer>();
複製代碼
Location<? extends Number> location;
location = new Location<Integer>(2,3);
//取
Number number = location.getX();//編譯經過
//存
location.setX(new Integer(123);//編譯報錯
複製代碼
location的類型由<? extends Number>決定,並不會由於location = new Location(2,3)而改變類型.因此不能肯定通配符類型.
/**
* 準備三個類Animal(動物),Mammal(哺乳動物),Mouse(老鼠).
* Mouse 繼承 Mammal, Mammal 繼承 Animal.
* .....省略相關類代碼
*/
List<? super Mammal> list;
list = new ArrayList<Animal>;
//取
Animal animal = list.get(0);//編譯報錯
Object object = list.get(0);
//存
list.add(new Animal());//編譯報錯
list.add(new Mammal());
list.add(new Mouse());
複製代碼
取的時候報錯,編譯器不能肯定<? super Mammal>的父類就是Animal.
至於存的時候報錯,一樣由於是未知類型,編譯器只能肯定通配符類型爲Mammal及其子類.
對於編譯器,只要能肯定通配符類型,就會編譯經過.不然會報錯.
最後的話:
本文只是對參考連接的內容總結,如需詳細瞭解,請參考啓艦大佬的博客連接.
因爲本人技術有限,若有錯誤的地方,麻煩你們給我提出來,本人不勝感激,你們一塊兒學習進步.
參考連接: