栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

【设计一个抽象的形状类Shape,方法:求周长和求面积。shape类的子类:Rect(矩形),Circle(圆形)。Rect类的子类:Square(正方形)不同的子类会有不同的计算周长和面积的方法。】 抽象的形状类Shape
public abstract class Shape {
    
   public abstract double getArea();

    
    public   abstract double getPerimeter();
}
Rect(矩形)
public class Rect extends Shape {
    
    private double width;

    
    private double length;

    public Rect(double width) {
        this.width = width;
    }

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

    public double getWidth() {
        return width;
    }

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

    public double getLength() {
        return length;
    }

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

    @Override
    public double getArea() {
        return length * width;
    }

    @Override
    public double getPerimeter() {
        return 2 * (length + width);
    }
}
Circle(圆形)

public class Circle extends Shape {
    
    private final double PI = 3.14;

    
    private double r;

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

    public double getPI() {
        return PI;
    }

    public double getR() {
        return r;
    }

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

    @Override
    public double getArea() {
        return PI * r * r;
    }

    @Override
    public double getPerimeter() {
        return 2 * PI * r;
    }
}
Square(正方形)
public class Square extends Rect {
    public Square(double width) {
        super(width);
    }

    @Override
    public double getArea() {
        return getWidth() * getWidth();
    }

    @Override
    public double getPerimeter() {
        return 4 * getWidth();
    }
}
测试类
public class TestMain {
    public static void main(String[] args) {
        System.out.println("==============圆==============");
        Circle circle = new Circle(3.0);
        double circleArea = circle.getArea();
        double circlePerimeter = circle.getPerimeter();
        System.out.println("圆半径:" + circle.getR());
        System.out.println("圆面积:" + circleArea + ", 圆周长:" + circlePerimeter);

        System.out.println("==============矩形==============");
        Rect rect = new Rect(3, 4);
        double rectArea = rect.getArea();
        double rectPerimeter = rect.getPerimeter();
        System.out.println("矩形长度:" + rect.getLength() + ", 矩形宽度:" + rect.getWidth());
        System.out.println("矩形面积:" + rectArea + ", 矩形周长:" + rectPerimeter);

        System.out.println("==============正方形==============");
        Rect square = new Square(3);
        double squareArea = square.getArea();
        double squarePerimeter = square.getPerimeter();
        System.out.println("正方形边长:" + square.getWidth());
        System.out.println("正方形面积:" + squareArea + ", 正方形周长:" + squarePerimeter);
    }
}
运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980916.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号