码迷,mamicode.com
首页 > 其他好文 > 详细

类的抽象与封装

时间:2018-04-24 21:43:39      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:void   out   show   函数   ring   double   turn   get   pac   

//圆类

package lx;

public class Circle {
 private double Radius;//存放圆的半径
 public double getRadius() {//获取圆的半径
  return Radius;
 }
 public void setRadius(double radius) {//设定半径
  Radius = radius;
 }
 public Circle( ){//无参构造函数,将半径设为0
  this.Radius=0;
 }
 public Circle(double  r){//带参构造函数,将半径初始化为r
  this.Radius = r;
 }
 public double getArea(double r){//获取圆的面积
  return 3.14159 * r * r;
 }
 public double getPerimeter(double r){//获取圆的周长
  return 2 * 3.14159* r;
 }
 public void  show( ){
  System.out.println("圆的面积: " + this.getArea(Radius));
  System.out.println("圆的周长: " + this.getPerimeter(Radius));
 }

}

 

//圆柱类
public class Cylinder extends Circle {//在圆的基础上定义一个圆柱
 private double hight;
   public Cylinder (double r, double  h ){
   super(r);
   this.hight = h;
  }
  public double getVolume(){//求圆柱的体积
   return 3.14159 * this.getRadius() * this.getRadius() * hight;
  }  
  public void showVolume( ){
   System.out.println("圆柱体的体积:" + this.getVolume());
  }
}
public class Test {
  public static void main(String[] args) {
  Circle cc = new Circle();
  cc.show( );
  Cylinder ccc =new Cylinder(3, 6);//圆柱的底面圆的半径和和圆柱的高
  ccc.showVolume();

 }

}

类的抽象与封装

标签:void   out   show   函数   ring   double   turn   get   pac   

原文地址:https://www.cnblogs.com/zw98916/p/8933446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!