Java作业:类的继承的实例(正方体、圆柱体、圆锥体、球体分别继承自矩形类和圆类)

圆类 :

//package Graph;

//圆类
public class Circle {

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

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

    //带参构造方法
    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;
    }
}

圆柱类:

//package Graph;

//圆柱类
public class Cylinder extends Circle {
    private float highth;

    //无参构造
    Cylinder() {
        highth = 1;
    }

    //带参构造
    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 + super.getArea() * 2);
    }

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

圆锥类:

//package Graph;

//圆锥类
public class Cone extends Circle {

    private float highth;

    //无参构造
    public Cone() {
        highth = 1;
    }

    //带参构造
    public Cone(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 (Math.PI * super.getRadius() * Math.sqrt(Math.pow(super.getRadius(), 2) + Math.pow(highth, 2)));
    }

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

 球类:

//package Graph;

//球类
public class Globe extends Circle {

    //无参构造
    public Globe() {
    }

    //带参构造
    public Globe(float radius) {
        super(radius);
    }

    //计算并返回球体的面积
    public double getArea() {
        return 4 * Math.PI * Math.pow(super.getRadius(), 2);
    }

    //计算并返回球体的体积
    public double getVolume() {
        return (3 * Math.PI * Math.pow(super.getRadius(), 3)) / 4;
    }
}

 矩形类:

//package Graph;

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

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

    //带参构造方法
    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;
    }
}

长方体类:

//package Graph;

//长方体类
public class Cuboid extends Rectangle {

    private float highth;

    //无参构造
    Cuboid() {
        highth = 1;
    }

    //带参构造
    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;
    }
}

 测试类:

//package Graph;

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

import java.util.Scanner;

//测试类
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");

        //圆锥类实例化
        Cone c3 = new Cone();
        System.out.println("输入圆锥的半径:");
        c3.setRadius(sc.nextFloat());
        System.out.println("输入圆锥的高:");
        c3.setHighth(sc.nextFloat());
        System.out.println("圆锥的面积:" + c3.getArea());
        System.out.println("圆锥的体积:" + c3.getVolume());

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

        //球类实例化
        Globe g = new Globe();
        System.out.println("输入球体的半径:");
        g.setRadius(sc.nextFloat());
        System.out.println("球体的面积:" + g.getArea());
        System.out.println("球体的体积:" + g.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());
    }
}

  运行结果:

 

  • 15
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值