迪士尼能带吃的进去吗:JAVA怎么先定义一个圆类,再计算圆的面积?明早要全部代码,谢谢!

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 18:53:50
我选修考试急用

class cir
{
double rad;//半径
double area;//面积
public double cal()//求面积的方法
{
this.area=3.1416 * this.rad * this.rad;
//计算的办法是读取自身的半径,并将结果赋给自身面积属性
}
}

通过cir y=new cir();创建对象后,可以直接
y.rad=31.25;
y.area();
这样,area属性就有了值
可以用
System.out.println(y.area);
来输出面积

public class Test {//测试
public static void main(String[] args) {
Round round = new Round(0,0,10);//建立一个round对象
System.out.println("area="+round.getArea());//显示面积
}
}

class Round {//圆的类Round
public Round() {
this.px=0;
this.py=0;
this.r=1;
}
public Round(float px,float py,float r){
this.px=px;
this.py=py;
this.r=r;
}
protected float px,py,r;//圆心位置(px,py)和半径r
public double getArea(){//计算圆的面积
return 3.14 * r * r;
}
}

希望对你有所帮助