有沒有會Java的來幫幫我?
public interface Shape {
double area();
double perimeter();
}
public class Circle implements Shape{
private static double PI = Math.PI;
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return PI * Math.pow(radius,2);
}
@Override
public double perimeter() {
return 2 * PI * radius;
}
}
public class Rectangle implements Shape{
private double longth;
private double width;
public Rectangle(double longth, double width) {
this.longth = longth;
this.width = width;
}
@Override
public double area() {
return longth * width;
}
@Override
public double perimeter() {
return 2 * (width + longth);
}
}
public class TestMain {
public static void main(String[] args) {
Shape circle = new Circle(1.5);
System.out.println("圓面積:"+circle.area());
System.out.println("圓周長:"+circle.perimeter());
Shape rectangle = new Rectangle(1.5, 3);
System.out.println("矩形面積:"+rectangle.area());
System.out.println("矩形長:"+rectangle.perimeter());
}
}