Java面向对象练习(2) (2024.7.11)

圆类

java 复制代码
package area20240711;

public class Circle extends Picture{
    private double r;
    private final double PI = 3.1415926;

    public Circle(){}

    public Circle(double r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }

    public double getPI() {
        return PI;
    }

    @Override
    public double length() {
        return 2 * PI * this.r;
    }

    @Override
    public double area() {
        return this.r * this.r * PI;
    }
}

图形类

java 复制代码
package area20240711;

public abstract class Picture {
    public abstract double length();
    public abstract double area();
}

矩形类

java 复制代码
package area20240711;

public class Rectangle extends Picture{
    private double length;
    private double width;

    public Rectangle(){}

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public double length() {
        return 2.0 * (this.length + this.width);
    }

    @Override
    public double area() {
        return this.length * this.width * 1.0;
    }
}

测试

java 复制代码
package area20240711;
import java.util.Scanner;
public class AreaTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 创建一个矩形
        Rectangle rtg = creatRectangle();
        System.out.println("长为" + rtg.getLength() + "宽为" + rtg.getWidth()
        + "的矩形的周长是" + rtg.length());
        System.out.println("长为" + rtg.getLength() + "宽为" + rtg.getWidth()
        + "的矩形的面积是" + rtg.area());

        // 创建一个圆形
        Circle c = creatCircle();
        System.out.println("半径为" + c.getR() + "圆的周长是" + c.length());
        System.out.println("半径为" + c.getR() + "圆的面积是" + c.area());


    }

    public static Rectangle creatRectangle () {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入矩形的长");
        double length = sc.nextDouble();
        System.out.println("请输入矩形的宽");
        double width = sc.nextDouble();
        return new Rectangle(length, width);
    }

    public static Circle creatCircle() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入圆形的半径");
        double r = sc.nextDouble();
        return new Circle(r);
    }
}
相关推荐
空の鱼4 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路5 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花6 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb6 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨6 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
带刺的坐椅6 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系
java·ioc·solon·plugin·aop·handler
不惑_7 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
费曼乐园7 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOME
java·kafka
feilieren7 小时前
SpringBoot 搭建 SSE
java·spring boot·spring
阿岳3168 小时前
Java导出通过Word模板导出docx文件并通过QQ邮箱发送
java·开发语言