本文主要是介绍java练习题--笔试题 :定义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");
}
}
}
这篇关于java练习题--笔试题 :定义MyDate类,在MyDate类中覆盖equals方法,使其判断当两个MyDate类型对象的年月日都相同是,结果为ture,否则为false。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!