import java.util.Scanner;
class Cylinder
{
private double radius;
private int height;
private double pi=3.14;
Cylinder(double r,int h) //定义构造函数,构造函数的函数名与类名相同,构造函数的目的是为私有变量赋值,即初始化
{
radius=r;
height=h;
}
double getRadius() //用成员函数获取半径
{
return radius;
}
int getHeight() //用成员函数获取高
{
return height;
}
double area()
{
return pi*radius*radius;
}
double volume()
{
return area()*height;
}
}
public class App7_1 {
public static void main(String[] args)
{
double r;
int h;
Scanner reader=new Scanner(System.in);
System.out.print("请输入底圆半径和高:");
r=reader.nextDouble();
h=reader.nextInt();
Cylinder volu=new Cylinder(r,h); //实际参数是:半径=2.5, 高=6
System.out.println("底圆半径="+volu.getRadius());
System.out.println("圆柱的高="+volu.getHeight());
System.out.print("圆柱");
System.out.println("底面积="+volu.area());
System.out.println("圆柱体体积="+volu.volume());
reader.close();
}
}
运行结果: