JAVA泛型使用練習2(自定義泛型使用)

raylu666發表於2017-04-14

自己想了個類似的題目接著寫程式碼,寫到熟練為止

DAO泛型,操作Student和Worker集合,當然也可以應用到更多的物件

然後對集合進行操作

package test10;

import java.util.HashMap;
import java.util.Map;

public class TestDAO {
	public static void main(String[] args) {
		//對泛型類DAO進行操作, 建立Student 泛型物件
		DAO<Student> dao = new DAO<Student>();
		//建立Student map集合
		Map<Integer, Student> map = new HashMap<Integer,Student>();
		dao.setMap(map);
		//對泛型類DAO進行操作, 建立Woker泛型物件
		DAO<Worker> dao1 = new DAO<Worker>();
		Map<Integer, Worker> map2 = new HashMap<Integer,Worker>();
		dao1.setMap(map2);
		//對Student Map操作
		dao.getMap().put(1, new Student("RayLu", 23, "大學一年級"));
		dao.getMap().put(5, new Student("Tom", 32, "大學四年級"));
		dao.getMap().put(3, new Student("Bleach", 24, "大學二年級"));
		dao.getMap().put(4, new Student("Bruce", 25, "大學二年級"));
		//遍歷StudentMap
		for(Integer i: dao.getKey()){
			System.out.println(i+"==="+dao.getValue(i));
		}
		//增加學生
		dao.add(8, new Student("Jason",23,"大學三年級"));
		//刪除學生
		dao.delete(3);
		//修改學生
		dao.modify(5, new Student("Tom", 32, "大學已畢業"));
		//查詢學生
		System.out.println(dao.getValue(5));
		//遍歷學生
		for(Integer i: dao.getKey()){
			System.out.println(i+"==="+dao.getValue(i));
		}
		//對Worker操作和Student操作方法同樣。。
	}
}
DAO類

package test10;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class DAO<T> {
//	private int id;
	//宣告Map變數,其中Key為Integer型別,T為可變型別
	Map<Integer, T> map;
//獲取Map物件
	public Map<Integer, T> getMap(){
		return map;
	}
	//建立Map物件
	public void setMap(Map<Integer, T> map){
		this.map = map;
	}
	//操作Map物件,新增元素
	public void add(int index, T value){
		map.put(index, value);
	}
	//操作Map物件,刪除元素
	public void delete(int index){
		map.remove(index);
	}
	//修改Map制定key的元素
	public void modify(int index, T value){
		map.put(index, value);
	}
	//查詢Map中的元素
	public T getValue(int index){
		return map.get(index);
	}
	//獲取Map中所有Key
	public Set<Integer> getKey(){
		Set<Integer> set = new TreeSet<Integer>();
		for(Integer in:map.keySet()){
			set.add(in);
		}
		return set;
	}
	
	//獲取Map中所有Value
	public List<T> getValue(){
		List<T> list = new ArrayList<T>();
		for(T t: map.values()){
			list.add(t);
		}
		return list;
	}
	
}
Student類

package test10;

public class Student extends Person{
	private String grade;

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	@Override
	public String toString() {
		return "Student [grade=" + grade + ", getName()=" + getName() + ", getAge()=" + getAge() + "]";
	}

	public Student(String name, int age, String grade) {
		super(name, age);
		this.grade = grade;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((grade == null) ? 0 : grade.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (grade == null) {
			if (other.grade != null)
				return false;
		} else if (!grade.equals(other.grade))
			return false;
		return true;
	}
}
Worker類

package test10;

public class Worker extends Person{
private int salary;

public int getSalary() {
	return salary;
}

public void setSalary(int salary) {
	this.salary = salary;
}

@Override
public String toString() {
	return "Worker [salary=" + salary + ", getName()=" + getName() + ", getAge()=" + getAge() + "]";
}

public Worker(String name, int age, int salary) {
	super(name, age);
	this.salary = salary;
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = super.hashCode();
	result = prime * result + salary;
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (!super.equals(obj))
		return false;
	if (getClass() != obj.getClass())
		return false;
	Worker other = (Worker) obj;
	if (salary != other.salary)
		return false;
	return true;
}

}



相關文章