1. Write the Order class, there are int-type orderId, String-type orderName, the corresponding getter () and setter () methods, the constructor of two parameters, override the equals () method of the parent class: public boolean equals(Object obj), and determine whether the two objects created in the test class are equal.
package test2; public class TestOrder { public static void main(String[] args) { Order o1 = new Order(1000,"AAA"); Order o2 = new Order(1000,"AAA"); System.out.println(o1==o2); System.out.println(o1.equals(o2)); } } class Order{ private int orderld; private String Oldername; public Order(int orderld, String oldername) { super(); this.orderld = orderld; Oldername = oldername; } public int getOrderld() { return orderld; } public void setOrderld(int orderld) { this.orderld = orderld; } public String getOldername() { return Oldername; } public void setOldername(String oldername) { Oldername = oldername; } public boolean equals(Object obj) { if(this==obj) { return true; }else if(obj instanceof Order) { Order o=(Order)obj; return this.orderld==o.orderld && this.Oldername==o.Oldername; }else { return false; } } }
Test results:
2. Please define MyDate class according to the following code, and override the equals method in MyDate class, so that it can judge that when two MyDate objects have the same year, month and day, the result is true, otherwise false, public boolean equals (Object o)
package test2; public class TestMyDate { public static void main(String[] args) { MyDate m1 = new MyDate(2,8,2019); MyDate m2 = new MyDate(2,8,2019); if(m1==m2) { System.out.println("m1==m2"); }else { System.out.println("m1!=m2"); } if(m1.equals(m2)) { System.out.println("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; } } }
Test results: