扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
以下代码来自http://www.java2s.com/ExampleCode/Language-Basics
1.一个参数的Generics
//Example 9(没有使用范型) class NonGen { Object ob; // ob is now of type Object // Pass the constructor a reference to // an object of type Object NonGen(Object o) { ob = o; } // Return type Object. Object getob() { return ob; } // Show type of ob. void showType() { System.out.println("Type of ob is " + ob.getClass().getName()); } } // Demonstrate the non-generic class. public class NonGenDemo { public static void main(String args[]) { NonGen iOb; // Create NonGen Object and store // an Integer in it. Autoboxing still occurs. iOb = new NonGen(88); // Show the type of data used by iOb. iOb.showType(); // Get the value of iOb. // This time, a cast is necessary. int v = (Integer) iOb.getob(); System.out.println("value: " + v); System.out.println(); // Create another NonGen object and // store a String in it. NonGen strOb = new NonGen("Non-Generics Test"); // Show the type of data used by strOb. strOb.showType(); // Get the value of strOb. // Again, notice that a cast is necessary. String str = (String) strOb.getob(); System.out.println("value: " + str); // This compiles, but is conceptually wrong! iOb = strOb; v = (Integer) iOb.getob(); // runtime error! } } |
//Example 10(使用范型) class Example1<T>{ private T t; Example1(T o){ this.t=o; } T getOb(){ return t; } void ShowObject(){ System.out.println("对象的类型是:"+t.getClass().getName()); } } public class GenericsExample1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Example1<Integer> examplei=new Example1<Integer>(100); examplei.ShowObject(); System.out.println("对象是:"+examplei.getOb()); Example1<String> examples=new Example1<String>("Bill"); examples.ShowObject(); System.out.println("对象是:"+examples.getOb()); } } |
我们来看Example 9没有使用范型,所以我们需要进行造型,而Example 10我们不需要任何的造型
2.二个参数的Generics
//Example 11 class TwoGen<T, V> { T ob1; V ob2; // Pass the constructor a reference to // an object of type T. TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; } // Show types of T and V. void showTypes() { System.out.println("Type of T is " + ob1.getClass().getName()); System.out.println("Type of V is " + ob2.getClass().getName()); } T getob1() { return ob1; } V getob2() { return ob2; } } public class GenericsExampleByTwoParam { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics"); // Show the types. tgObj.showTypes(); // Obtain and show values. int v = tgObj.getob1(); System.out.println("value: " + v); String str = tgObj.getob2(); System.out.println("value: " + str); } } |
3.Generics的Hierarchy
//Example 12 class Stats<T extends Number> { T[] nums; // array of Number or subclass // Pass the constructor a reference to // an array of type Number or subclass. Stats(T[] o) { nums = o; } // Return type double in all cases. double average() { double sum = 0.0; for(int i=0; i < nums.length; i++) sum += nums[i].doubleValue(); return sum / nums.length; } } public class GenericsExampleByHierarchy { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Integer inums[] = { 1, 2, 3, 4, 5 }; Stats<Integer> iob = new Stats<Integer>(inums); double v = iob.average(); System.out.println("iob average is " + v); Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Stats<Double> dob = new Stats<Double>(dnums); double w = dob.average(); System.out.println("dob average is " + w); // This won't compile because String is not a // subclass of Number. // String strs[] = { "1", "2", "3", "4", "5" }; // Stats<String> strob = new Stats<String>(strs); // double x = strob.average(); // System.out.println("strob average is " + v); } } |
4.使用通配符
//Example 14 class StatsWildCard<T extends Number> { T[] nums; // array of Number or subclass // Pass the constructor a reference to // an array of type Number or subclass. StatsWildCard(T[] o) { nums = o; } // Return type double in all cases. double average() { double sum = 0.0; for (int i = 0; i < nums.length; i++) sum += nums[i].doubleValue(); return sum / nums.length; } // Determine if two averages are the same. // Notice the use of the wildcard. boolean sameAvg(StatsWildCard<?> ob) { if (average() == ob.average()) return true; return false; } } public class GenericsExampleByWildcard { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Integer inums[] = { 1, 2, 3, 4, 5 }; StatsWildCard<Integer> iob = new StatsWildCard<Integer>(inums); double v = iob.average(); System.out.println("iob average is " + v); Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 }; StatsWildCard<Double> dob = new StatsWildCard<Double>(dnums); double w = dob.average(); System.out.println("dob average is " + w); Float fnums[] = { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F }; StatsWildCard<Float> fob = new StatsWildCard<Float>(fnums); double x = fob.average(); System.out.println("fob average is " + x); // See which arrays have same average. System.out.print("Averages of iob and dob "); if (iob.sameAvg(dob)) System.out.println("are the same."); else System.out.println("differ."); System.out.print("Averages of iob and fob "); if (iob.sameAvg(fob)) System.out.println("are the same."); else System.out.println("differ."); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者