用java定义一个图形接口,声明计算图形面积和周长的抽象方法,再设计圆类和长方形类去实现这个接口
展开全部
public abstract class Shape {
public abstract double getArea();
public abstract double getLength();
}
public class Circle extends Shape {
private double dRadius;
public Circle(double r) {
this.dRadius = r;
}
@Override
public double getArea() {
return dRadius * Math.PI * Math.PI;
}
@Override
public double getLength() {
return 2 * dRadius * Math.PI;
}
public void printString() {
System.out.println("我是圆形!我的半径是:" + this.dRadius);
}
public void printArea() {
System.out.println("圆的面积:" + getArea());
}
public void printLength() {
System.out.println("圆的周长:" + getLength());
}
}
public class Rectangle extends Shape {
private double length;
private double width;
public Circle(double l,double w,) {
this.length= l;
this.width= w;
}
@Override
public double getArea() {
return length * width;
}
@Override
public double getLength() {
return 2 *(length + width);
}
public void printString() {
System.out.println("我是长方形!我的长是:" + this.length+",我的宽是:"+width);
}
public void printArea() {
System.out.println("长方形的面积:" + getArea());
}
public void printLength() {
System.out.println("长方形的周长:" + getLength());
}
}
public class Test{
public static void main(String[] args) {
Circle circle = new Circle(2d);
circle.printString();
circle.printArea();
circle.printLength();
Rectangle rectangle = new Rectangle (2d,3d);
rectangle.printString();
rectangle.printArea();
rectangle.printLength();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询