java百天练习题59
题目:
笔试题 :定义MyDate类,在MyDate类中覆盖equals方法,使其判断当两个MyDate类型对象的年月日都相同是,结果为ture,否则为false。
/** day 59
* 笔试题
* 练习1:定义MyDate类,在MyDate类中覆盖equals方法,使其判断当两个MyDate类型对象的年月日都相同是,结果为ture,否则为false。
* */
public class MyDate {
int year;
int month;
int day;
MyDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof MyDate){
MyDate myDate = (MyDate) o;
return year == myDate.year && month == myDate.month && day == myDate.day;
}
return false;
}
}
/** * main 方法 */
public class test {
public static void main(String[] args) {
MyDate m1=new MyDate(2022,5,16);
MyDate m2=new MyDate(2022,5,16);
if(m1==m2){
System.out.println("m1=m2");
}else {
System.out.println("m1!=m2");
}
if (m1.equals(m2)){
System.out.println("true");
System.out.println("m1 is equal to m2");
}else{
System.out.println("false");
System.out.println("m1 is not equal to m2");
}
}
}

浙公网安备 33010602011771号