0
点赞
收藏
分享

微信扫一扫

Java作业:类的继承的实例(正方体、圆柱体)

一叶轻舟okok 2022-04-13 阅读 49
java

/**
 * 类的继承的实例
 * 父类:矩形类、圆类
 * 子类:正方体类、圆柱类
 * 继承关键词:extends
 */

import java.util.Scanner;

//圆类
class Circle {

    //定义圆类的私有属性radius
    private float radius;

    //无参构造方法
    Circle() {
        radius = 0;
    }

    //带参构造方法
    Circle(float radius) {
        this.radius = radius;
    }

    //获取radius的值并返回
    public float getRadius() {
        return radius;
    }

    //修改私有属性radius的值
    public void setRadius(float radius) {
        this.radius = radius;
    }

    //计算圆的面积并返回
    public double getArea() {
        return radius * radius * Math.PI;
    }

    //计算圆的周长并返回
    public double getPerimeter() {
        return radius * Math.PI * 2;
    }

}

//圆柱类
class Cylinder extends Circle {

    private float highth;

    //无参构造
    Cylinder() {
    }

    //带参构造
    Cylinder(float radius, float highth) {
        super(radius);
        this.highth = highth;
    }

    //获取属性高的值
    public float getHighth() {
        return highth;
    }

    //设置私有属性高的值
    public void setHighth(float highth) {
        this.highth = highth;
    }

    //计算并返回面积的值
    public double getArea() {
        return (super.getPerimeter() * highth);
    }

    //计算并返回长方体的体积
    public double getVolume() {
        return super.getArea() * highth;
    }

}

//矩形类
class Rectangle {

    //定义矩形类的私有属性:长和宽
    private float length;
    private float width;

    //无参构造方法
    Rectangle() {
        length = 0;
        width = 0;
    }

    //带参构造方法
    Rectangle(float length, float width) {
        this.length = length;
        this.width = width;
    }

    //获取length的值并返回
    public float getLength() {
        return length;
    }

    //修改私有属性length的值
    public void setLength(float length) {
        this.length = length;
    }

    //获取width的值并返回
    public float getWidth() {
        return width;
    }

    //修改私有属性width的值
    public void setWidth(float width) {
        this.width = width;
    }

    //计算矩形的面积并返回
    public double getArea() {
        return width * length;
    }

    //计算矩形的周长并返回
    public double getPerimeter() {
        return (width + length) * 2;
    }

}

class Cuboid extends Rectangle {

    private float highth;

    //无参构造
    Cuboid() {
    }

    //带参构造
    Cuboid(float length, float width, float highth) {
        super(length, width);
        this.highth = highth;
    }

    //获取属性高的值
    public float getHighth() {
        return highth;
    }

    //设置私有属性高的值
    public void setHighth(float highth) {
        this.highth = highth;
    }

    //计算并返回面积的值
    public double getArea() {
        return (super.getArea() + super.getLength() * highth + super.getWidth() * highth) * 2;
    }

    //计算并返回长方体的体积
    public double getVolume() {
        return super.getArea() * highth;
    }
}

//测试类
public class Graph {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //圆类实例化
        Circle c1 = new Circle();
        System.out.println("\n请输入圆的半径:");
        c1.setRadius(sc.nextFloat());
        System.out.println("圆的面积为:" + c1.getArea());
        System.out.println("圆的周长为:" + c1.getPerimeter());

        System.out.println("\n*********************\n");

        //圆柱类
        Cylinder c2 = new Cylinder();
        System.out.println("输入圆柱的半径:");
        c2.setRadius(sc.nextFloat());
        System.out.println("输入圆柱的高:");
        c2.setHighth(sc.nextFloat());
        System.out.println("圆柱的面积:" + c2.getArea());
        System.out.println("圆柱的体积:" + c2.getVolume());

        System.out.println("\n*********************\n");

        //矩形类实例化
        Rectangle r = new Rectangle();
        System.out.print("请输入矩形的长:");
        r.setLength(sc.nextFloat());
        System.out.print("请输入矩形的宽:");
        r.setWidth(sc.nextFloat());
        System.out.println("矩形的面积为:" + r.getArea());
        System.out.println("矩形的周长为:" + r.getPerimeter());

        System.out.println("\n*********************\n");

        //长方体类实例化
        Cuboid c = new Cuboid();
        System.out.println("输入长方体的长:");
        c.setLength(sc.nextFloat());
        System.out.println("输入长方体的宽:");
        c.setWidth(sc.nextFloat());
        System.out.println("输入长方体的高:");
        c.setHighth(sc.nextFloat());
        System.out.println("长方体的表面积为:" + c.getArea());
        System.out.println("长方体的体积为:" + c.getVolume());
    }
}
举报

相关推荐

0 条评论