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);
    }
}
相关推荐
言、雲几秒前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
TT哇7 分钟前
【数据结构练习题】链表与LinkedList
java·数据结构·链表
Yvemil736 分钟前
《开启微服务之旅:Spring Boot 从入门到实践》(三)
java
Anna。。37 分钟前
Java入门2-idea 第五章:IO流(java.io包中)
java·开发语言·intellij-idea
.生产的驴1 小时前
SpringBoot 对接第三方登录 手机号登录 手机号验证 微信小程序登录 结合Redis SaToken
java·spring boot·redis·后端·缓存·微信小程序·maven
爱上语文1 小时前
宠物管理系统:Dao层
java·开发语言·宠物
王ASC2 小时前
SpringMVC的URL组成,以及URI中对/斜杠的处理,解决IllegalStateException: Ambiguous mapping
java·mvc·springboot·web
是小崔啊2 小时前
开源轮子 - Apache Common
java·开源·apache
因我你好久不见2 小时前
springboot java ffmpeg 视频压缩、提取视频帧图片、获取视频分辨率
java·spring boot·ffmpeg
程序员shen1616112 小时前
抖音短视频saas矩阵源码系统开发所需掌握的技术
java·前端·数据库·python·算法