Java基础之集合框架(二)--TreeSet、泛型

本文深入探讨Java中TreeSet的两种排序方式及其原理,并详细解析泛型的概念、使用场景及高级特性,如泛型类、泛型方法、泛型接口、通配符等。
[java]  view plain copy
  1. import java.util.*;  
  2.   
  3. /* 
  4. Set:无序,不可以重复元素。 
  5.     |--HashSet:数据结构是哈希表。线程是非同步的。 
  6.                 保证元素唯一性的原理:判断元素的hashCode值是否相同。 
  7.                 如果相同,还会继续判断元素的equals方法,是否为true。 
  8.  
  9.     |--TreeSet:可以对Set集合中的元素进行排序。 
  10.                 底层数据结构是二叉树。 
  11.                 保证元素唯一性的依据: 
  12.                 compareTo方法return 0. 
  13.  
  14.                 TreeSet排序的第一种方式:让元素自身具备比较性。 
  15.                 元素需要实现Comparable接口,覆盖compareTo方法。 
  16.                 也种方式也成为元素的自然顺序,或者叫做默认顺序。 
  17.  
  18.                 TreeSet的第二种排序方式。 
  19.                 当元素自身不具备比较性时,或者具备的比较性不是所需要的。 
  20.                 这时就需要让集合自身具备比较性。 
  21.                 在集合初始化时,就有了比较方式。 
  22.  
  23. 需求: 
  24. 往TreeSet集合中存储自定义对象学生。 
  25. 想按照学生的年龄进行排序。 
  26.  
  27. 记住,排序时,当主要条件相同时,一定判断一下次要条件。 
  28.  
  29. */  
  30.   
  31. class TreeSetDemo   
  32. {  
  33.     public static void main(String[] args)   
  34.     {  
  35.         TreeSet ts = new TreeSet();  
  36.   
  37.         ts.add(new Student("lisi02",22));  
  38.         ts.add(new Student("lisi007",20));  
  39.         ts.add(new Student("lisi09",19));  
  40.         ts.add(new Student("lisi08",19));  
  41.         //ts.add(new Student("lisi007",20));  
  42.         //ts.add(new Student("lisi01",40));  
  43.   
  44.         Iterator it = ts.iterator();  
  45.         while(it.hasNext())  
  46.         {  
  47.             Student stu = (Student)it.next();  
  48.             System.out.println(stu.getName()+"..."+stu.getAge());  
  49.         }  
  50.     }  
  51. }  
  52.   
  53.   
  54. class Student implements Comparable//该接口强制让学生具备比较性。  
  55. {  
  56.     private String name;  
  57.     private int age;  
  58.   
  59.     Student(String name,int age)  
  60.     {  
  61.         this.name = name;  
  62.         this.age = age;  
  63.     }  
  64.   
  65.     public int compareTo(Object obj)  
  66.     {  
  67.   
  68.         //return 0;  
  69.           
  70.         if(!(obj instanceof Student))  
  71.             throw new RuntimeException("不是学生对象");  
  72.         Student s = (Student)obj;  
  73.   
  74.         System.out.println(this.name+"....compareto....."+s.name);  
  75.         if(this.age>s.age)  
  76.             return 1;  
  77.         if(this.age==s.age)  
  78.         {  
  79.             return this.name.compareTo(s.name);  
  80.         }  
  81.         return -1;  
  82.         /**/  
  83.     }  
  84.   
  85.     public String getName()  
  86.     {  
  87.         return name;  
  88.   
  89.     }  
  90.     public int getAge()  
  91.     {  
  92.         return age;  
  93.     }  
  94. }  

[java]  view plain copy
  1. import java.util.*;  
  2.   
  3. /* 
  4. 当元素自身不具备比较性,或者具备的比较性不是所需要的。 
  5. 这时需要让容器自身具备比较性。 
  6. 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。 
  7.  
  8. 当两种排序都存在时,以比较器为主。 
  9.  
  10. 定义一个类,实现Comparator接口,覆盖compare方法。 
  11.  
  12.  
  13. */  
  14. class Student implements Comparable//该接口强制让学生具备比较性。  
  15. {  
  16.     private String name;  
  17.     private int age;  
  18.   
  19.     Student(String name,int age)  
  20.     {  
  21.         this.name = name;  
  22.         this.age = age;  
  23.     }  
  24.   
  25.     public int compareTo(Object obj)  
  26.     {  
  27.   
  28.         //return 0;  
  29.           
  30.         if(!(obj instanceof Student))  
  31.             throw new RuntimeException("不是学生对象");  
  32.         Student s = (Student)obj;  
  33.   
  34.         //System.out.println(this.name+"....compareto....."+s.name);  
  35.         if(this.age>s.age)  
  36.             return 1;  
  37.         if(this.age==s.age)  
  38.         {  
  39.             return this.name.compareTo(s.name);  
  40.         }  
  41.         return -1;  
  42.         /**/  
  43.     }  
  44.   
  45.     public String getName()  
  46.     {  
  47.         return name;  
  48.   
  49.     }  
  50.     public int getAge()  
  51.     {  
  52.         return age;  
  53.     }  
  54. }  
  55. class TreeSetDemo2   
  56. {  
  57.     public static void main(String[] args)   
  58.     {  
  59.         TreeSet ts = new TreeSet();  
  60.   
  61.         ts.add(new Student("lisi02",22));  
  62.         ts.add(new Student("lisi02",21));  
  63.         ts.add(new Student("lisi007",20));  
  64.         ts.add(new Student("lisi09",19));  
  65.         ts.add(new Student("lisi06",18));  
  66.         ts.add(new Student("lisi06",18));  
  67.         ts.add(new Student("lisi007",29));  
  68.         //ts.add(new Student("lisi007",20));  
  69.         //ts.add(new Student("lisi01",40));  
  70.   
  71.         Iterator it = ts.iterator();  
  72.         while(it.hasNext())  
  73.         {  
  74.             Student stu = (Student)it.next();  
  75.             System.out.println(stu.getName()+"..."+stu.getAge());  
  76.         }  
  77.     }  
  78. }  
  79.   
  80. class MyCompare implements Comparator  
  81. {  
  82.     public int compare(Object o1,Object o2)  
  83.     {  
  84.         Student s1 = (Student)o1;  
  85.         Student s2 = (Student)o2;  
  86.   
  87.         int num = s1.getName().compareTo(s2.getName());  
  88.         if(num==0)  
  89.         {  
  90.   
  91.             return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));  
  92.             /* 
  93.             if(s1.getAge()>s2.getAge()) 
  94.                 return 1; 
  95.             if(s1.getAge()==s2.getAge()) 
  96.                 return 0; 
  97.             return -1; 
  98.             */  
  99.         }  
  100.   
  101.           
  102.         return num;  
  103.   
  104.     }  
  105. }  

TreeSet练习:

[java]  view plain copy
  1. /* 
  2. 练习:按照字符串长度排序。 
  3.  
  4. 字符串本身具备比较性。但是它的比较方式不是所需要的。 
  5.  
  6. 这时就只能使用比较器。 
  7.  
  8.  
  9.  
  10. */  
  11.   
  12. import java.util.*;  
  13. class  TreeSetTest  
  14. {  
  15.     public static void main(String[] args)   
  16.     {  
  17.         TreeSet ts = new TreeSet(new StrLenComparator());  
  18.   
  19.         ts.add("abcd");  
  20.         ts.add("cc");  
  21.         ts.add("cba");  
  22.         ts.add("aaa");  
  23.         ts.add("z");  
  24.         ts.add("hahaha");  
  25.   
  26.         Iterator it = ts.iterator();  
  27.   
  28.         while(it.hasNext())  
  29.         {  
  30.             System.out.println(it.next());  
  31.         }  
  32.     }  
  33. }  
  34.   
  35. class StrLenComparator implements Comparator  
  36. {  
  37.     public int compare(Object o1,Object o2)  
  38.     {  
  39.         String s1 = (String)o1;  
  40.         String s2 = (String)o2;  
  41.   
  42.         /* 
  43.         if(s1.length()>s2.length()) 
  44.             return 1; 
  45.         if(s1.length()==s2.length()) 
  46.             return 0; 
  47.             */  
  48.   
  49.               
  50.   
  51.         int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));  
  52.         if(num==0)  
  53.             return s1.compareTo(s2);  
  54.   
  55.         return num;  
  56.     }  
  57. }  

[java]  view plain copy
  1. /* 
  2. "90 -7 0 18 2 45 4" 
  3.  
  4. 将字符串中的数值进行排序。使用TreeSet完成。 
  5.  
  6. 思路 
  7. 1,将字符串切割。 
  8. 2,可以将这些对象存入TreeSet集合。因为TreeSet自身具备排序功能。 
  9.  
  10.  
  11. */  
  12. import java.util.*;  
  13. class TreeSetTest2   
  14. {  
  15.     public static void main(String[] args)   
  16.     {  
  17.   
  18.         ArrayList al = new ArrayList();  
  19.   
  20.         String str = "90 -7 0 18 2 45 4";  
  21.   
  22.         String[] arr = str.split(" ");  
  23.   
  24.         TreeSet ts = new TreeSet();  
  25.   
  26.         for(int x=0; x<arr.length; x++)  
  27.         {  
  28.             //ts.add(new Integer(arr[x]));  
  29.             ts.add(Integer.parseInt(arr[x]));//  
  30.         }  
  31.   
  32.         System.out.println(ts);  
  33.     }  
  34. }  

泛型:

[java]  view plain copy
  1. import java.util.*;  
  2.   
  3. /* 
  4. 泛型:JDK1.5版本以后出现新特性。用于解决安全问题,是一个类型安全机制。 
  5.  
  6. 好处 
  7. 1.将运行时期出现问题ClassCastException,转移到了编译时期。, 
  8.     方便于程序员解决问题。让运行时问题减少,安全。, 
  9.  
  10. 2,避免了强制转换麻烦。 
  11.  
  12.  
  13. 泛型格式:通过<>来定义要操作的引用数据类型。 
  14.  
  15. 在使用java提供的对象时,什么时候写泛型呢? 
  16.  
  17. 通常在集合框架中很常见, 
  18. 只要见到<>就要定义泛型。 
  19.  
  20. 其实<> 就是用来接收类型的。 
  21.  
  22. 当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。 
  23.  
  24.  
  25. */  
  26.   
  27. class GenericDemo   
  28. {  
  29.     public static void main(String[] args)   
  30.     {  
  31.   
  32.         ArrayList<String> al = new ArrayList<String>();  
  33.   
  34.         al.add("abc01");  
  35.         al.add("abc0991");  
  36.         al.add("abc014");  
  37.   
  38.         //al.add(4);//al.add(new Integer(4));  
  39.           
  40.   
  41.         Iterator<String> it = al.iterator();  
  42.         while(it.hasNext())  
  43.         {  
  44.             String s = it.next();  
  45.   
  46.             System.out.println(s+":"+s.length());  
  47.         }  
  48.     }  
  49. }  

[java]  view plain copy
  1. import java.util.*;  
  2. class GenericDemo2   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         TreeSet<String> ts = new TreeSet<String>(new LenComparator());  
  7.   
  8.         ts.add("abcd");  
  9.         ts.add("cc");  
  10.         ts.add("cba");  
  11.         ts.add("aaa");  
  12.         ts.add("z");  
  13.         ts.add("hahaha");  
  14.   
  15.   
  16.         Iterator<String> it = ts.iterator();  
  17.   
  18.         while(it.hasNext())  
  19.         {  
  20.             String s = it.next();  
  21.             System.out.println(s);  
  22.         }  
  23.     }  
  24. }  
  25.   
  26.   
  27. class LenComparator implements Comparator<String>  
  28. {  
  29.     public int compare(String o1,String o2)  
  30.     {  
  31.         int num = new Integer(o2.length()).compareTo(new Integer(o1.length()));  
  32.   
  33.         if(num==0)  
  34.             return o2.compareTo(o1);  
  35.         return num;  
  36.     }  
  37. }  

[java]  view plain copy
  1. /* 
  2. class Tool 
  3. { 
  4.     private Worker w; 
  5.     public void setWorker(Worker w) 
  6.     { 
  7.         this.w = w; 
  8.     } 
  9.     public Worker getWorker() 
  10.     { 
  11.         return w; 
  12.     } 
  13. } 
  14. */  
  15.   
  16. class Worker  
  17. {  
  18.   
  19. }  
  20. class Student  
  21. {  
  22. }  
  23. //泛型前做法。  
  24. class Tool  
  25. {  
  26.     private Object obj;  
  27.     public void setObject(Object obj)  
  28.     {  
  29.         this.obj = obj;  
  30.     }  
  31.     public Object getObject()  
  32.     {  
  33.         return obj;  
  34.     }  
  35. }  
  36.   
  37. //泛型类。  
  38. /* 
  39. 什么时候定义泛型类? 
  40. 当类中要操作的引用数据类型不确定的时候, 
  41. 早期定义Object来完成扩展。 
  42. 现在定义泛型来完成扩展。 
  43. */  
  44. class Utils<QQ>  
  45. {  
  46.     private QQ q;  
  47.     public void setObject(QQ q)  
  48.     {  
  49.         this.q = q;  
  50.     }  
  51.     public QQ getObject()  
  52.     {  
  53.         return q;  
  54.     }  
  55. }  
  56.   
  57.   
  58. class  GenericDemo3  
  59. {  
  60.     public static void main(String[] args)   
  61.     {  
  62.   
  63.         Utils<Worker> u = new Utils<Worker>();  
  64.   
  65.         u.setObject(new Student());  
  66.         Worker w = u.getObject();;  
  67.         /* 
  68.         Tool t = new Tool(); 
  69.         t.setObject(new Student()); 
  70.         Worker w = (Worker)t.getObject(); 
  71.         */  
  72.     }  
  73. }  

[java]  view plain copy
  1. /* 
  2. class Demo<T> 
  3. { 
  4.     public void show(T t) 
  5.     { 
  6.         System.out.println("show:"+t); 
  7.     } 
  8.     public void print(T t) 
  9.     { 
  10.         System.out.println("show:"+t); 
  11.     } 
  12.  
  13. } 
  14. */  
  15.   
  16. //泛型类定义的泛型,在整个类中有效。如果被方法使用,  
  17. //那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。  
  18. //  
  19. //为了让不同方法可以操作不同类型,而且类型还不确定。  
  20. //那么可以将泛型定义在方法上。  
  21.   
  22.   
  23. /* 
  24. 特殊之处: 
  25. 静态方法不可以访问类上定义的泛型。 
  26. 如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。 
  27.  
  28. */  
  29.   
  30. class Demo<T>  
  31. {  
  32.     public  void show(T t)  
  33.     {  
  34.         System.out.println("show:"+t);  
  35.     }  
  36.     public <Q> void print(Q q)  
  37.     {  
  38.         System.out.println("print:"+q);  
  39.     }  
  40.     public  static <W> void method(W t)  
  41.     {  
  42.         System.out.println("method:"+t);  
  43.     }  
  44. }  
  45. class GenericDemo4   
  46. {  
  47.     public static void main(String[] args)   
  48.     {  
  49.         Demo <String> d = new Demo<String>();  
  50.         d.show("haha");  
  51.         //d.show(4);  
  52.         d.print(5);  
  53.         d.print("hehe");  
  54.   
  55.         Demo.method("hahahahha");  
  56.   
  57.         /* 
  58.         Demo d = new Demo(); 
  59.         d.show("haha"); 
  60.         d.show(new Integer(4)); 
  61.         d.print("heihei"); 
  62.         */  
  63.         /* 
  64.         Demo<Integer> d = new Demo<Integer>(); 
  65.  
  66.         d.show(new Integer(4)); 
  67.         d.print("hah"); 
  68.  
  69.         Demo<String> d1 = new Demo<String>(); 
  70.         d1.print("haha"); 
  71.         d1.show(5); 
  72.         */  
  73.     }  
  74. }  

[java]  view plain copy
  1. //泛型定义在接口上。  
  2. interface Inter<T>  
  3. {  
  4.     void show(T t);  
  5. }  
  6.   
  7. /* 
  8. class InterImpl implements Inter<String> 
  9. { 
  10.     public void show(String t) 
  11.     { 
  12.         System.out.println("show :"+t); 
  13.     } 
  14. } 
  15.  
  16. */  
  17.   
  18. class InterImpl<T> implements Inter<T>  
  19. {  
  20.     public void show(T t)  
  21.     {  
  22.         System.out.println("show :"+t);  
  23.     }  
  24. }  
  25. class GenericDemo5   
  26. {  
  27.     public static void main(String[] args)   
  28.     {  
  29.   
  30.         InterImpl<Integer> i = new InterImpl<Integer>();  
  31.         i.show(4);  
  32.         //InterImpl i = new InterImpl();  
  33.         //i.show("haha");  
  34.     }  
  35. }  

[java]  view plain copy
  1. import java.util.*;  
  2. /* 
  3. ? 通配符。也可以理解为占位符。 
  4. 泛型的限定; 
  5. ? extends E: 可以接收E类型或者E的子类型。上限。 
  6. ? super E: 可以接收E类型或者E的父类型。下限 
  7.  
  8. */  
  9.   
  10.   
  11. class  GenericDemo6  
  12. {  
  13.     public static void main(String[] args)   
  14.     {  
  15.         /* 
  16.         ArrayList<String> al = new ArrayList<String>(); 
  17.  
  18.         al.add("abc1"); 
  19.         al.add("abc2"); 
  20.         al.add("abc3"); 
  21.  
  22.         ArrayList<Integer> al1 = new ArrayList<Integer>(); 
  23.         al1.add(4); 
  24.         al1.add(7); 
  25.         al1.add(1); 
  26.  
  27.         printColl(al); 
  28.         printColl(al1); 
  29.         */  
  30.   
  31.         ArrayList<Person> al = new ArrayList<Person>();  
  32.         al.add(new Person("abc1"));  
  33.         al.add(new Person("abc2"));  
  34.         al.add(new Person("abc3"));  
  35.         //printColl(al);  
  36.   
  37.         ArrayList<Student> al1 = new ArrayList<Student>();  
  38.         al1.add(new Student("abc--1"));  
  39.         al1.add(new Student("abc--2"));  
  40.         al1.add(new Student("abc--3"));  
  41.         printColl(al1);  //ArrayList<? extends Person> al = new ArrayList<Student>();error  
  42.   
  43.     }  
  44.     public static void printColl(Collection<? extends Person> al)  
  45.     {  
  46.         Iterator<? extends Person> it = al.iterator();  
  47.   
  48.   
  49.         while(it.hasNext())  
  50.         {  
  51.             System.out.println(it.next().getName());  
  52.         }  
  53.     }  
  54.     /* 
  55.     public static void printColl(ArrayList<?> al)//ArrayList al = new ArrayList<Integer>();error 
  56.     { 
  57.         Iterator<?> it = al.iterator(); 
  58.  
  59.  
  60.         while(it.hasNext()) 
  61.         { 
  62.             System.out.println(it.next().toString()); 
  63.         } 
  64.     } 
  65.     */  
  66. }  
  67.   
  68. class Person  
  69. {  
  70.     private String name;  
  71.     Person(String name)  
  72.     {  
  73.         this.name = name;  
  74.     }  
  75.     public String getName()  
  76.     {  
  77.         return name;  
  78.     }  
  79. }  
  80.   
  81. class Student extends Person  
  82. {  
  83.     Student(String name)  
  84.     {  
  85.         super(name);  
  86.     }  
  87.   
  88. }  
  89.   
  90.   
  91. /* 
  92.  
  93. class Student implements Comparable<Person>//<? super E> 
  94. { 
  95.     public int compareTo(Person s) 
  96.     { 
  97.         this.getName() 
  98.     } 
  99. } 
  100. */  
  101. class Comp implements Comparator<Person>  
  102. {  
  103.     public int compare(Person s1,Person s2)  
  104.     {  
  105.   
  106.         //Person s1 = new Student("abc1");  
  107.         return s1.getName().compareTo(s2.getName());  
  108.     }  
  109. }  
  110.   
  111. TreeSet<Student> ts = new TreeSet<Student>(new Comp());  
  112. ts.add(new Student("abc1"));  
  113. ts.add(new Student("abc2"));  
  114. ts.add(new Student("abc3"));  

[java]  view plain copy
  1. import java.util.*;  
  2. class GenericDemo7   
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.           
  7.         TreeSet<Student> ts = new TreeSet<Student>(new Comp());  
  8.   
  9.         ts.add(new Student("abc03"));  
  10.         ts.add(new Student("abc02"));  
  11.         ts.add(new Student("abc06"));  
  12.         ts.add(new Student("abc01"));  
  13.           
  14.         Iterator<Student> it = ts.iterator();  
  15.   
  16.         while(it.hasNext())  
  17.         {  
  18.             System.out.println(it.next().getName());  
  19.         }  
  20.         /**/  
  21.   
  22.   
  23.   
  24.         TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());  
  25.   
  26.         ts1.add(new Worker("wabc--03"));  
  27.         ts1.add(new Worker("wabc--02"));  
  28.         ts1.add(new Worker("wabc--06"));  
  29.         ts1.add(new Worker("wabc--01"));  
  30.   
  31.   
  32.         Iterator<Worker> it1 = ts1.iterator();  
  33.   
  34.         while(it1.hasNext())  
  35.         {  
  36.             System.out.println(it1.next().getName());  
  37.         }  
  38.     }  
  39. }  
  40.   
  41. /* 
  42. class StuComp implements Comparator<Student> 
  43. { 
  44.     public int compare(Student s1,Student s2) 
  45.     { 
  46.         return s1.getName().compareTo(s2.getName()); 
  47.     } 
  48. } 
  49.  
  50. class WorkerComp implements Comparator<Worker> 
  51. { 
  52.     public int compare(Worker s1,Worker s2) 
  53.     { 
  54.         return s1.getName().compareTo(s2.getName()); 
  55.     } 
  56. } 
  57. */  
  58.   
  59. class Comp implements Comparator<Person>  
  60. {  
  61.     public int compare(Person p1,Person p2)  
  62.     {  
  63.         return p2.getName().compareTo(p1.getName());  
  64.     }  
  65. }  
  66.   
  67.   
  68. class Person  
  69. {  
  70.     private String name;  
  71.     Person(String name)  
  72.     {  
  73.         this.name = name;  
  74.     }  
  75.     public String getName()  
  76.     {  
  77.         return name;  
  78.     }  
  79.     public String toString()  
  80.     {  
  81.         return "person :"+name;  
  82.     }  
  83. }  
  84.   
  85. class Student extends Person  
  86. {  
  87.     Student(String name)  
  88.     {  
  89.         super(name);  
  90.     }  
  91.   
  92. }  
  93.   
  94. class Worker extends Person  
  95. {  
  96.     Worker(String name)  
  97.     {  
  98.         super(name);  
  99.     }  
  100. }  

[java]  view plain copy
  1. import java.util.*;  
  2. class  GenericTest  
  3. {  
  4.     public static void main(String[] args)   
  5.     {  
  6.         /* 
  7.         ArrayList al = new  ArrayList(); 
  8.         al.add(new Person("heihei")); 
  9.  
  10.         ArrayList al1 = new ArrayList(); 
  11.  
  12.         al1.add("haha1"); 
  13.         al1.add("haha2"); 
  14.  
  15.         al.addAll(al1); 
  16.  
  17.         System.out.println(al);int.next().getName(); 
  18.         */  
  19.   
  20.   
  21.         /**/  
  22.           
  23.         ArrayList<Person> al = new    ArrayList<Person>();  
  24.         al.add(new Person("ahah"));  
  25.   
  26.         ArrayList<Student> al1 = new ArrayList<Student>();  
  27.   
  28.         al1.add(new Student("haha"));  
  29.   
  30.         al.addAll(al1);  
  31.           
  32.   
  33.         Iterator<Person> it = al.iterator();  
  34.   
  35.         while(it.hasNext())  
  36.         {  
  37.             System.out.println(it.next().getName());  
  38.         }  
  39.   
  40.   
  41.   
  42.   
  43.     }  
  44. }  
  45. class Person  
  46. {  
  47.     private String name;  
  48.     Person(String name)  
  49.     {  
  50.         this.name = name;  
  51.     }  
  52.     public String getName()  
  53.     {  
  54.         return name;  
  55.     }  
  56.     public String toString()  
  57.     {  
  58.         return "person :"+name;  
  59.     }  
  60. }  
  61.   
  62. class Student extends Person  
  63. {  
  64.     Student(String name)  
  65.     {  
  66.         super(name);  
  67.     }  
  68.   
  69. }  
  70.   
  71. /* 
  72. Person p = new PErson(); 
  73. p.equals("haha"); 
  74. Demo d = new Demo(); 
  75. d.equals(p); 
  76. */  
  77.   
  78. //泛型搞定:泛型的使用。对于集合类中的泛型会用即可。可以看得懂上限下限,泛型类和泛型方法定义。  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值