Object类的练习(equals,toString)

本文详细讲解了如何使用Object类进行实例比较,涉及数值、字符、字符串以及自定义类的equals方法。通过Circle类和GeometricObject子类实例,探讨对象相等性判断,包括颜色、半径和toString方法应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Object 类的使用
int it = 65;
float fl = 65.0f;
System.out.println(6565.0f是否相等?” + (it == fl)); //1

char ch1 = 'A'; char ch2 = 12;
System.out.println("65和'A'是否相等?" + (it == ch1));//2
System.out.println(12和ch2是否相等?" + (12 == ch2));//3

String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1和str2是否相等?"+ (str1 == str2));//4

System.out.println("str1是否equals str2?"+(str1.equals(str2)));//5

System.out.println(“hello” == new java.util.Date()); //6

答案:
1.相等
2.相等
3.相等
4.不相等
5.是
6.报错

2.编写Order类,有int型的orderId,String型的orderName,相应的getter()和setter()方法,两个参数的构造器,重写父类的equals()方法:public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。
package com.lhc.objectexer1;

public class Order {
	private int orderId;
	private String orderName;
	public Order() {
		
	}
	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;
	}
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		boolean flag = false;	//先给一个初始化的值,flag是标识位
		if(obj instanceof Order) {//判断obj是不是Order类
			Order o = (Order) obj;//如果是,把obj强转成Order类型
			if(this.orderId == o.orderId && this.orderName.equals(o.orderName)) {
			//比较当前Order类的orderId与传入的orderId是否相等,String类型的orderName则用equals()方法比较值
				flag = true;
			}
		}
		
		return flag;
	}
}


package com.lhc.objectexer1;

public class OrderTest {
	public static void main(String[] args) {
		Order or1 = new Order(1,"zjw");
		Order or2 = new Order(1,"zjw");
		System.out.println(or1.equals(or2));
	}
	
}

在这里插入图片描述

3.请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖equals方法,使其判断当两个MyDate类型对象的年月日都相同时,结果为true,否则为false。 public boolean equals(Object o)
public class EqualsTest {
    public static void main(String[] args) {
        MyDate m1 = new MyDate(2021,2,22);
        MyDate m2 = new MyDate(2021,2,22);
        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");
        }
    }
}
package com.lhc.objectexer1;

public class MyDate {
	int year,month,day;
	
	
	public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}


	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		if(obj instanceof MyDate) {
			//向下转型
			MyDate md = (MyDate) obj;
			return md.year == this.year && md.month == this.month && md.day == this.day;
		}
		return false;
	}
	
}

4.
public void test() {
    char[] arr = new char[] { 'a', 'b', 'c' };
    System.out.println(arr);//1

    int[] arr1 = new int[] { 1, 2, 3 };
    System.out.println(arr1);//2

    double[] arr2 = new double[] { 1.1, 2.2, 3.3 };
    System.out.println(arr2);//3
}

在这里插入图片描述

5.定义两个类,父类GeometricObject代表几何形状,子类Circle代表圆形。

在这里插入图片描述
在这里插入图片描述

写一个测试类,创建两个Circle对象,判断其颜色是否相等;利用equals方法判断其半径是否相等;利用toString()方法输出其半径。

GeometricObject.java

package com.lhc.objectexer1;

public class GeometricObject {
	protected String color;
	protected  double  weight;
	
	protected GeometricObject() {
		this.color = "white";
		this.weight = 1.0;
	}

	protected GeometricObject(String color, double weight) {
		super();
		this.color = color;
		this.weight = weight;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}
	
	
	
}

Circle.java

package com.lhc.objectexer1;

public class Circle extends GeometricObject {
	private double radius;

	public Circle() {
		super();
		this.radius = 1.0;
	}

	public Circle(double radius) {
		super();
		this.radius = radius;
	}

	public Circle(double radius,String color,double weight) {
		super();
		this.radius = radius;
		this.color = color;
		this.weight = weight;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double findArea() {
		return Math.PI * radius *radius;
	}
	
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		if(obj instanceof Circle) {
			Circle c = (Circle) obj;
			return c.radius == this.radius;
		}
		return false;
	}
	
	public String toString() {
		return "radius="+radius;
	}
	
	
}

CircleTest.java

package com.lhc.objectexer1;

public class CircleTest {
	public static void main(String[] args) {
		Circle c1 = new Circle(3,"red",6);
		Circle c2 = new Circle(3,"red",6);
		System.out.println(c1.color == c2.color);
		System.out.println(c1.equals(c2));
		System.out.println(c1.toString());
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沿辰ᓂ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值