集合框架-泛型-泛型限定(上限的體現)
阿新 • • 發佈:2021-10-24
1 package cn.itcast.p5.generic.advance.demo; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Iterator; 6 7 import com.sun.crypto.provider.PBEKeyFactory.PBEWithSHA1AndRC2_40; 8 9 import cn.itcast.p2.bean.Person; 10 import cn.itcast.p2.bean.Student; 11 importGenericAdvanceDemo4cn.itcast.p2.bean.Worker; 12 13 public class GenericAdvanceDemo4 { 14 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 ArrayList<Person> al1 = new ArrayList<Person>(); 18 19 al1.add(new Person("abc",30)); 20 al1.add(newPerson("abc4",34)); 21 22 ArrayList<Student> al2 = new ArrayList<Student>(); 23 al2.add(new Student("stu1",11)); 24 al2.add(new Student("stu2",22)); 25 26 ArrayList<Worker> al3 = new ArrayList<Worker>(); 27 al3.add(newWorker("stu1",11)); 28 al3.add(new Worker("stu2",22)); 29 30 ArrayList<String> al4 = new ArrayList <String>(); 31 al4.add("abcsaasd"); 32 33 // al1.addAll(al4);//型別不匹配 取出來型別肯定出錯,不知道是轉成Person還是字串,型別會有安全隱患 34 // al1.add(new Person("xixi",20)); 35 36 37 al1.addAll(al2); 38 System.out.println(al1.size()); 39 // printCollection(al3); 40 41 } 42 43 44 } 45 /* 46 * 一般在儲存元素的時候都是用上限,因為這樣取出都是按照上限的型別來運算的,不會出現型別安全隱患。 47 */ 48 class MyCollection<E>{ 49 public void add(E e) { 50 51 } 52 public void addAll(MyCollection<? extends E> e) {//解釋這段,存元素通常用上限。 53 54 } 55 }
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 }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