代码如下:
float perimeter();
}
class Sequare implements Shape {
private float a;
public Sequare(float a) {
super();
this.a = a;
}
@Override
public float perimeter() {
return 4 * a;
}
}
class Rou implements Shape {
private float r;
public Rou(float r) {
this.r = r;
}
@Override
public float perimeter() {
return 2 * 3.14f * r;
}
}
public class MainClass {
public static float getPerimeter(Shape s) {
return s.perimeter();
}
public static void main(String[] args) {
Sequare s = new Sequare(20);
System.out.println("Square周长:" + getPerimeter(s));
Rou r = new Rou(15.5f);
System.out.println("Rou周长:" + getPerimeter(r));
}
}
运行结果: