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

类的抽象与封装

时间:2018-04-23 22:48:02      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:his   成员   return   成员方法   应用   tin   out   volume   程序   

编写一个圆类。该类拥有(1)成员变量Radius;(2)两个构造方法circle(),circle(double r);(3)三个成员方法double getarea (),double getperimeter(),void show(),

编写应用程序,创建类的对象,分别设置圆的半径,圆柱体的高,计算并分别显示圆半径,圆面积,圆周长,圆柱体的体积。

public class  Circle

{    private double  Radius;

      public  double getRadius(){

     return  Radius;

}

public void setRadius(double  radius){

Radius=radius;

}

 

public Circle(){

System.out.printin("圆!");}

public Circle(double r){

this.radius=r;

}

public double getArea(double r)

{return Math.PI*r*r;

}

public  double getPerimeter(double r){

return  2*Math.PI*r;

}

public void show()

{System.out.println("圆的半径:+getRadius(radius));

System.out.println("圆的面积:+getArea(Radius));

System.out.println("圆的周长:+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 Math.PI*this.getRadius()*this.getRadius*this.getRadius()*hight;}

public void showVolume(){

System.out.println("圆柱体的体积:+this.getVolume());

}

}

 

类的抽象与封装

标签:his   成员   return   成员方法   应用   tin   out   volume   程序   

原文地址:https://www.cnblogs.com/mwjc/p/8921973.html

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