圆 equals


圆 equals


父类

package com.atguigu.exer3;
/**
 * 
 * @Description  圆的类
 * @author LeungQiChen Email:1256469594@qq.com
 * @version
 * @date 2021年7月19日上午12:53:39
 */
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(String color, double weight, double radius)	{
		super(color, weight);
		this.radius = radius;
	}

	public double getRadius()	{
		return radius;
	}

	public void setRadius(double radius)	{
		this.radius = radius;
	}
	
	public double findArea() {
		return Math.PI*radius*radius;
	}
	
	/**
	 * 比较两个圆的面积是否相同
	 */
	@Override
/*	public boolean equals(Object obj)	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Circle other = (Circle) obj;
		if (Double.doubleToLongBits(radius) != Double.doubleToLongBits(other.radius))
			return false;
		return true;
	}		*/
	
	public boolean equals(Object obj)	{
		if(this == obj) {
			return true;
		}
		if(obj instanceof Circle) {
			Circle c = (Circle)obj;
			return this.radius == c.radius;
		}
		return false;
	}

	/**
	 * 输出圆的面积
	 */
	@Override
	public String toString()	{
		return "Circle [radius=" + radius + "]";
	}	
}

子类

package com.atguigu.exer3;

/**
 * 
 * @Description  几何形状
 * @author LeungQiChen Email:1256469594@qq.com
 * @version
 * @date 2021年7月19日上午12:44:27
 */
public class GeometricObject
{
	protected String color;
	protected double weight;
	
	public GeometricObject()	{
		super();
		this.color = "white";
		this.weight = 1.0;
	}

	public 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;
	}
	
	
	
	
	
}

测试类

package com.atguigu.exer3;

public class Test
{
	public static void main(String[] args)
	{
		Circle c1 = new Circle(2.3);
		Circle c2 = new Circle("white",2.0,3.3);
		System.out.println(c1.getColor().equals(c2.getColor()));
		System.out.println(c1.getRadius() == c2.getRadius());
		System.out.println(c1.toString() + " " + c2.toString());
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值