希望各位指点一二!
↑这是题目
package jiekou;
interface Shape{
double getArea();
double getPerimeter();
void drawing();
}
class Rectangle implements Shape
{
private double length;
private double width;
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
@Override
public String toString() {
return " 长为" + length + ", 宽为" + width ;
}
public double getArea()
{
return length*width;
}
public double getPerimeter()
{
return 2*(length+width);
}
public void drawing()
{
System.out.println("正在绘制一个长方形"+toString());
}
}
class Circle implements Shape
{
private double radius;
public Circle(double radius) {
super();
this.radius = radius;
}
@Override
public String toString() {
return "半径为" + radius ;
}
public double getArea()
{
return Math.PI*radius*radius;
}
public double getPerimeter()
{
return 2*Math.PI*radius;
}
public void drawing()
{
System.out.println("正在绘制一个圆"+toString());
}
}
以上是我已经完成的代码,MyShape类的建立和之后测试类的调用我就不是很会用了
希望得到各位指点
Circle类的radius加上getter方法
MyShape
class MyShape {
public static void area(Shape shape) {
if (shape instanceof Circle) {
System.out.println("半径为" + ((Circle) shape).getRadius() + "圆面积:");
} else {
System.out.println("指定长宽的矩形面积:");
}
System.out.println(shape.getArea());
}
public static void perimeter(Shape shape) {
if (shape instanceof Circle) {
System.out.println("半径为" + ((Circle) shape).getRadius() + "圆周长:");
} else {
System.out.println("指定长宽的矩形周长:");
}
System.out.println(shape.getPerimeter());
}
public static void drawing(Shape shape) {
shape.drawing();
}
}
测试代码
public class TestShape {
public static void main(String[] args) {
Circle circle = new Circle(10);
MyShape.area(circle);
MyShape.perimeter(circle);
MyShape.drawing(circle);
Rectangle rectangle = new Rectangle(10, 20);
MyShape.area(rectangle);
MyShape.perimeter(rectangle);
MyShape.drawing(rectangle);
}
}