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);
    }
}
相关推荐
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸4 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了5 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王6 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
qq_327342737 小时前
Java实现离线身份证号码OCR识别
java·开发语言