成功案例 网站互联网+营销策略怎么写
news/
2025/9/23 10:12:27/
文章来源:
成功案例 网站,互联网+营销策略怎么写,入门做网站,网站策划书基本内容练习1编写Order类#xff0c;有int型的orderId#xff0c;String型的orderName#xff0c;相应的getter()和setter()方法#xff0c;两个参数的构造器#xff0c;
重写父类的equals()方法#xff1a;public boolean equals(Object obj)#xff0c;并判断测试类中创建的两…练习1编写Order类有int型的orderIdString型的orderName相应的getter()和setter()方法两个参数的构造器
重写父类的equals()方法public boolean equals(Object obj)并判断测试类中创建的两个对象是否相等。 package chapter07_oop2.src.com.atguigu07.object.equals.exer1;/*** ClassName: Order* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer1* Description:** Author 小白* Create 2024/4/3 9:20* Version 1.0*/
public class Order {private int orderId;private String orderName;public Order() {}public Order(int orderId, String orderName) {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;}//手写一个equals()Overridepublic boolean equals(Object obj) {if (this obj) { //判断当前this对象和形参obj是否是同一个对象return true;}if (obj instanceof Order) {//判断obj是否是Order 如果是的话 我们就进行强转 好比动物类 在判断动物是否狗//如果是的话 在进行强转 把动物类转换成狗Order order (Order) obj;return this.orderId order.orderId this.orderName.equals(order.orderName);}else {return false;}}
}package chapter07_oop2.src.com.atguigu07.object.equals.exer1;/*** ClassName: OrderTest* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer1* Description:** Author 小白* Create 2024/4/3 9:36* Version 1.0*/
public class OrderTest {public static void main(String[] args) {Order order1 new Order(1001, orderAA);Order order2 new Order(1001, orderAA);System.out.println(order1.equals(order2));//trueOrder order3 new Order(1002, new String(orderBB));Order order4 new Order(1002, new String(orderBB));System.out.println(order3.equals(order4)); //true}}练习2 请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖equals方法
使其判断当两个MyDate类型对象的年月日都相同时结果为true否则为false。
public boolean equals(Object o)public class EqualsTest {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(m1m2);} 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);}}
} 还没有重equals package chapter07_oop2.src.com.atguigu07.object.equals.exer2;/*** ClassName: MyDate* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer2* Description:** Author 小白* Create 2024/4/3 9:57* Version 1.0*/
public class MyDate {private int day;private int month;private int year;public MyDate() {}public MyDate(int day, int month, int year) {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;}
}package chapter07_oop2.src.com.atguigu07.object.equals.exer2;/*** ClassName: EqualsTest* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer2* Description:** Author 小白* Create 2024/4/3 9:57* Version 1.0*/
public class EqualsTest {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(m1m2);} 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);}}}重写后 package chapter07_oop2.src.com.atguigu07.object.equals.exer2;/*** ClassName: MyDate* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer2* Description:** Author 小白* Create 2024/4/3 9:57* Version 1.0*/
public class MyDate {private int day;private int month;private int year;public MyDate() {}public MyDate(int day, int month, int year) {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;}Overridepublic boolean equals(Object obj) {if(this obj){return true;}///如果地址不一样 判断obj是否等于MyDate 如果相等 进行强转 并判断各个属性是否相等if(obj instanceof MyDate){MyDate myDate (MyDate) obj;return this.year myDate.year this.month myDate.month this.day myDate.day;}return false;}
}package chapter07_oop2.src.com.atguigu07.object.equals.exer2;/*** ClassName: EqualsTest* Package: chapter07_oop2.src.com.atguigu07.object.equals.exer2* Description:** Author 小白* Create 2024/4/3 9:57* Version 1.0*/
public class EqualsTest {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(m1m2);} 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//重写equals后 m1 is equal to m2} else {System.out.println(m1 is not equal to m2);}}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912166.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!