集合框架-泛型-泛型上限
1 package cn.itcast.p5.generic.advance.demo; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.LinkedList; 8 import java.util.List; 9 10 public class GenericAdvanceDemo { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 ArrayList<String> al = new ArrayList<String>(); 15 16 al.add("abc"); 17 al.add("hehe"); 18 19 LinkedList<Integer> al2 = new LinkedList<Integer>(); 20 21 al.add("5"); 22 al.add("6"); 23 24 HashSet<String> al3 = new HashSet<String>(); 25 26 al.add("abc2"); 27 al.add("hehe2"); 28 29 printCollection(al); 30 printCollection(al2); 31 printCollection(al3); 32 33 34 } 35 36 /** 37 * 迭代并打印集合中元素 38 * @param al 39 */ 40 public static void printCollection(Collection<?> al) {//Collecton可以接收所有集合 41 42 Iterator<?> it = al.iterator(); 43 44 while(it.hasNext()) { 45 // String str = it.next(); 46 // System.out.println(str); 47 System.out.println(it.next()); 48 } 49 } 50 /* 51 * public static <T> T printCollection(Collection<T> al) {// Iterator<T> it = 52 * al.iterator(); 53 * 54 * while(it.hasNext()) { // T str = 55 * it.next();//虽然这个和Collection加?区别不大,但是这里的T能代表一个具体的类型可被访问 // 56 * System.out.println(str); // System.out.println(it.next()); T t = it.next(); 57 * return t;//可能返回值会用到T,Collection加?用于无此返回值 } } 58 */ 59 60 }
1 package cn.itcast.p5.generic.advance.demo; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.LinkedList; 8 import java.util.List; 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 GenericAdvanceDemo2 { 15 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 ArrayList<Worker> al = new ArrayList<Worker>(); 19 20 al.add(new Worker("abc",30)); 21 al.add(new Worker("abc4",34)); 22 23 ArrayList<Student> al2 = new ArrayList<Student>(); 24 al2.add(new Student("stu1",11)); 25 al2.add(new Student("stu2",22)); 26 27 ArrayList<String> al3 = new ArrayList<String>(); 28 al3.add("stu3331"); 29 al3.add("stu33332"); 30 31 32 printCollection(al); 33 printCollection(al2); 34 // printCollection(al3); 35 36 37 38 39 } 40 41 /** 42 * 迭代并打印集合中元素。 43 * 44 * 可以对类型进行限定: 45 * ? extends E:接收E类型或者E的子类型对象。上限! 46 * 47 * ? super E:接收E类型或者E的父类型。下限! 48 * 49 * 50 * @param al 51 */ 52 public static void printCollection(Collection<? extends Person > al) {//Collecton可以接收所有集合 53 //Collection<Person> al = new ArrayList<Student>(); 54 //不行泛型不匹配 55 Iterator<? extends Person > it = al.iterator();//取得时候泛型限定,可以调用父类的方法 56 57 while(it.hasNext()) { 58 // String str = it.next(); 59 // System.out.println(str); 60 // System.out.println(it.next()); 61 Person p = it.next(); 62 System.out.println(p.getName()+":"+p.getAge()); 63 } 64 } 65 /* 66 * public static <T> T printCollection(Collection<T> al) {// Iterator<T> it = 67 * al.iterator(); 68 * 69 * while(it.hasNext()) { // T str = 70 * it.next();//虽然这个和Collection加?区别不大,但是这里的T能代表一个具体的类型可被访问 // 71 * System.out.println(str); // System.out.println(it.next()); T t = it.next(); 72 * return t;//可能返回值会用到T,Collection加?用于无此返回值 } } 73 */ 74 75 }
1 package cn.itcast.p2.bean; 2 3 import java.util.Comparator; 4 5 public class Person implements Comparable<Person>{ 6 private String name; 7 private int age; 8 9 10 public Person() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public Person(String name, int age) { 16 super(); 17 this.name = name; 18 this.age = age; 19 } 20 public int compareTo(Person p) {//指定比较类型,传入比较类型 21 // Person p= (Person)obj; 22 int temp = this.age-p.age; 23 return temp==0?this.name.compareTo(p.name):temp; 24 } 25 26 27 // @Override 28 // public boolean equals(Object obj) {//不能把Object改为Person,equals方法来自Object,Object没有定义泛型 29 // // TODO Auto-generated method stub 30 // if (this == obj) { 31 // return true; 32 // } 33 // if (!(obj instanceof Person)) { 34 // throw new RuntimeException() 35 // } 36 // Person p = (Person)obj; 37 // return super.equals(obj); 38 // } 39 /* 40 * @Override public int hashCode() { final int prime = 31; int result = 1; 41 * result = prime * result + age; result = prime * result + ((name == null) ? 0 42 * : name.hashCode()); return result; } 43 * 44 * @Override public boolean equals(Object obj) { if (this == obj) return true; 45 * if (obj == null) return false; if (getClass() != obj.getClass()) return 46 * false; Person other = (Person) obj; if (age != other.age) return false; if 47 * (name == null) { if (other.name != null) return false; } else if 48 * (!name.equals(other.name)) return false; return true; }//alt+shift+s 49 * hashCode和equals 50 */ 51 public String getName() { 52 return name; 53 } 54 public void setName(String name) { 55 this.name = name; 56 } 57 public int getAge() { 58 return age; 59 } 60 public void setAge(int age) { 61 this.age = age; 62 } 63 64 }
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 "Worker:"+getName()+":"+getAge(); 19 } 20 21 }
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 }
见集合框架-泛型TXT