equals练习

这篇博客探讨了Java中equals()方法的重写,通过Order类和MyDate类的示例展示了如何正确比较对象的相等性。在Order类中,equals()方法检查订单ID和订单名称是否相同来确定对象是否相等;而在MyDate类中,方法同样检查日期、月份和年份的对应属性。这两个示例突显了在自定义类中覆盖equals()方法的重要性,以确保正确比较对象实例。
摘要由CSDN通过智能技术生成

equals练习


package com.atguigu.exer2;
/*
 * 1.编写Order类,有int型的orderId,String型的orderName,相应的
	getter()和setter()方法,两个参数的构造器,重写父类的equals()方法:
	public boolean equals(Object obj),并判断测试类中创建的两个对象是否
	相等。
 */
public class OrderTest
{
	public static void main(String[] args)
	{
		Order order1 = new Order(1001, "AA");
		Order order2 = new Order(1001, "BB");
		System.out.println(order1.equals(order2)); //false
		Order order3 = new Order(1001, "BB");
		System.out.println(order2.equals(order3)); //true		
	}
}

class Order{
	private int id;
	private String orderName;
	
	public int getId()	{
		return id;
	}
	public void setId(int id)	{
		this.id = id;
	}
	public String getOrderName()	{
		return orderName;
	}
	public void setOrderName(String orderName)	{
		this.orderName = orderName;
	}
	
	public Order(int id, String orderName)	{
		super();
		this.id = id;
		this.orderName = orderName;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(this == obj) {
			return true;
		}
		if(obj instanceof Order) {
			Order order = (Order)obj;
			return this.id == order.id && this.orderName.equals(order.orderName);
		}
		return false;
	}	

	
}


package com.atguigu.exer2;

public class MyDateTest
{
	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;
	}

/*	@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;
	}			*/
	@Override
	public boolean equals(Object obj)
	{
		if(this == obj) {
			return true;
		}
		if(obj instanceof MyDate) {
			MyDate mydate = (MyDate)obj;
			return this.day == mydate.day && this.month == mydate.month && this.year == mydate.year;
		}
		return false;
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值