其他分享
首页 > 其他分享> > 在圆柱体类cylinder中,利用方法的重载来设置成员变量

在圆柱体类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();
    }
}

 

标签:setcylinder,cylinder,double,private,volu,重载,圆柱体,public
来源: https://blog.csdn.net/m0_58138734/article/details/119296435