`

Java 泛型学习(Java 泛型的理解与等价实现)02

阅读更多

三、泛型的综合运用实例(代码参考java参考大全,有改动)

  1. public class AvgGen<T extends Number> {   
  2.   public AvgGen() {   
  3.   }   
  4.   
  5.   public double getAvg(T[] arr) {   
  6.     double sum = 0.0;   
  7.     for (int i = 0; i < arr.length; i++) {   
  8.       sum = sum + arr[i].doubleValue();   
  9.     }   
  10.     return sum / arr.length;   
  11.   }   
  12.   
  13.   public static void main(String[] args) {   
  14.     // 整形数组求均值   
  15.     System.out.println("整形数组{1, 3}求均值:");   
  16.     Integer[] intArr = { 13 };   
  17.     AvgGen<Integer> intObj = new AvgGen<Integer>();   
  18.     double intavg = intObj.getAvg(intArr);   
  19.     System.out.println(intavg);   
  20.     System.out.println();   
  21.     // 浮点型数组求均值   
  22.     System.out.println("浮点型数组{1.1f,2.9f}求均值:");   
  23.     Float[] fArr = { 1.1f, 2.9f };   
  24.     AvgGen<Float> fObj = new AvgGen<Float>();   
  25.     double favg = fObj.getAvg(fArr);   
  26.     System.out.println(favg);   
  27.   }   
  28. }   
  29.   
  30. /**  
  31.  * Created by IntelliJ IDEA. User: leizhimin Date: 2007-9-18 Time: 11:08:14 使用通配符泛型参数:泛型参数是可变的,可在运行时来确定。  
  32.  */  
  33. public class AvgCompGen<T extends Number> {   
  34.   private T[] arr;   
  35.   
  36.   /**  
  37.    * 构造函数  
  38.    *   
  39.    * @param arr  
  40.    */  
  41.   public AvgCompGen(T[] arr) {   
  42.     this.arr = arr;   
  43.   }   
  44.   
  45.   /**  
  46.    * 求数组均值  
  47.    *   
  48.    * @return 数组均值  
  49.    */  
  50.   public double getAvg() {   
  51.     double sum = 0.0;   
  52.     for (int i = 0; i < arr.length; i++) {   
  53.       sum += arr[i].doubleValue();   
  54.     }   
  55.     return sum / arr.length;   
  56.   }   
  57.   
  58.   /**  
  59.    * 比较数组均值是否相等(使用通配符泛型参数) AvgCompGen<?>表示可以匹配任意的AvgCompGen对象,有点类似Object  
  60.    *   
  61.    * @param x 目标对象  
  62.    * @return 均值是否相等  
  63.    */  
  64.   public boolean sameAvg(AvgCompGen<?> x) {   
  65.     if (getAvg() == x.getAvg())   
  66.       return true;   
  67.     return false;   
  68.   }   
  69.   
  70.   /**  
  71.    * 主函数:用来测试  
  72.    *   
  73.    * @param args  
  74.    */  
  75.   public static void main(String[] args) {   
  76.     // 创建参数为Integer类型泛型对象   
  77.     Integer[] intArr = { 13 };   
  78.     AvgCompGen<Integer> intObj = new AvgCompGen<Integer>(intArr);   
  79.     System.out.println("intObj的平均值=" + intObj.getAvg());   
  80.     // 创建参数为Double类型泛型对象   
  81.     Double[] douArr = { 1.03.0 };   
  82.     AvgCompGen<Double> douObj = new AvgCompGen<Double>(douArr);   
  83.     System.out.println("douObj的平均值=" + douObj.getAvg());   
  84.     // 创建参数为Float类型泛型对象   
  85.     Float[] fltArr = { 0.8f, 3.2f };   
  86.     AvgCompGen<Float> fltObj = new AvgCompGen<Float>(fltArr);   
  87.     System.out.println("fltObj的平均值=" + fltObj.getAvg());   
  88.     // 两两比较对象的均值是否相等   
  89.     if (intObj.sameAvg(douObj))   
  90.       System.out.println("intArr与douArr的值相等,结果为:" + " intObj的均值=" + intObj.getAvg()   
  91.           + "   douObj的均值=" + douObj.getAvg());   
  92.     else  
  93.       System.out.println("intArr与douArr的值不相等,结果为:" + " intObj的均值=" + intObj.getAvg()   
  94.           + "   douObj的均值=" + douObj.getAvg());   
  95.     if (intObj.sameAvg(fltObj))   
  96.       System.out.println("intArr与fltObj的值相等,结果为:" + " intObj的均值=" + intObj.getAvg()   
  97.           + "   fltObj的均值=" + fltObj.getAvg());   
  98.     else  
  99.       System.out.println("intArr与fltObj的值不相等,结果为:" + " intObj的均值=" + intObj.getAvg()   
  100.           + "   fltObj的均值=" + fltObj.getAvg());   
  101.     if (douObj.sameAvg(fltObj))   
  102.       System.out.println("douObj与fltObj的值相等,结果为:" + " douObj的均值=" + intObj.getAvg()   
  103.           + "   fltObj的均值=" + fltObj.getAvg());   
  104.     else  
  105.       System.out.println("douObj与fltObj的值不相等,结果为:" + " douObj的均值=" + intObj.getAvg()   
  106.           + "   fltObj的均值=" + fltObj.getAvg());   
  107.   }   
  108. }   
  109.   
  110. /**  
  111.  * Created by IntelliJ IDEA. User: leizhimin Date: 2007-9-18 Time: 16:09:37 三种坐标,用泛型实现坐标打印  
  112.  */  
  113. public class TwoD {   
  114.   int x, y;   
  115.   
  116.   public TwoD(int x, int y) {   
  117.     this.x = x;   
  118.     this.y = y;   
  119.   }   
  120. }   
  121.   
  122. class ThreeD extends TwoD {   
  123.   int z;   
  124.   
  125.   public ThreeD(int x, int y, int z) {   
  126.     super(x, y);   
  127.     this.z = z;   
  128.   }   
  129. }   
  130.   
  131. class FourD extends ThreeD {   
  132.   int t;   
  133.   
  134.   public FourD(int x, int y, int z, int t) {   
  135.     super(x, y, z);   
  136.     this.t = t;   
  137.   }   
  138. }   
  139.   
  140. /**  
  141.  * 存放泛型坐标的(数据结构)类  
  142.  */  
  143. class Coords<T extends TwoD> {   
  144.   T[] coords;   
  145.   
  146.   public Coords(T[] coords) {   
  147.     this.coords = coords;   
  148.   }   
  149. }   
  150.   
  151. /**  
  152.  * 工具类--打印泛型数据 并给出一个测试方法  
  153.  */  
  154. class BoundeWildcard {   
  155.   static void showXY(Coords<?> c) {   
  156.     System.out.println("X Y Coordinates:");   
  157.     for (int i = 0; i < c.coords.length; i++) {   
  158.       System.out.println(c.coords[i].x + "  " + c.coords[i].y);   
  159.     }   
  160.     System.out.println();   
  161.   }   
  162.   
  163.   static void showXYZ(Coords<? extends ThreeD> c) {   
  164.     System.out.println("X Y Z Coordinates:");   
  165.     for (int i = 0; i < c.coords.length; i++) {   
  166.       System.out.println(c.coords[i].x + "  " + c.coords[i].y + "  " + c.coords[i].z);   
  167.     }   
  168.     System.out.println();   
  169.   }   
  170.   
  171.   static void showAll(Coords<? extends FourD> c) {   
  172.     System.out.println("X Y Z Coordinates:");   
  173.     for (int i = 0; i < c.coords.length; i++) {   
  174.       System.out.println(c.coords[i].x + "  " + c.coords[i].y + "  " + c.coords[i].z + "  "  
  175.           + c.coords[i].t);   
  176.     }   
  177.     System.out.println();   
  178.   }   
  179.   
  180.   public static void main(String args[]) {   
  181.     TwoD td[] = { new TwoD(00), new TwoD(79), new TwoD(184), new TwoD(-1, -23) };   
  182.     Coords<TwoD> tdlocs = new Coords<TwoD>(td);   
  183.     System.out.println("Contents of tdlocs.");   
  184.     showXY(tdlocs);   
  185.     FourD fd[] = { new FourD(1234), new FourD(68148), new FourD(22949),   
  186.         new FourD(3, -2, -2317) };   
  187.     Coords<FourD> fdlocs = new Coords<FourD>(fd);   
  188.     System.out.println("Contents of fdlocs.");   
  189.     showXY(fdlocs);   
  190.     showXYZ(fdlocs);   
  191.     showAll(fdlocs);   
  192.   }   
  193. }  
public class AvgGen<T extends Number> {
  public AvgGen() {
  }

  public double getAvg(T[] arr) {
    double sum = 0.0;
    for (int i = 0; i < arr.length; i++) {
      sum = sum + arr[i].doubleValue();
    }
    return sum / arr.length;
  }

  public static void main(String[] args) {
    // 整形数组求均值
    System.out.println("整形数组{1, 3}求均值:");
    Integer[] intArr = { 1, 3 };
    AvgGen<Integer> intObj = new AvgGen<Integer>();
    double intavg = intObj.getAvg(intArr);
    System.out.println(intavg);
    System.out.println();
    // 浮点型数组求均值
    System.out.println("浮点型数组{1.1f,2.9f}求均值:");
    Float[] fArr = { 1.1f, 2.9f };
    AvgGen<Float> fObj = new AvgGen<Float>();
    double favg = fObj.getAvg(fArr);
    System.out.println(favg);
  }
}

/**
 * Created by IntelliJ IDEA. User: leizhimin Date: 2007-9-18 Time: 11:08:14 使用通配符泛型参数:泛型参数是可变的,可在运行时来确定。
 */
public class AvgCompGen<T extends Number> {
  private T[] arr;

  /**
   * 构造函数
   * 
   * @param arr
   */
  public AvgCompGen(T[] arr) {
    this.arr = arr;
  }

  /**
   * 求数组均值
   * 
   * @return 数组均值
   */
  public double getAvg() {
    double sum = 0.0;
    for (int i = 0; i < arr.length; i++) {
      sum += arr[i].doubleValue();
    }
    return sum / arr.length;
  }

  /**
   * 比较数组均值是否相等(使用通配符泛型参数) AvgCompGen<?>表示可以匹配任意的AvgCompGen对象,有点类似Object
   * 
   * @param x 目标对象
   * @return 均值是否相等
   */
  public boolean sameAvg(AvgCompGen<?> x) {
    if (getAvg() == x.getAvg())
      return true;
    return false;
  }

  /**
   * 主函数:用来测试
   * 
   * @param args
   */
  public static void main(String[] args) {
    // 创建参数为Integer类型泛型对象
    Integer[] intArr = { 1, 3 };
    AvgCompGen<Integer> intObj = new AvgCompGen<Integer>(intArr);
    System.out.println("intObj的平均值=" + intObj.getAvg());
    // 创建参数为Double类型泛型对象
    Double[] douArr = { 1.0, 3.0 };
    AvgCompGen<Double> douObj = new AvgCompGen<Double>(douArr);
    System.out.println("douObj的平均值=" + douObj.getAvg());
    // 创建参数为Float类型泛型对象
    Float[] fltArr = { 0.8f, 3.2f };
    AvgCompGen<Float> fltObj = new AvgCompGen<Float>(fltArr);
    System.out.println("fltObj的平均值=" + fltObj.getAvg());
    // 两两比较对象的均值是否相等
    if (intObj.sameAvg(douObj))
      System.out.println("intArr与douArr的值相等,结果为:" + " intObj的均值=" + intObj.getAvg()
          + "   douObj的均值=" + douObj.getAvg());
    else
      System.out.println("intArr与douArr的值不相等,结果为:" + " intObj的均值=" + intObj.getAvg()
          + "   douObj的均值=" + douObj.getAvg());
    if (intObj.sameAvg(fltObj))
      System.out.println("intArr与fltObj的值相等,结果为:" + " intObj的均值=" + intObj.getAvg()
          + "   fltObj的均值=" + fltObj.getAvg());
    else
      System.out.println("intArr与fltObj的值不相等,结果为:" + " intObj的均值=" + intObj.getAvg()
          + "   fltObj的均值=" + fltObj.getAvg());
    if (douObj.sameAvg(fltObj))
      System.out.println("douObj与fltObj的值相等,结果为:" + " douObj的均值=" + intObj.getAvg()
          + "   fltObj的均值=" + fltObj.getAvg());
    else
      System.out.println("douObj与fltObj的值不相等,结果为:" + " douObj的均值=" + intObj.getAvg()
          + "   fltObj的均值=" + fltObj.getAvg());
  }
}

/**
 * Created by IntelliJ IDEA. User: leizhimin Date: 2007-9-18 Time: 16:09:37 三种坐标,用泛型实现坐标打印
 */
public class TwoD {
  int x, y;

  public TwoD(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

class ThreeD extends TwoD {
  int z;

  public ThreeD(int x, int y, int z) {
    super(x, y);
    this.z = z;
  }
}

class FourD extends ThreeD {
  int t;

  public FourD(int x, int y, int z, int t) {
    super(x, y, z);
    this.t = t;
  }
}

/**
 * 存放泛型坐标的(数据结构)类
 */
class Coords<T extends TwoD> {
  T[] coords;

  public Coords(T[] coords) {
    this.coords = coords;
  }
}

/**
 * 工具类--打印泛型数据 并给出一个测试方法
 */
class BoundeWildcard {
  static void showXY(Coords<?> c) {
    System.out.println("X Y Coordinates:");
    for (int i = 0; i < c.coords.length; i++) {
      System.out.println(c.coords[i].x + "  " + c.coords[i].y);
    }
    System.out.println();
  }

  static void showXYZ(Coords<? extends ThreeD> c) {
    System.out.println("X Y Z Coordinates:");
    for (int i = 0; i < c.coords.length; i++) {
      System.out.println(c.coords[i].x + "  " + c.coords[i].y + "  " + c.coords[i].z);
    }
    System.out.println();
  }

  static void showAll(Coords<? extends FourD> c) {
    System.out.println("X Y Z Coordinates:");
    for (int i = 0; i < c.coords.length; i++) {
      System.out.println(c.coords[i].x + "  " + c.coords[i].y + "  " + c.coords[i].z + "  "
          + c.coords[i].t);
    }
    System.out.println();
  }

  public static void main(String args[]) {
    TwoD td[] = { new TwoD(0, 0), new TwoD(7, 9), new TwoD(18, 4), new TwoD(-1, -23) };
    Coords<TwoD> tdlocs = new Coords<TwoD>(td);
    System.out.println("Contents of tdlocs.");
    showXY(tdlocs);
    FourD fd[] = { new FourD(1, 2, 3, 4), new FourD(6, 8, 14, 8), new FourD(22, 9, 4, 9),
        new FourD(3, -2, -23, 17) };
    Coords<FourD> fdlocs = new Coords<FourD>(fd);
    System.out.println("Contents of fdlocs.");
    showXY(fdlocs);
    showXYZ(fdlocs);
    showAll(fdlocs);
  }
}
原文请见: http://www.java2000.net/p7927
                 http://www.java2000.net/p7926



注意:多个泛型类、接口,接口、类继承,这种设计方式往往会导致泛型很复杂,程序的可读性急剧下降,程序中应该兼顾代码的可读性。

总结:
泛型其实就是一个类型的参数化,没有它程序照样写!把这句话记心里。有两层含义:一是泛型的实质,二是要知其然还要知其所以然。泛型不可怕,泛型的设计也从开发者角度出发的,使用得当会大大提高代码的安全性和简洁性。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics