image.png

    1. package com.atguigu.exercise2;
    2. public class Circle {
    3. //属性
    4. private double radius;//半径
    5. //构造器
    6. public Circle(){
    7. radius = 1.0;
    8. }
    9. public Circle(double radius){
    10. this.radius = radius;
    11. }
    12. //方法
    13. public void setRadius(double radius){
    14. this.radius = radius;
    15. }
    16. public double getRadius(){
    17. return radius;
    18. }
    19. //返回圆的面积
    20. public double findArea(){
    21. return Math.PI*radius*radius;
    22. }
    23. }

    1. package com.atguigu.exercise2;
    2. public class Cylinder extends Circle{
    3. //属性
    4. private double length;//高
    5. //构造器
    6. public Cylinder(){
    7. length = 1.0;
    8. }
    9. public Cylinder(double length){
    10. this.length = length;
    11. }
    12. //方法
    13. public double getLength(){
    14. return length;
    15. }
    16. public void setLength(double length){
    17. this.length = length;
    18. }
    19. public double findVolume(){
    20. //return Math.PI*getRadius()*getRadius()*getLength();
    21. return findArea()*getLength();
    22. }
    23. }

    1. package com.atguigu.exercise2;
    2. public class CylinderTest {
    3. public static void main(String[] args) {
    4. Cylinder cd = new Cylinder();
    5. cd.setRadius(2);
    6. cd.setLength(5);
    7. cd.findVolume();
    8. System.out.println("圆柱的体积是:" + cd.findVolume());
    9. }
    10. }