⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯⋯
僅供參考
public class ShapeTest {
abstract class Shape{
public abstract double area();
public abstract double perimeter();
}
class Rect extends Shape{
private double width;
private double length;
public Rect(double w,double l){
width = w;
length = l;
}
@Override
public double area() {
return width*length;
}
@Override
public double perimeter() {
return 2*(width+length);
}
}
class Circle extends Shape{
private double r;
public Circle(double r){
this.r = r;
}
@Override
public double area() {
return Math.PI * r * r;
}
@Override
public double perimeter() {
return 2 * Math.PI * r;
}
}
public static void main(String[] args){
ShapeTest st = new ShapeTest();
Rect r = st.new Rect(2,3);
System.out.println("矩形的周長為:"+r.perimeter());
System.out.println("矩形的面積為:"+r.area());
Circle c = st.new Circle(1); //單位圓
System.out.println("圓形的周長為:"+c.perimeter());
System.out.println("圓形的面積為:"+c.area());
}
}