Home

韧桂

思考,总结

泛型的两个使用练习

  1. 定义一个Employee类, 该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象; 并为每一个属性定义 getter, setter 方法; 并重写 toString 方法输出 name, age, birthday

MyDate类包含: private成员变量month,day,year;并为每一个属性定义 getter, setter 方法;

创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(TreeSet 需使用泛型来定义), 分别按以下两种方式对集合中的元素进行排序,并遍历输出:

1). 使Employee 继承 Comparable 接口,并按 name 排序 2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。

package fourteendemo;
/*
 * MyDate类包含:
private成员变量month,day,year;并为每一个属性定义 getter, setter 方法;

 */
public class MyDate {
	private int day;
	private int month;
	private int year;
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	public MyDate(int day, int month, int year) {
		super();
		this.day = day;
		this.month = month;
		this.year = year;
	}
	@Override
	public String toString() {
		return "MyDate [day=" + day + ", month=" + month + ", year=" + year
				+ "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + day;
		result = prime * result + month;
		result = prime * result + year;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyDate other = (MyDate) obj;
		if (day != other.day)
			return false;
		if (month != other.month)
			return false;
		if (year != other.year)
			return false;
		return true;
	}
	
	
}
package fourteendemo;
/*
 * 定义一个Employee类,
该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday

 */
public class Employee implements Comparable{
	private String name;
	private int age;
	private MyDate birthday;
	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 MyDate getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}
	public Employee(String name, int age, MyDate birthday) {
		super();
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", birthday="
				+ birthday + "]";
	}
	@Override
	public int compareTo(Object o) {
		if(o instanceof Employee){
			Employee e = (Employee)o;
			return this.name.compareTo(e.name);
		}
		return 0;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result
				+ ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (age != other.age)
			return false;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
package fourteendemo;
/*
 * 定义一个Employee类,
该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday

 */
public class Employee1 {
	private String name;
	private int age;
	private MyDate birthday;
	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 MyDate getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}
	public Employee1(String name, int age, MyDate birthday) {
		super();
		this.name = name;
		this.age = age;
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", birthday="
				+ birthday + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result
				+ ((birthday == null) ? 0 : birthday.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee1 other = (Employee1) obj;
		if (age != other.age)
			return false;
		if (birthday == null) {
			if (other.birthday != null)
				return false;
		} else if (!birthday.equals(other.birthday))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
package fourteendemo;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import org.junit.Test;

/*
 * 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:

1). 使Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。

提示:Employee类是否需要重写equals()方法?MyDate类呢?

 */
public class TestEmployee {
	//定制排序:创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。
	@Test
	public void test2(){
		Comparator com = new Comparator(){

			@Override
			public int compare(Object o1, Object o2) {
				if(o1 instanceof Employee1 && o2 instanceof Employee1){
					Employee1 e1 = (Employee1)o1;
					Employee1 e2 = (Employee1)o2;
					MyDate birth1 = e1.getBirthday();
					MyDate birth2 = e2.getBirthday();
					if(birth1.getYear() != birth2.getYear()){
						return birth1.getYear() - birth2.getYear();
					}else{
						if(birth1.getMonth() != birth2.getMonth()){
							return birth1.getMonth() - birth2.getMonth();
						}else{
							return birth1.getDay() - birth2.getDay();
						}
					}
				}
				return 0;
			}
			
		};
		TreeSet<Employee1> set = new TreeSet<>(com);
		Employee1 e1 = new Employee1("刘德华", 55, new MyDate(4, 12, 1976));
		Employee1 e2 = new Employee1("郭富城", 43, new MyDate(7, 3, 1954));
		Employee1 e3 = new Employee1("张学友", 33, new MyDate(9, 12, 1954));
		Employee1 e4 = new Employee1("黎明", 54, new MyDate(12, 3, 1954));
		Employee1 e5 = new Employee1("李敏镐", 65, new MyDate(4, 21, 1945));
		set.add(e1);
		set.add(e2);
		set.add(e3);
		set.add(e4);
		set.add(e5);
		
		Iterator<Employee1> i = set.iterator();
		while(i.hasNext()){
			System.out.println(i.next());
		}
		
	}
	
	//自然排序: 使Employee 实现 Comparable 接口,并按 name 排序
	@Test
	public void test1(){
		Employee e1 = new Employee("刘德华", 55, new MyDate(4, 12, 1976));
		Employee e2 = new Employee("郭富城", 43, new MyDate(7, 3, 1965));
		Employee e3 = new Employee("张学友", 33, new MyDate(9, 12, 1954));
		Employee e4 = new Employee("黎明", 54, new MyDate(12, 2, 1967));
		Employee e5 = new Employee("李敏镐", 65, new MyDate(4, 21, 1945));
//		Employee e6 = new Employee("李敏镐", 63, new MyDate(4, 21, 1945));
		TreeSet<Employee> set = new TreeSet<>();
		set.add(e1);
		set.add(e2);
		set.add(e3);
		set.add(e4);
		set.add(e5);
//		set.add(e6);
		
		Iterator<Employee> i = set.iterator();
		while(i.hasNext()){
			System.out.println(i.next());
		}
	}
}
  1. 定义个泛型类 DAO,在其中定义一个Map 成员变量,Map 的键为 String 类型,值为 T 类型。

分别创建以下方法: public void save(String id,T entity): 保存 T 类型的对象到 Map 成员变量中 T get(String id):从 map 中获取 id 对应的对象 void update(String id,T entity):替换 map 中key为id的内容,改为 entity 对象 List list():返回 map 中存放的所有 T 对象 void delete(String id):删除指定 id 对象

定义一个 User 类: 该类包含:private成员变量(int类型) id,age;(String 类型)name。

创建 DAO 类的对象, 分别调用其 save、get、update、list、delete 方法来操作 User 对象, 使用 Junit 单元测试类进行测试。

package fourteendemo1;
/*
 * 定义一个 User 类:
该类包含:private成员变量(int类型) id,age;(String 类型)name。

 */
public class User {
	private int id;
	private int age;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public User(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
package fourteendemo1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 定义个泛型类 DAO<T>,在其中定义一个Map 成员变量,Map 的键为 String 类型,值为 T 类型。

分别创建以下方法:
public void save(String id,T entity): 保存 T 类型的对象到 Map 成员变量中
T get(String id):从 map 中获取 id 对应的对象
void update(String id,T entity):替换 map 中key为id的内容,改为 entity 对象
List<T> list():返回 map 中存放的所有 T 对象
void delete(String id):删除指定 id 对象

 */
public class DAO<T> {
	Map<String,T> map;
	
	public void delete(String id){
		map.remove(id);
	}
	public List<T> list(){
		List<T> list = new ArrayList<T>();
		for(String s : map.keySet()){
			list.add(map.get(s));
		}
		return list;
	}
	
	public void update(String id,T entity){
		//map.remove(id);
		map.put(id, entity);
	}
	
	public T get(String id){
		return map.get(id);
	}
	public void save(String id,T entity){
		map.put(id, entity);
	}
}
package fourteendemo1;

import java.util.HashMap;
import java.util.List;

/*
 * 创建 DAO 类的对象, 分别调用其 save、get、update、list、delete 方法来操作 User 对象,
使用 Junit 单元测试类进行测试。

 */
public class TestDAO {
	public static void main(String[] args) {
		DAO<User> dao = new DAO<User>();
		
		dao.map = new HashMap<String,User>();
		
		dao.save("1001", new User(1, 32, "梁朝伟"));
		dao.save("1002", new User(2,34,"汤唯"));
		dao.save("1003", new User(3,23,"刘嘉玲"));
		User u = dao.get("1002");//把1002的数据传给 u
		System.out.println(u);
		dao.update("1002", new User(4,45,"成龙"));//替换1002的数据
		dao.delete("1003");//删除1003 的数据
		List<User> list = dao.list();
		System.out.println(list);
		
	}
}

韧桂 2020-01-03