集合框架-泛型-泛型下限
阿新 • • 發佈: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 import cn.itcast.p2.bean.Person; 7 import cn.itcast.p2.bean.Student; 8 9 public class GenericAdvanceDemo3 { 10 11 public static void main(String[] args) {GenericAdvanceDemo312 // TODO Auto-generated method stub 13 ArrayList<Person> al = new ArrayList<Person>(); 14 15 al.add(new Person("abc",30)); 16 al.add(new Person("abc4",34)); 17 18 ArrayList<Student> al2 = new ArrayList<Student>(); 19 al2.add(newStudent("stu1",11)); 20 al2.add(new Student("stu2",22)); 21 22 ArrayList<String> al3 = new ArrayList<String>(); 23 al3.add("stu3331"); 24 al3.add("stu33332"); 25 26 27 printCollection2(al); 28 printCollection2(al2); 29 // printCollection(al3);30 31 32 33 34 } 35 36 /** 37 * 迭代並列印集合中元素。 38 * 39 * 可以對型別進行限定: 40 * ? extends E:接收E型別或者E的子型別物件。上限! 41 * 42 * ? super E:接收E型別或者E的父型別。下限! 43 * 44 * 45 * @param al 46 */ 47 // public static void printCollection(Collection<? extends Person > al) {//Collecton可以接收所有集合 48 // //Collection<Person> al = new ArrayList<Student>(); 49 // //不行泛型不匹配 50 // Iterator<? extends Person > it = al.iterator();//取得時候泛型限定,可以呼叫父類的方法 51 // 52 // while(it.hasNext()) { 53 //// String str = it.next(); 54 //// System.out.println(str); 55 //// System.out.println(it.next()); 56 // Person p = it.next(); 57 // System.out.println(p.getName()+":"+p.getAge()); 58 // } 59 // } 60 /* 61 * public static <T> T printCollection(Collection<T> al) {// Iterator<T> it = 62 * al.iterator(); 63 * 64 * while(it.hasNext()) { // T str = 65 * it.next();//雖然這個和Collection加?區別不大,但是這裡的T能代表一個具體的型別可被訪問 // 66 * System.out.println(str); // System.out.println(it.next()); T t = it.next(); 67 * return t;//可能返回值會用到T,Collection加?用於無此返回值 } } 68 */ 69 public static void printCollection2(Collection<? super Student> al) { 70 Iterator<? super Student > it = al.iterator();//取得時候泛型限定,可以呼叫父類的方法 71 72 while(it.hasNext()) { 73 // String str = it.next(); 74 // System.out.println(str); 75 // System.out.println(it.next()); 76 // Person p = it.next(); 77 // System.out.println(p.getName()+":"+p.getAge()); 78 System.out.println(it.next()); 79 } 80 81 } 82 83 84 85 }
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