WEB开发网
开发学院WEB开发Jsp Java高级编程——泛型类型第二部分 阅读

Java高级编程——泛型类型第二部分

 2008-01-05 19:28:35 来源:WEB开发网   
核心提示:创建泛型和泛型方法创建一个简单的泛型是非常轻易的,首先,Java高级编程——泛型类型第二部分,在一对尖括号(< >)中声明类型变量,以逗号间隔变量名列表,例子中使用类型变量绑定来确保值类型的Serializable和Comparable,import java.io.Serializable;import

  创建泛型和泛型方法
  
  创建一个简单的泛型是非常轻易的。首先,在一对尖括号(< >)中声明类型变量,以逗号间隔变量名列表。在类的实例变量和方法中,可以在任何类型的地方使用那些类型变量。切记,类型变量仅在编译时存在,所以不能使用instanceof和new这类运行时操作符来操作类型变量。
  
  让我们以一个简单的例子来开始这部分的学习,而后将精简这个例子。这段代码定义了一个树形数据结构,使用类型变量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); }}
  
  正如你所看到的,命名一个类型变量习惯于一个大写字母。使用一个字母可以同现实中那些具有描述性的,长的实际变量名有所区别。使用大写字母要同变量命名规则一致,并且要区别于局部变量,方法参数,成员变量,而这些变量经常使用一个小写字母。集合类中,比如java.util中经常使用类型变量E代表“Element type”。T和S经常用来表示范型变量名(似乎使用i和j作为循环变量一样)。
  
  注重到,当一个变量被声明为泛型时,只能被实例变量和方法调用(还有内嵌类型)而不能被静态变量和方法调用。原因很简单,参数化的泛型是一些实例。静态成员是被类的实例和参数化的类所共享的,所以静态成员不应该有类型参数和他们关联。方法,包括静态方法,可以声明和使用他们自己的类型参数,但是,调用这样一个方法,可以被不同地参数化。这些内容将在本章后面谈到。
  
  类型变量绑定
  
  上面例子中的Tree<V>中的类型变量V是不受约束的:Tree可以被参数化为任何类型。以前我们经常会设置一些约束条件在需要使用的类型上:也许我们需要强制一个类型参数实现一个或多个接口,或是一个特定类的子类。这可以通过指明类型绑定来完成。我们已经看到了统配符的上界,而且使用简单的语法可以指定一般类型变量的上界。后面的代码,还是使用Tree这个例子,并且通过实现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后的名字和一个类型列表(这可以是参数化的,就像Comparable一样)表达的。

Tags:Java 高级 编程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接