首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java 五泛型深入研究 泛型——精解

2012-11-08 
Java 5泛型深入研究 泛型——精解[sizemedium][/size]elseSystem.out.println(intArr与douArr的值不相等,

Java 5泛型深入研究 泛型——精解
[size=medium][/size]        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,,
                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);
    }
}

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

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

本文出自 “熔 岩” 博客,转载请与作者联系!

本文出自 51CTO.COM技术博客

热点排行