三、泛型的综合运用实例(代码参考java参考大全,有改动)
- 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);
- }
- }
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
注意:多个泛型类、接口,接口、类继承,这种设计方式往往会导致泛型很复杂,程序的可读性急剧下降,程序中应该兼顾代码的可读性。
总结:
泛型其实就是一个类型的参数化,没有它程序照样写!把这句话记心里。有两层含义:一是泛型的实质,二是要知其然还要知其所以然。泛型不可怕,泛型的设计也从开发者角度出发的,使用得当会大大提高代码的安全性和简洁性。