圆 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());
}
}
731

被折叠的 条评论
为什么被折叠?



