LZBC
2015-07-31 20:22
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();
}
}
包名统一小写 shape
方法名一般头字母小写
public final double PI=3.1415; public可以去掉吧 ,我不确定
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题
回答 1
回答 4