1. 程式人生 > >泛型固定上邊界和泛型固定下邊界

泛型固定上邊界和泛型固定下邊界

 泛型固定下邊界
 ? super E


類 TreeSet<E>構造方法
 public TreeSet(Collection<? extends E> c)
 public TreeSet(Comparator<? super E> comparator)
類 TreeMap<K,V>構造方法
public TreeMap(Comparator<? super K> comparator)
 public TreeMap(Map<? extends K,? extends V> m)



泛型固定上邊界
? extends E


 介面 Collection<E>方法
boolean addAll(Collection<? extends E> c)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
import bean.Person;
import bean.Student;
public class k {

	/**
	 * person是student的子類
	 */
	public static void main(String[] args) {
		
		ArrayList<Student> list1 = new ArrayList<>();
		list1.add(new Student("張三", 23));
		list1.add(new Student("李四", 24));
		
		ArrayList<Person> list2 = new ArrayList<>();
		list2.add(new Person("王五", 25));
		list2.add(new Person("趙六", 26));
		
		list1.addAll(list2);//可以把子類物件新增到父類集合中區
		System.out.println(list1);
		
		
		TreeSet<Student> ts1 = new TreeSet<>(new CompareByAge());
		ts1.add(new Student("張三", 33));
		ts1.add(new Student("李四", 13));
		ts1.add(new Student("王五", 23));
		ts1.add(new Student("趙六", 43));
		
		TreeSet<Person> ts2 = new TreeSet<>(new CompareByAge());
		ts2.add(new Person("張三", 33));
		ts2.add(new Person("李四", 13));
		ts2.add(new Person("王五", 23));
		ts2.add(new Person("趙六", 43));
		//比較器比較的是student物件,為什麼student的子類person也可以比較
		System.out.println(ts2);
		
		
		
	}

}
//比較器做成之後可以放入student及其子類
class CompareByAge implements Comparator<Student> {

	@Override
	public int compare(Student s1, Student s2) {
		int num = s1.getAge() - s2.getAge();
		return num == 0 ? s1.getName().compareTo(s2.getName()) :  num;
	}
	
}
student
package bean;

public class Student implements Comparable<Student> {
	
	String name;
	int age;
	
	public Student() {
		super();
		
	}
	public Student(String name, int age) {
		super();
		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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	@Override
	public int compareTo(Student obj) {
		// TODO Auto-generated method stub
		//return 0;
		//根元素為13 ,傳入的為23,23這個物件呼叫 compareTo方法 23-13》0,在13的右邊
		//return (this.age - obj.age);
		int num = this.age - obj.age;//age是主要判據
		//string裡面重寫了compareto方法,按字典順序
		return num == 0 ? this.name.compareTo(obj.name) : num;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
		
} 

person

package bean;

public class Person extends Student {
	public Person() {
		super();	
	}
	public Person(String name, int age) {
		super(name ,age);
	}
}