package com.atguigu.exercise2;
public class GeometricObject {//几何图形
// 属性
protected String color;// 颜色
protected double weight;// 重量
// 构造器
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 0;
}
}
package com.atguigu.exercise2;
public class Ciecle extends GeometricObject{
//属性
private double radius;
//构造器
public Ciecle(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;
}
}
package com.atguigu.exercise2;
public class MyRectangle extends GeometricObject{//矩形
//属性
private double width;
private double height;
//构造器
public MyRectangle(double width,double height,String color,double weight){
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;
}
}
package com.atguigu.exercise2;
public class GeometricTest {
public static void main(String[] args) {
GeometricTest test = new GeometricTest();
Ciecle c1 = new Ciecle(2.3,"red",1.0);
test.displayGeometricObject(c1);
Ciecle c2 = new Ciecle(3.3,"black",1.0);
System.out.println("c1和c2的面积是否相等:"+test.equalsArea(c1,c2));
}
//测试两个对象的面积是否相等
public boolean equalsArea(GeometricObject o1,GeometricObject o2){
return o1.findArea() == o2.findArea();
}
//计算面积
public void displayGeometricObject(GeometricObject o) {
System.out.println("面积为:" + o.findArea());
}
}