image.png

    1. package com.atguigu.exercise2;
    2. public class GeometricObject {//几何图形
    3. // 属性
    4. protected String color;// 颜色
    5. protected double weight;// 重量
    6. // 构造器
    7. public GeometricObject(String color,double weight){
    8. this.color = color;
    9. this.weight = weight;
    10. }
    11. //方法
    12. public String getColor() {
    13. return color;
    14. }
    15. public void setColor(String color) {
    16. this.color = color;
    17. }
    18. public double getWeight() {
    19. return weight;
    20. }
    21. public void setWeight(double weight) {
    22. this.weight = weight;
    23. }
    24. public double findArea(){
    25. return 0;
    26. }
    27. }

    1. package com.atguigu.exercise2;
    2. public class Ciecle extends GeometricObject{
    3. //属性
    4. private double radius;
    5. //构造器
    6. public Ciecle(double radius,String color,double weight){
    7. super(color, weight);//父类中的属性
    8. this.radius = radius;
    9. }
    10. //方法
    11. public double getRadius() {
    12. return radius;
    13. }
    14. public void setRadius(double radius) {
    15. this.radius = radius;
    16. }
    17. public double findArea(){
    18. return Math.PI*radius*radius;
    19. }
    20. }

    1. package com.atguigu.exercise2;
    2. public class MyRectangle extends GeometricObject{//矩形
    3. //属性
    4. private double width;
    5. private double height;
    6. //构造器
    7. public MyRectangle(double width,double height,String color,double weight){
    8. super(color, weight);//父类中的属性
    9. this.width = width;
    10. this.height = height;
    11. }
    12. //方法
    13. public double getWidth() {
    14. return width;
    15. }
    16. public void setWidth(double width) {
    17. this.width = width;
    18. }
    19. public double getHeight() {
    20. return height;
    21. }
    22. public void setHeight(double height) {
    23. this.height = height;
    24. }
    25. public double findArea(){
    26. return width*height;
    27. }
    28. }

    1. package com.atguigu.exercise2;
    2. public class GeometricTest {
    3. public static void main(String[] args) {
    4. GeometricTest test = new GeometricTest();
    5. Ciecle c1 = new Ciecle(2.3,"red",1.0);
    6. test.displayGeometricObject(c1);
    7. Ciecle c2 = new Ciecle(3.3,"black",1.0);
    8. System.out.println("c1和c2的面积是否相等:"+test.equalsArea(c1,c2));
    9. }
    10. //测试两个对象的面积是否相等
    11. public boolean equalsArea(GeometricObject o1,GeometricObject o2){
    12. return o1.findArea() == o2.findArea();
    13. }
    14. //计算面积
    15. public void displayGeometricObject(GeometricObject o) {
    16. System.out.println("面积为:" + o.findArea());
    17. }
    18. }