package top.qaqaq.java.P567;
/**
* 定义一个 Employee 类。
* 该类包含:private 成员变量 name,age,birthday,其中 birthday 为MyDate 类的对象;
* 并为每一个属性定义 getter, setter 方法;
* 并重写 toString 方法输出 name, age, birthday
*
* @author RichieZhang
* @create 2022-10-27 下午 3:08
*/
public class Employee implements Comparable<Employee> {
private String name;
private int age;
private MyDate birthday;
public Employee() {
}
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = 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;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
//指明泛型时的写法
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
//没有指明泛型时的写法
//按 name 排序
// @Override
// public int compareTo(Object o) {
// if (o instanceof Employee){
// Employee e = (Employee) o;
// return this.name.compareTo(e.name);
// }
//// return 0;
// throw new RuntimeException("传入的数据类型不一致!");
// }
}
package top.qaqaq.java.P567;
/**
* MyDate 类包含:
* private 成员变量 year,month,day;并为每一个属性定义 getter, setter方法;
*
* @author RichieZhang
* @create 2022-10-27 下午 3:07
*/
public class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.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 String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
// @Override
// public int compareTo(Object o) {
// if (o instanceof MyDate){
// MyDate m = (MyDate) o;
//
// //比较年
// int minusYear = this.getYear() - m.getYear();
// if (minusYear != 0){
// return minusYear;
// }
// //比较月
// int minusMouth = this.getMonth() - m.getMonth();
// if (minusMouth != 0){
// return minusMouth;
// }
// //比较日
// return this.getDay() - m.getDay();
// }
//
// throw new RuntimeException("传入的数据类型不一致!");
// }
@Override
public int compareTo(MyDate m) {
//比较年
int minusYear = this.getYear() - m.getYear();
if (minusYear != 0){
return minusYear;
}
//比较月
int minusMouth = this.getMonth() - m.getMonth();
if (minusMouth != 0){
return minusMouth;
}
//比较日
return this.getDay() - m.getDay();
}
}
package top.qaqaq.java.P567;
/**
* MyDate 类包含:
* private 成员变量 year,month,day;并为每一个属性定义 getter, setter方法;
*
* @author RichieZhang
* @create 2022-10-27 下午 3:07
*/
public class MyDate implements Comparable<MyDate>{
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.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 String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
// @Override
// public int compareTo(Object o) {
// if (o instanceof MyDate){
// MyDate m = (MyDate) o;
//
// //比较年
// int minusYear = this.getYear() - m.getYear();
// if (minusYear != 0){
// return minusYear;
// }
// //比较月
// int minusMouth = this.getMonth() - m.getMonth();
// if (minusMouth != 0){
// return minusMouth;
// }
// //比较日
// return this.getDay() - m.getDay();
// }
//
// throw new RuntimeException("传入的数据类型不一致!");
// }
@Override
public int compareTo(MyDate m) {
//比较年
int minusYear = this.getYear() - m.getYear();
if (minusYear != 0){
return minusYear;
}
//比较月
int minusMouth = this.getMonth() - m.getMonth();
if (minusMouth != 0){
return minusMouth;
}
//比较日
return this.getDay() - m.getDay();
}
}