集合框架-泛型-泛型限定(下限的体现)
作者:互联网
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 10 import cn.itcast.p2.bean.Person; 11 import cn.itcast.p2.bean.Student; 12 import cn.itcast.p2.bean.Worker; 13 14 public class GenericAdvanceDemo5 { 15 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 TreeSet<Person> al1 = new TreeSet<Person>(); 19 20 21 al1.add(new Person("abc4",34)); 22 al1.add(new Person("abc",30)); 23 al1.add(new Person("abc2",38)); 24 25 TreeSet<Student> al2 = new TreeSet<Student>(); 26 al2.add(new Student("stu1",11)); 27 al2.add(new Student("stu7",20)); 28 al2.add(new Student("stu2",22)); 29 30 TreeSet<Worker> al3 = new TreeSet<Worker>(); 31 al3.add(new Worker("stu1",11)); 32 al3.add(new Worker("stu5",24)); 33 al3.add(new Worker("stu2",22)); 34 35 TreeSet<String> al4 = new TreeSet <String>(); 36 al4.add("abcsaasd"); 37 38 // al1.addAll(al2); 39 // System.out.println(al1.size()); 40 // al1.addAll(al2); 41 // 42 // Iterator<Worker> it = al3.iterator(); 43 // while(it.hasNext()) { 44 // System.out.println(it.next()); 45 // } 46 47 printCollection(al2); 48 49 } 50 51 public static void printCollection(Collection<? super Student> al) { 52 Iterator<? super Student> it = al.iterator(); 53 54 while(it.hasNext()) { 55 System.out.println(it.next()); 56 } 57 } 58 } 59 /* 60 * class TreeSet<E>{ 61 * Tree(Comparator<? super E> comp); 62 * } 63 * 64 * 什么时候用下限呢?通常对集合中的元素进行取出操作时,可以是用下限 65 */ 66 class CompByName implements Comparator<Person>{ 67 68 @Override 69 public int compare(Person o1, Person o2) { 70 // TODO Auto-generated method stub 71 int temp = o1.getName().compareTo(o2.getName()); 72 return temp==0?o1.getAge()-o2.getAge():temp; 73 } 74 75 } 76 class CompByStuName implements Comparator<Student>{//运行的都是父类的东西 77 78 @Override 79 public int compare(Student o1, Student o2) { 80 // TODO Auto-generated method stub 81 int temp = o1.getName().compareTo(o2.getName()); 82 return temp==0?o1.getAge()-o2.getAge():temp; 83 } 84 85 }GenericAdvanceDemo5
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
标签:return,name,限定,age,Person,int,下限,泛型,public 来源: https://www.cnblogs.com/doremi429/p/15452256.html