类的抽象与封装

package 类的抽象与封装;
import java.util.Scanner;
public class 圆类 {
    private double radius;
    public double getRadius() {
    return radius;
    }
    public void setRadius(double radius) {
    this.radius = radius;
    }
    //无参构造函数
    public void Circle(){
    this.radius=0;
    }
    //带参构造函数
    public void Circle(double r ){
    this.radius=r;
    }
    //获取面积
    public double getArea(){
    double r=this.radius;
    double area=r*r*3.14;
    return area;
    }
    //获取周长
    public double getPerimeter(){
    double perimeter=this.radius*2*3.14;
    return perimeter;
    }
    //打印圆的信息
    public void show(){
    System.out.println("请输入圆的半径");
    Scanner sc=new Scanner(System.in);
    this.setRadius(sc.nextInt());
    System.out.println("圆的半径"+this.getRadius());
    System.out.println("圆的面积"+this.getArea());
    System.out.println("圆的周长"+this.getPerimeter());
    }
    }
    //圆柱类
    public class Cylinder extends Circle {
    //圆柱高
    private double height;
    public double getHeight() {
    return height;
    }
    public void setHeight(double height) {
    this.height = height;
    }
    //构造方法
    public Cylinder (double r, double  h){
    super();
    this.height=h;
    this.setRadius(r);
    }
    public double getVolume( ) {
    double volume=this.getArea()*this.height;
    return volume;
    }
    //求体积
    public void showVolume(){
    System.out.println("圆柱的体积"+this.getVolume());
    }
    }
    //主程序入口
    public class Test {
   
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Circle circle=new Circle();
    circle.show();
    Cylinder cylinder=new Cylinder(2.0,8.5);
    cylinder.showVolume();
    }
    }

posted on 2018-04-24 21:34  小小青🌸  阅读(85)  评论(0编辑  收藏  举报