集合框架-泛型-泛型限定(下限的體現)
阿新 • • 發佈:2021-10-24
1 package cn.itcast.p5.generic.advance.demo; 2 3 import java.util.TreeSet; 4 import java.util.Collection; 5 import java.util.Comparator; 6 import java.util.Iterator; 7 import java.util.TreeSet; 8 9 import com.sun.crypto.provider.PBEKeyFactory.PBEWithSHA1AndRC2_40; 10 11 import cn.itcast.p2.bean.Person;GenericAdvanceDemo512 import cn.itcast.p2.bean.Student; 13 import cn.itcast.p2.bean.Worker; 14 15 public class GenericAdvanceDemo5 { 16 17 public static void main(String[] args) { 18 // TODO Auto-generated method stub 19 TreeSet<Person> al1 = new TreeSet<Person>(new CompByName()); 20 2122 al1.add(new Person("abc4",34)); 23 al1.add(new Person("abc",30)); 24 al1.add(new Person("abc2",38)); 25 26 TreeSet<Student> al2 = new TreeSet<Student>(new CompByStuName()); 27 al2.add(new Student("stu1",11)); 28 al2.add(newStudent("stu7",20)); 29 al2.add(new Student("stu2",22)); 30 31 TreeSet<Worker> al3 = new TreeSet<Worker>(new CompByName()); 32 al3.add(new Worker("stu1",11)); 33 al3.add(new Worker("stu5",24)); 34 al3.add(new Worker("stu2",22)); 35 36 TreeSet<String> al4 = new TreeSet <String>(); 37 al4.add("abcsaasd"); 38 39 // al1.addAll(al2); 40 // System.out.println(al1.size()); 41 al1.addAll(al2); 42 43 Iterator<Worker> it = al3.iterator(); 44 while(it.hasNext()) { 45 System.out.println(it.next()); 46 } 47 48 49 } 50 51 52 } 53 /* 54 * class TreeSet<E>{ 55 * Tree(Comparator<? super E> comp); 56 * } 57 * 58 * 什麼時候用下限呢?通常對集合中的元素進行取出操作時,可以是用下限 59 */ 60 class CompByName implements Comparator<Person>{ 61 62 @Override 63 public int compare(Person o1, Person o2) { 64 // TODO Auto-generated method stub 65 int temp = o1.getName().compareTo(o2.getName()); 66 return temp==0?o1.getAge()-o2.getAge():temp; 67 } 68 69 } 70 class CompByStuName implements Comparator<Student>{//執行的都是父類的東西 71 72 @Override 73 public int compare(Student o1, Student o2) { 74 // TODO Auto-generated method stub 75 int temp = o1.getName().compareTo(o2.getName()); 76 return temp==0?o1.getAge()-o2.getAge():temp; 77 } 78 79 }
1 package cn.itcast.p2.bean; 2 3 public class Person implements Comparable<Person>{ 4 private String name; 5 private int age; 6 7 8 public Person() { 9 super(); 10 // TODO Auto-generated constructor stub 11 } 12 13 public Person(String name, int age) { 14 super(); 15 this.name = name; 16 this.age = age; 17 } 18 public int compareTo(Person p) {//指定比較型別,傳入比較型別 19 // Person p= (Person)obj; 20 int temp = this.age-p.age; 21 return temp==0?this.name.compareTo(p.name):temp; 22 } 23 24 25 // @Override 26 // public boolean equals(Object obj) {//不能把Object改為Person,equals方法來自Object,Object沒有定義泛型 27 // // TODO Auto-generated method stub 28 // if (this == obj) { 29 // return true; 30 // } 31 // if (!(obj instanceof Person)) { 32 // throw new RuntimeException() 33 // } 34 // Person p = (Person)obj; 35 // return super.equals(obj); 36 // } 37 /* 38 * @Override public int hashCode() { final int prime = 31; int result = 1; 39 * result = prime * result + age; result = prime * result + ((name == null) ? 0 40 * : name.hashCode()); return result; } 41 * 42 * @Override public boolean equals(Object obj) { if (this == obj) return true; 43 * if (obj == null) return false; if (getClass() != obj.getClass()) return 44 * false; Person other = (Person) obj; if (age != other.age) return false; if 45 * (name == null) { if (other.name != null) return false; } else if 46 * (!name.equals(other.name)) return false; return true; }//alt+shift+s 47 * hashCode和equals 48 */ 49 public String getName() { 50 return name; 51 } 52 public void setName(String name) { 53 this.name = name; 54 } 55 public int getAge() { 56 return age; 57 } 58 public void setAge(int age) { 59 this.age = age; 60 } 61 62 @Override 63 public String toString() { 64 return "Person:"+getName()+":"+getAge(); 65 } 66 67 68 }Person
1 package cn.itcast.p2.bean; 2 3 public class Student extends Person { 4 5 public Student() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public Student(String name, int age) { 11 super(name, age); 12 // TODO Auto-generated constructor stub 13 } 14 15 @Override 16 public String toString() { 17 // TODO Auto-generated method stub 18 return "Student:"+getName()+":"+getAge(); 19 } 20 21 }Student
1 package cn.itcast.p2.bean; 2 3 public class Worker extends Person { 4 5 public Worker() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public Worker(String name, int age) { 11 super(name, age); 12 // TODO Auto-generated constructor stub 13 } 14 15 @Override 16 public String toString() { 17 // TODO Auto-generated method stub 18 return "Worker:"+getName()+":"+getAge(); 19 } 20 21 22 }Worker