标签:
因为看的是旧书,所以没学习过泛型的内容。导致编译器编写代码的时候老是提示需要对通用类型参数化。今天终于找到一个实例了:
1 package comparableclass.main; 2 3 import com.bruceeckel.util.*; 4 5 public class SCompType implements Comparable<SCompType> { 6 String s1,s2; 7 public SCompType(String s1i,String s2i){ 8 s1=s1i; 9 s2=s2i; 10 } 11 @Override 12 public String toString(){ 13 return "[s1="+s1+",s2="+s2+"]"; 14 } 15 16 public int compareTo(SCompType rv){ 17 String rvi=rv.s1; 18 return s1.compareTo(rvi); 19 } 20 private static Arrays2.RandStringGenerator gen= 21 new Arrays2.RandStringGenerator(7); 22 public static Generator generator(){ 23 return new Generator(){ 24 public Object next(){ 25 return new SCompType((String)gen.next(),(String)gen.next()); 26 } 27 }; 28 } 29 }
1 package comparableclass.main; 2 3 import com.bruceeckel.util.*; 4 5 public class SCompType implements Comparable{//一直提示需要参数化 6 String s1,s2; 7 public SCompType(String s1i,String s2i){ 8 s1=s1i; 9 s2=s2i; 10 } 11 @Override 12 public String toString(){ 13 return "[s1="+s1+",s2="+s2+"]"; 14 } 15 16 public int compareTo(Object rv){ 17 String rvi=((SCompType)rv).s1; 18 return s1.compareTo(rvi); 19 } 20 private static Arrays2.RandStringGenerator gen= 21 new Arrays2.RandStringGenerator(7); 22 public static Generator generator(){ 23 return new Generator(){ 24 public Object next(){ 25 return new SCompType((String)gen.next(),(String)gen.next()); 26 } 27 }; 28 } 29 }
上面是对比代码。
标签:
原文地址:http://www.cnblogs.com/MicroPetter/p/4830612.html