标签:
1 x = 10、y = 10 2 x = 12.88、y = 129.65 3 x = "东京180度"、y = "北纬210度"
1 int --> Integer --> Object 2 double -->Double --> Object 3 String --> Object
1 public class Demo { 2 public static void main(String[] args){ 3 Point p = new Point(); 4 p.setX(10); // int -> Integer -> Object 5 p.setY(20); 6 int x = (Integer)p.getX(); // 必须向下转型 7 int y = (Integer)p.getY(); 8 System.out.println("This point is:" + x + ", " + y); 9 10 p.setX(25.4); // double -> Integer -> Object 11 p.setY("东京180度"); 12 double m = (Double)p.getX(); // 必须向下转型 13 double n = (Double)p.getY(); // 运行期间抛出异常 14 System.out.println("This point is:" + m + ", " + n); 15 } 16 } 17 class Point{ 18 Object x = 0; 19 Object y = 0; 20 public Object getX() { 21 return x; 22 } 23 public void setX(Object x) { 24 this.x = x; 25 } 26 public Object getY() { 27 return y; 28 } 29 public void setY(Object y) { 30 this.y = y; 31 } 32 }
1 public class Demo { 2 public static void main(String[] args){ 3 // 实例化泛型类 4 Point<Integer, Integer> p1 = new Point<Integer, Integer>(); 5 p1.setX(10); 6 p1.setY(20); 7 int x = p1.getX(); 8 int y = p1.getY(); 9 System.out.println("This point is:" + x + ", " + y); 10 11 Point<Double, String> p2 = new Point<Double, String>(); 12 p2.setX(25.4); 13 p2.setY("东京180度"); 14 double m = p2.getX(); 15 String n = p2.getY(); 16 System.out.println("This point is:" + m + ", " + n); 17 } 18 } 19 // 定义泛型类 20 class Point<T1, T2>{ 21 T1 x; 22 T2 y; 23 public T1 getX() { 24 return x; 25 } 26 public void setX(T1 x) { 27 this.x = x; 28 } 29 public T2 getY() { 30 return y; 31 } 32 public void setY(T2 y) { 33 this.y = y; 34 } 35 }
运行结果:
This point is:10, 201 public class Demo { 2 public static void main(String[] args){ 3 // 实例化泛型类 4 Point<Integer, Integer> p1 = new Point<Integer, Integer>(); 5 p1.setX(10); 6 p1.setY(20); 7 p1.printPoint(p1.getX(), p1.getY()); 8 9 Point<Double, String> p2 = new Point<Double, String>(); 10 p2.setX(25.4); 11 p2.setY("东京180度"); 12 p2.printPoint(p2.getX(), p2.getY()); 13 } 14 } 15 // 定义泛型类 16 class Point<T1, T2>{ 17 T1 x; 18 T2 y; 19 public T1 getX() { 20 return x; 21 } 22 public void setX(T1 x) { 23 this.x = x; 24 } 25 public T2 getY() { 26 return y; 27 } 28 public void setY(T2 y) { 29 this.y = y; 30 } 31 32 // 定义泛型方法 33 public <T1, T2> void printPoint(T1 x, T2 y){ 34 T1 m = x; 35 T2 n = y; 36 System.out.println("This point is:" + m + ", " + n); 37 } 38 }
1 public static <V1, V2> void printPoint(V1 x, V2 y){ 2 V1 m = x; 3 V2 n = y; 4 System.out.println("This point is:" + m + ", " + n); 5 }
1 public class Demo { 2 public static void main(String arsg[]) { 3 Info<String> obj = new InfoImp<String>("www.weixueyuan.net"); 4 System.out.println("Length Of String: " + obj.getVar().length()); 5 } 6 } 7 //定义泛型接口 8 interface Info<T> { 9 public T getVar(); 10 } 11 //实现接口 12 class InfoImp<T> implements Info<T> { 13 private T var; 14 // 定义泛型构造方法 15 public InfoImp(T var) { 16 this.setVar(var); 17 } 18 public void setVar(T var) { 19 this.var = var; 20 } 21 public T getVar() { 22 return this.var; 23 } 24 }
1 public class Demo { 2 public static void main(String[] args){ 3 Point p = new Point(); // 类型擦除 4 p.setX(10); 5 p.setY(20.8); 6 int x = (Integer)p.getX(); // 向下转型 7 double y = (Double)p.getY(); 8 System.out.println("This point is:" + x + ", " + y); 9 } 10 } 11 class Point<T1, T2>{ 12 T1 x; 13 T2 y; 14 public T1 getX() { 15 return x; 16 } 17 public void setX(T1 x) { 18 this.x = x; 19 } 20 public T2 getY() { 21 return y; 22 } 23 public void setY(T2 y) { 24 this.y = y; 25 } 26 }
1 public <T> T getMax(T array[]){ 2 T max = null; 3 for(T element : array){ 4 max = element.doubleValue() > max.doubleValue() ? element : max; 5 } 6 return max; 7 }
1 public <T extends Number> T getMax(T array[]){ 2 T max = null; 3 for(T element : array){ 4 max = element.doubleValue() > max.doubleValue() ? element : max; 5 } 6 return max; 7 }
标签:
原文地址:http://www.cnblogs.com/Coda/p/4445706.html