新手寫的課后練習(xí),感覺基本能滿足題目的要求... 有什么需要改進(jìn)的地方嗎?
package Shape; //父類
public abstract class Shape {
public abstract void ZhouChang();
public abstract void Area();
} ?
//長方形類
package Shape;
public class Rectangle extends Shape {
? ? int length;
? ? int width;
public Rectangle(int a,int b)
{
length=a;
width=b;
//System.out.println("長方形周長為"+Sum);
}
public void ZhouChang()
{
int total=(length+width)*2;
System.out.println("該長方形的周長為: "+total);
}
public void Area()
{
int area=length*width;
System.out.println(" ? ? ? ?面積為: "+area);
//System.out.println("長方形的面積為"+area);
}
}
//圓形類
package Shape;
public class Circle extends Shape {
? public final double PI=3.1415;
? int R;
public Circle(int r)
{
this.R=r;//System.out.println("長方形周長為"+Sum);
}
public void ZhouChang()
{
double total=2*PI*R;
System.out.println("該圓形的周長為: "+total);
}
public void Area()
{
double area=PI*R;
System.out.println(" ? ? ? ?面積為: "+area);
//System.out.println("長方形的面積為"+area);
}
}
//測試類
package Shape;
public class Initial {
static void main(String[] args) {
// TODO Auto-generated method stub
? ? ? ? Shape one=new Rectangle(2,3);
? ? ? ? one.ZhouChang();
? ? ? ? one.Area();?
? ? ? ? Shape two=new Circle(3);
? ? ? ? two.ZhouChang();
? ? ? ? two.Area();
? ?
}
}
2015-08-01
包名統(tǒng)一小寫 shape
方法名一般頭字母小寫
public final double PI=3.1415; ?public可以去掉吧 ,我不確定