国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java高級編程——泛型類型第二部分

2019-11-17 06:34:36
字體:
來源:轉載
供稿:網(wǎng)友

  創(chuàng)建泛型和泛型方法
  
  創(chuàng)建一個簡單的泛型是非常輕易的。首先,在一對尖括號(< >)中聲明類型變量,以逗號間隔變量名列表。在類的實例變量和方法中,可以在任何類型的地方使用那些類型變量。切記,類型變量僅在編譯時存在,所以不能使用instanceof和new這類運行時操作符來操作類型變量。
  
  讓我們以一個簡單的例子來開始這部分的學習,而后將精簡這個例子。這段代碼定義了一個樹形數(shù)據(jù)結構,使用類型變量V代表存儲在各個樹結點中的值。
  
  import Java.util.*;/** * A tree is a data strUCture that holds values of type V. * Each tree has a single value of type V and can have any number of * branches, each of which is itself a Tree. */public class Tree<V> {  // The value of the tree is of type V.  V value;  // A Tree<V> can have branches, each of which is also a Tree<V>  List<Tree<V>> branches = new ArrayList<Tree<V>>();  // Here's the constructor. Note the use of the type variable V.  public Tree(V value) { this.value = value; }
  // These are instance methods for manipulating the node value and branches.
  // Note the use of the type variable V in the arguments or return types.
  V getValue() { return value; }
  void setValue(V value) { this.value = value; }
  int getNumBranches() { return branches.size(); }
  Tree<V> getBranch(int n) { return branches.get(n); }
  void addBranch(Tree<V> branch) { branches.add(branch); }}
  
  正如你所看到的,命名一個類型變量習慣于一個大寫字母。使用一個字母可以同現(xiàn)實中那些具有描述性的,長的實際變量名有所區(qū)別。使用大寫字母要同變量命名規(guī)則一致,并且要區(qū)別于局部變量,方法參數(shù),成員變量,而這些變量經(jīng)常使用一個小寫字母。集合類中,比如java.util中經(jīng)常使用類型變量E代表“Element type”。T和S經(jīng)常用來表示范型變量名(似乎使用i和j作為循環(huán)變量一樣)。
  
  注重到,當一個變量被聲明為泛型時,只能被實例變量和方法調用(還有內嵌類型)而不能被靜態(tài)變量和方法調用。原因很簡單,參數(shù)化的泛型是一些實例。靜態(tài)成員是被類的實例和參數(shù)化的類所共享的,所以靜態(tài)成員不應該有類型參數(shù)和他們關聯(lián)。方法,包括靜態(tài)方法,可以聲明和使用他們自己的類型參數(shù),但是,調用這樣一個方法,可以被不同地參數(shù)化。這些內容將在本章后面談到。
  
  類型變量綁定
  
  上面例子中的Tree<V>中的類型變量V是不受約束的:Tree可以被參數(shù)化為任何類型。以前我們經(jīng)常會設置一些約束條件在需要使用的類型上:也許我們需要強制一個類型參數(shù)實現(xiàn)一個或多個接口,或是一個特定類的子類。這可以通過指明類型綁定來完成。我們已經(jīng)看到了統(tǒng)配符的上界,而且使用簡單的語法可以指定一般類型變量的上界。后面的代碼,還是使用Tree這個例子,并且通過實現(xiàn)Serializable和Comparable來重寫。為了做到這點,例子中使用類型變量綁定來確保值類型的Serializable和Comparable。
  
  import java.io.Serializable;import java.util.*;public class Tree<V extends Serializable & Comparable<V>>
  implements Serializable, Comparable<Tree<V>>{
  V value;
  List<Tree<V>> branches = new ArrayList<Tree<V>>();
  public Tree(V value) { this.value = value; }
  // Instance methods  V getValue() { return value; }
  void setValue(V value) { this.value = value; }
  int getNumBranches() { return branches.size(); }
  Tree<V> getBranch(int n) { return branches.get(n); }
  void addBranch(Tree<V> branch) { branches.add(branch); }
  // This method is a nonrecursive implementation of Comparable<Tree<V>>
  // It only compares the value of this node and ignores branches.
  public int compareTo(Tree<V> that) {
  if (this.value == null && that.value == null) return 0;
  if (this.value == null) return -1;
  if (that.value == null) return 1;
  return this.value.compareTo(that.value);
  }
  // javac -Xlint warns us if we omit this field in a Serializable class
  PRivate static final long serialVersionUID = 833546143621133467L;}
  
  一個類型變量的綁定是通過extends后的名字和一個類型列表(這可以是參數(shù)化的,就像Comparable一樣)表達的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金阳县| 高淳县| 开阳县| 长阳| 宁城县| 赣州市| 图们市| 石首市| 延庆县| 无锡市| 伊吾县| 建昌县| 隆子县| 南平市| 建昌县| 黄大仙区| 乌审旗| 赞皇县| 大埔县| 宁陵县| 惠水县| 玉山县| 黄山市| 游戏| 深泽县| 建阳市| 类乌齐县| 邢台市| 泰州市| 襄汾县| 定襄县| 神池县| 宿迁市| 遂昌县| 无为县| 唐河县| 乌海市| 穆棱市| 沅陵县| 祥云县| 东丽区|