Java 第15章 泛型 课堂练习+本章作业
2023-12-22 21:17:57
ArrayList中的Comparator
ArrayList要先加入对象再调用sort才能排序……
对birthday的比较,最好是放到MyDate类中,这样比较两个日期大小就可以直接调用MyDate类中重写的compareTo方法即可。具体做法是让MyDate类实现Comparable接口,然后重写其中的compareTo方法
public class Homework03 {
public static void main(String[] args) {
Employee a = new Employee("a", 1, new MyDate(2003, 1, 1));
Employee b = new Employee("a", 2, new MyDate(2001, 2, 1));
Employee c = new Employee("a", 3, new MyDate(2002, 3, 1));
ArrayList<Employee> employees = new ArrayList<>();
employees.add(a);
employees.add(b);
employees.add(c);
employees.sort(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
if (! (o1.getName()).equals(o2.getName()))
return (o1.getName()).compareTo(o2.getName());
else
return ((o1.getDate()).compareTo(o2.getDate()));
}
});
System.out.println(employees);
}
}
class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public int compareTo(MyDate o) {
MyDate myDate2 = o;
if (year == myDate2.getYear()) {
if (month == myDate2.getMonth()) {
return day - myDate2.getDay();
}
return month - myDate2.getMonth();
}
return year - myDate2.getYear();
}
}
class Employee {
private String name;
private double sal;
private MyDate date;
public Employee(String name, double sal, MyDate date) {
this.name = name;
this.sal = sal;
this.date = date;
}
@Override
public String toString() {
return "\nEmployee{" +
"name='" + name + '\'' +
", sal=" + sal +
", date=" + date.toString() +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public MyDate getDate() {
return date;
}
public void setDate(MyDate date) {
this.date = date;
}
}
判断main中是否出现错误
public class CustomGeneric_ {
public static void main(String[] args) {
//T=Double, R=String, M=Integer
Tiger<Double,String,Integer> g = new Tiger<>("john");
g.setT(10.9);
g.setT("yy");
System.out.println(g);
Tiger g2 = new Tiger("john~~");
g2.setT("yy");
System.out.println("g2=" + g2);
}
}
class Tiger<T, R, M> {
String name;
R r; //属性使用到泛型
M m;
T t;
T[] ts;
public Tiger(String name) {
this.name = name;
}
public Tiger(R r, M m, T t) {//构造器使用泛型
this.r = r;
this.m = m;
this.t = t;
}
public Tiger(String name, R r, M m, T t) {//构造器使用泛型
this.name = name;
this.r = r;
this.m = m;
this.t = t;
}
/*
get and set ...
*/
@Override
public String toString() {
return "Tiger{" +
"name='" + name + '\'' +
", r=" + r +
", m=" + m +
", t=" + t +
", ts=" + Arrays.toString(ts) +
'}';
}
}
Tiger g2 = new Tiger(“john~~”); 没错,但要能分析出来,由于<T, R, M>没有被确定所以都是Object类型;
g2.setT(“yy”); 没错,因为 T=Object “yy”=String 是Object子类
public class CustomGeneric_ {
public static void main(String[] args) {
//T=Double, R=String, M=Integer
Tiger<Double,String,Integer> g = new Tiger<>("john");
g.setT(10.9); //OK
// g.setT("yy"); //错误,类型不对
System.out.println(g);
Tiger g2 = new Tiger("john~~");//OK T=Object R=Object M=Object
g2.setT("yy"); //OK ,因为 T=Object "yy"=String 是Object子类
System.out.println("g2=" + g2);
}
}
泛型编程
不难,注意JUnit中Test在测试中的使用。
JUnit是一个Java语言的单元测试框架,多数Java的开发环境都已经集成了JUnit作为单元测试的工具
import org.junit.jupiter.api.Test;
import java.util.*;
public class Homework01 {
public static void main(String[] args) {
}
@Test
public void testList() {
DAO<User> dao = new DAO<>();
// save
dao.save("01", new User(1, 11, "a"));
dao.save("02", new User(2, 22, "b"));
dao.save("03", new User(3, 33, "c"));
// list
List<User> list = dao.list();
System.out.println(list);
// delete
dao.delete("01");
list = dao.list();
System.out.println(list);
// update
dao.update("02", new User(12, 32, "bb"));
list = dao.list();
System.out.println(list);
// get
System.out.println(dao.get("03"));
}
}
class User {
private int id;
private int age;
private String name;
public User(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
class DAO<T> {
private Map<String, T> map = new HashMap<>();
public void save(String id, T entity) {
map.put(id, entity);
}
public T get(String id) {
return (T) map.get(id);
}
public void update(String id, T entity) {
map.put(id, entity);
}
public List<T> list() {
List<T> list = new ArrayList<>();
Set<Map.Entry<String, T>> entries = map.entrySet();
Iterator<Map.Entry<String, T>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, T> entry = iterator.next();
list.add(entry.getValue());
}
return list;
}
public void delete(String id) {
map.remove(id);
}
}
文章来源:https://blog.csdn.net/Winnie_deer/article/details/135142556
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!