在圆柱体类cylinder中,利用方法的重载来设置成员变量
class cylinder
{
private double radius;
private int height;
private double pi=3.14;
private String color;
public double setcylinder(double r,int h)
{
radius=r;
height=h;
return r+h;
}
public void setcylinder(String str)//重载方法
{
color=str;
}
public void show()
{
System.out.println("圆柱的颜色为:"+color);
}
double area() {//定义缺省访问控制符的方法
return pi * radius * radius;
}
double volume()
{
return this.area() * height;
}
}
public class sentence {
public static void main(String[] args) {
double r_h;
cylinder volu=new cylinder();
r_h=volu.setcylinder(2.5,5);
volu.setcylinder("红色");
System.out.println("圆柱低半径与高之和="+r_h);
System.out.println("圆柱体体积="+volu.volume());
volu.show();
}
}