zoukankan      html  css  js  c++  java
  • 对象中—练习6

    Circle.java

    package com.atguigu.exer1;
    
    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 3.14 * radius * radius;
        }
    
    }

    GeometricObject.java

    package com.atguigu.exer1;
    
    public class GeometricObject {//几何图形
    
        protected String color;
        protected double 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 GeometricObject(String color, double weight) {
            super();
            this.color = color;
            this.weight = weight;
        }
        
        public double findArea(){
            return 0.0;
        }
        
    }

    GeometricTest.java

    package com.atguigu.exer1;
    /*
     * 
     * 定义一个测试类GeometricTest,
     * 编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
     * 编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
    
     */
    public class GeometricTest {
    
        public static void main(String[] args) {
            GeometricTest test = new GeometricTest();
            
            Circle c1 = new Circle(3.3, "white", 1.0);
            test.displayGeometricObject(c1);
            Circle c2 = new Circle(3.3, "white", 1.0);
            test.displayGeometricObject(c2);
            
            boolean isEquals = test.equalsArea(c1, c2);
            System.out.println("c1 和 c2的面积是否相等:" + isEquals);
            
            MyRectangle rect = new MyRectangle(2.1, 3.4, "red", 2.0);
            test.displayGeometricObject(rect);
            
        }
        
        public void displayGeometricObject(GeometricObject o){//GeometricObject o = new Circle(...)
            System.out.println("面积为:" + o.findArea());
        }
        
        //测试两个对象的面积是否相等
        public boolean equalsArea(GeometricObject o1,GeometricObject o2){
            return o1.findArea() == o2.findArea();
        }
    }

    MyRectangle.java

    package com.atguigu.exer1;
    
    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;
        }
    
        @Override
        public double findArea() {
            return width * height;
        }
    }
  • 相关阅读:
    rails s 命令不起作用
    ubuntu下virtualbox共享usb
    ubuntu15.04 无法识别exfat格式
    .net core 2.2 修改IdentityUser主键标识类型
    Mac os 安装node.js及环境变量的配置过程
    常见互联网网络名词整理
    assert的用法
    Mac系统中 改变 pip总是默认安装在Mac上自带的python上为python3
    测试工程师的发展之路
    MySQL的mysql-8.0.17-winx64版本安装过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/fenxiangyuan/p/14390846.html
Copyright © 2011-2022 走看看