Java作业-实验四

Circle类

package com.itcast.atd.demo05;

public class Circle {
    private int radius;

    public Circle(){
    }

    public Circle(int r) {
        this.radius = r;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int r) {
        this.radius = r;
    }

    public double getPerimeter(){
        return radius*3.14*2;
    }

    public double getS(){
        return Math.pow(radius,2)*3.14;
    }

    public void disp(){
        System.out.println(radius);
        System.out.println(this.getPerimeter());
        System.out.println(this.getS());
    }
}

 

 

Cylinder类

package com.itcast.atd.demo05;

public class Cylinder extends Circle{
    private double height;

    public Cylinder(double height) {
        this.height = height;
    }

    public Cylinder(int r, double height) {
        super(r);//使用super调用父类中的构造方法
        this.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getVol(){
        return super.getS()*height;
    }

    public void dispVol(){
        System.out.println(this.getVol());
    }
}

 

TEST

package com.itcast.atd.demo05;

public class Test {
public static void main(String[] args) {
        Cylinder cylinder = new Cylinder(2,4);
        System.out.println(cylinder.getVol());
    }
}

题目链接:https://viewer.mosoteach.cn/viewer?token=152bd977ea40b78b8fb396cfbbb1a805

总结:学习起来之后,这题目很简单。相关学习情况我会发随笔记录,欢迎大家阅读。

你可能感兴趣的:(Java作业-实验四)