public class GeometricObjectText {
public static void main(String[] args) {
GeometricObjectText g = new GeometricObjectText();
Circle c = new Circle(1, "黑色", 1);
MyRectangle m = new MyRectangle("绿色", 1, 2, 1);
System.out.println(g.equalsArea(c, m));
g.displayGeometricObject(c);
g.displayGeometricObject(m);
}
输出结果 :
false
面积是3.141592653589793
黑色
面积是2.0
绿色
public boolean equalsArea(GeometricObject c, GeometricObject m) {
if (c.findArea() == m.findArea()) {
return true;
} else {
return false;
}
}
public void displayGeometricObject(GeometricObject g) {
System.out.println("面积是" + g.findArea());
System.out.println(g.getColor());
}
}
public class GeometricObject {
private String color;
private double weight;
public GeometricObject() {
}
public GeometricObject(String color, double weight) {
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;
}
public double findArea() {
return 1.0;
}
}
public class Circle extends GeometricObject {
private double radius;
public Circle(double radius, String color, double weight) {
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;
}
}
public class MyRectangle extends GeometricObject {
private double width;
private double height;
public MyRectangle(String color, double weight, double width, double height) {
super(color, weight);
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//方法的重写
public double findArea() {
return (width) * height;
}
}