Java 泛型類擴(kuò)展

2018-02-20 00:38 更新

Java教程 - 如何擴(kuò)展Java泛型類


泛型類可以充當(dāng)超類或者作為子類。在泛型層次結(jié)構(gòu)中,需要任何類型參數(shù)由泛型超類必須由所有子類向上傳遞到層次結(jié)構(gòu)。

例子

使用泛型超類

class MyClass<T> {
  T ob;
  MyClass(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class MySubclass<T, V> extends MyClass<T> {
  V ob2;

  MySubclass(T o, V o2) {
    super(o);
    ob2 = o2;
  }

  V getob2() {
    return ob2;
  }
}
public class Main {
  public static void main(String args[]) {
    MySubclass<String, Integer> x = new MySubclass<String, Integer>("Value is: ", 99);
    System.out.print(x.getob());
    System.out.println(x.getob2());
  }
}

上面的代碼生成以下結(jié)果。


例2

非類屬類是類屬子類的超類是完全可以接受的。

class MyClass {
  int num;

  MyClass(int i) {
    num = i;
  }

  int getnum() {
    return num;
  }
}
class MySubclass<T> extends MyClass {
  T ob;
  MySubclass(T o, int i) {
    super(i);
    ob = o;
  }
  T getob() {
    return ob;
  }
}
public class Main {
  public static void main(String args[]) {
     MySubclass<String> w = new MySubclass<String>("Hello", 4);
    System.out.print(w.getob() + " ");
    System.out.println(w.getnum());
  }
}

上面的代碼生成以下結(jié)果。


例3

instanceof運(yùn)算符可以應(yīng)用于泛型類的對(duì)象。

class Gen<T> {
  T ob;

  Gen(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
}

public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<Integer> iOb2 = new Gen2<Integer>(99);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    System.out.println("iOb2 is instance of Gen2"+(iOb2 instanceof Gen2<?>));
    System.out.println("iOb2 is instance of Gen"+(iOb2 instanceof Gen<?>));
    System.out.println("strOb2 is instance of Gen2"+(strOb2 instanceof Gen2<?>));
    System.out.println("strOb2 is instance of Gen"+(strOb2 instanceof Gen<?>));
    System.out.println("iOb is instance of Gen2"+(iOb instanceof Gen2<?>));
    System.out.println("iOb is instance of Gen"+(iOb instanceof Gen<?>));
  }
}

輸出:

例4

泛型類中的方法可以像任何其他方法一樣重寫。

class Gen<T> {
  T obj;
  Gen(T o) {
    obj = o;
  }
  T getob() {
    System.out.print("Gen"s getob(): ");
    return obj;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
  T getob() {
    System.out.print("Gen2"s getob(): ");
    return obj;
  }
}
public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    System.out.println(iOb.getob());
    System.out.println(strOb2.getob());
  }
}

上面的代碼生成以下結(jié)果。

Java泛型類型轉(zhuǎn)換

你可以將一個(gè)泛型類的一個(gè)實(shí)例轉(zhuǎn)換成另一個(gè)只有兩個(gè)是兼容的,它們的類型參數(shù)是相同的。

例如,假設(shè)以下程序,此強(qiáng)制轉(zhuǎn)換是合法的:

class Gen<T> {
  T ob;

  Gen(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
}

public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<Integer> iOb2 = new Gen2<Integer>(99);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    iOb = (Gen<Integer>) iOb2;
  }
}

因?yàn)閕Ob2是Gen< Integer>的實(shí)例。 但是,這個(gè)演員:

class Gen<T> {
  T ob;

  Gen(T o) {
    ob = o;
  }
  T getob() {
    return ob;
  }
}
class Gen2<T> extends Gen<T> {
  Gen2(T o) {
    super(o);
  }
}

public class Main {
  public static void main(String args[]) {
    Gen<Integer> iOb = new Gen<Integer>(88);
    Gen2<Integer> iOb2 = new Gen2<Integer>(99);
    Gen2<String> strOb2 = new Gen2<String>("Generics Test");
    //iOb = (Gen<Long>) iOb2;//wrong
  }
}

是不合法的,因?yàn)閕Ob2不是Gen<Long>的實(shí)例。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)