JAVA新手急救

来源:百度知道 编辑:UC知道 时间:2024/05/22 01:53:03
编写使用Circle类和Rectangle类的程序。每个类中都将包含要用来计算物体面积的数据成员和成员函数。例如,Circle需要radius(半径)或diameter(直径)变量,而Rectangle需要length(长)和width(宽)变量。此外,这些类还应具有持有所创建对象面积值的数据成员。在测试类中访问两个类对象的面积,求和并显示结果
一楼的哥们 你写的是什么东东

/*
*MyShape.java
*/
public class MyShape{
public static void main(String[] args){
Circle myCir=new Circle(10);
Rectangle myRec=new Rectangle(10,10);
System.out.println(myCir);
System.out.println(myRec);
System.out.println("The sum of the two shapes' area is:"+myCir.area+myRec.area);
}
}

class Circle{
final float PI=3.1415926;
private float radius,area;
Circle(){
this(0);
}
Circle(float r){
radius=r;
}
public float getR(){
return radius;
}
public void setR(float r){
radius=r;
}
public float area(){
area=PI*radius*radius;
return area;
}
public String toString(){
return("The area of Circle is:"+area);
}
}

class Rectangle{
private float length,width,area;
Rectangle(){
this(0,0);
}
Rectangle(float newLen,float newWid){
lenth=newLen;
width=newWid