java 抽象 封装_Java程序(类的抽象与封装)

package clearlove;

public class yuan {//定义构造函数

private double Radius;//成员变量

public double circle() {

return Radius;

}

public void circle(double r) {//创建时将半径初始化为r

Radius = r;

}

public yuan( ){

System.out.println("圆!");

}

public yuan(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("圆的面积: " + this.getArea(Radius));

System.out.println("圆的周长: " + this.getPerimeter(Radius));

}

}

package clearlove;

public class yuanzhu extends yuan {

private double hight;

public yuanzhu (double r, double  h )

{

super(r);//成员变量

this.hight = h;

}

public double getVolume(){//get方法存取对象 跟set相比 它不能重载 方法名必须不相同

return Math.PI * this.circle() * this.circle() * hight;

}

public void showVolume( ){

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

}

}

package clearlove;

import java.util.Scanner;//输入函数的要定义的

public class 主函数 {

public static void main(String[] args){

double r,h;

Scanner in=new Scanner(System.in);

System.out.print("输入圆的半径");

r=in.nextInt();

yuan c1=new yuan(r);

c1.show( );

Scanner i=new Scanner(System.in);

System.out.print("输入圆柱的高");

h=i.nextInt();

yuanzhu c2=new yuanzhu(r,h);

c2.showVolume( );

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值