equals()方法练习
一
package nine;
/**
* 1.编写Order类,有int型的orderId,String型的OrderName,相应的getter()和setter()方法,两个参数的构造器,
* 重写父类的equals()方法:public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。
*/
public class TestOrder {
public static void main(String[] args) {
Order o1 = new Order(1001, "AA");
Order o2 = new Order(1001, "AA");
System.out.println(o1 == o2);// false
System.out.println(o1.equals(o2));// false --->true
}
}
class Order {
private int orderId;
private String orderName;
public Order(int orderId, String orderName) {
super();
this.orderId = orderId;
this.orderName = orderName;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
//比较两个 Order 对象的属性是否完全相同。相同的话,返回 true。
// public boolean equals(Object obj) {
// if (this == obj) {
// return true;
// } else if (obj instanceof Order) {
// Order o1 = (Order) obj;
// return this.orderId == o1.orderId
// && this.orderName.equals(o1.orderName);
// } else {
// return false;
// }
// }
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Order other = (Order) obj;
if (orderId != other.orderId)
return false;
if (orderName == null) {
if (other.orderName != null)
return false;
} else if (!orderName.equals(other.orderName))
return false;
return true;
}
}
二
package nine;
/**
2.请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖equals方法,
使其判断当两个MyDate类型对象的年月日都相同时,结果为true,否则为false。
public boolean equals(Object o)
*/
public class TestMyDate {
public static void main(String[] args) {
MyDate m1 = new MyDate(14, 3, 1976);
MyDate m2 = new MyDate(14, 3, 1976);
if (m1 == m2) {
System.out.println("m1 == m2");
} else {
System.out.println("m1 != m2"); // m1 != m2
}
if (m1.equals(m2)) {
System.out.println("m1 is equal to m2");
// m1 is equal to m2
} else {
System.out.println("m1 is not equal to m2");
}
}
}
class MyDate {
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//手动生成的
// public boolean equals(Object obj) {
// if (this == obj) {
// return true;
// } else if (obj instanceof MyDate) {
// MyDate m = (MyDate) obj;
// return this.day == m.day && this.month == m.month
// && this.year == m.year;
// } else {
// return false;
// }
// }
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate other = (MyDate) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
韧桂 2019-12-26