问答详情
源自:10-3 Java 中的抽象类

新手写的课后练习,感觉基本能满足题目的要求... 有什么需要改进的地方吗?

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();

   

}


}


提问者:LZBC 2015-07-31 20:22

个回答

  • 神经旷野舞者
    2015-08-01 19:37:57

    1. 包名统一小写 shape

    2. 方法名一般头字母小写

    3. public final double PI=3.1415;  public可以去掉吧 ,我不确定