Main Class
package Comparator.Bean; import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* ? extends E : 接收E或者是E的子类对象。上限
* ? super E :接收E或者E的父类对象。下限
* @author asus
*
*/
public class ComparatorMain {
public static void main(String[] args) {
System.out.println("----------extends---------------");
List<People> peoples = new ArrayList<People>();
peoples.add(new People("Tom",22));
peoples.add(new People("Kitty",18));
peoples.add(new People("Marry",20));
printCollectionExtends(peoples);
List<Worker> workers = new ArrayList<Worker>();
workers.add(new Worker("Programmer", new BigDecimal(20000)));
workers.add(new Worker("teacher", new BigDecimal(6000)));
printCollectionExtends(workers);
List<String> strings = new ArrayList<String>();
strings.add("abc");
strings.add("efg");
strings.add("hij");;
//printCollection(strings);
System.out.println("----------super---------------");
printCollectionSuper(peoples);
List<Student> students = new ArrayList<Student>();
students.add(new Student(87, 96));
students.add(new Student(100, 100));
//printCollectionSuper(students);
List<Human> humans = new ArrayList<Human>();
humans.add(new Human("China", "1991-6-4"));
humans.add(new Human("US", "2000-6-4"));
printCollectionSuper(humans);
//printCollectionExtends(humans);
} public static void printCollectionExtends(Collection<? extends People> collection){
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
} public static void printCollectionSuper(Collection<? super People> collection){
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}
People class
package Comparator.Bean; public class People extends Human{
private String name;
private int age;
private Student student;
private Worker worker; public People() {
super();
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Worker getWorker() {
return worker;
}
public void setWorker(Worker worker) {
this.worker = worker;
} public String toString(){
return "name:"+name+";age:"+age;
} }
Human class
package Comparator.Bean; public class Human {
private String country;
private String birthday; public Human(){} public Human(String country, String birthday) {
this.country = country;
this.birthday = birthday;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
} public String toString(){
return "country:"+country+"birthday:"+birthday;
}
}
Worker class
package Comparator.Bean; import java.math.BigDecimal; public class Worker extends People{
private String occupation;
private BigDecimal salary; public Worker(String occupation, BigDecimal salary) {
this.occupation = occupation;
this.salary = salary;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public String toString(){
return "Occupation:"+occupation+";salary:"+salary;
}
}
Student class
package Comparator.Bean; public class Student extends People{
private int ChineseScores;
private int MathScores; public Student(int chineseScores, int mathScores) {
ChineseScores = chineseScores;
MathScores = mathScores;
}
public int getChineseScores() {
return ChineseScores;
}
public void setChineseScores(int chineseScores) {
ChineseScores = chineseScores;
}
public int getMathScores() {
return MathScores;
}
public void setMathScores(int mathScores) {
MathScores = mathScores;
} }