本文主要是介绍Java 第15章 泛型 课堂练习+本章作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- ArrayList中的Comparator
- 判断main中是否出现错误
- 泛型编程
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>() {@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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=IntegerTiger<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 ...*/@Overridepublic 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=IntegerTiger<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=Objectg2.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) {}@Testpublic void testList() {DAO<User> dao = new DAO<>();// savedao.save("01", new User(1, 11, "a"));dao.save("02", new User(2, 22, "b"));dao.save("03", new User(3, 33, "c"));// listList<User> list = dao.list();System.out.println(list);// deletedao.delete("01");list = dao.list();System.out.println(list);// updatedao.update("02", new User(12, 32, "bb"));list = dao.list();System.out.println(list);// getSystem.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;}@Overridepublic 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);}
}
这篇关于Java 第15章 泛型 课堂练习+本章作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!