package day_15; /** * 定义一个平面图形抽象类 */ public abstract class PlaneFigure { public abstract double getArea(); //定义得到面积的抽象方法 public abstract double getPerimeter(); //定义得到周长的抽象方法 } package day_15; //定义一个矩形类 public class Rectangle extends PlaneFigure { private double wide; private double length; public Rectangle(){ } public Rectangle(double wide, double length) { this.wide = wide; this.length = length; } public double getArea(){ //重写得到面积的抽象方法 return wide*length; } public double getPerimeter(){ //重写得到周长的抽象方法 return 2*(wide+length); } } package day_15; //定义一个圆类 public class Circle extends PlaneFigure{ private double r; private static final double PI=3.14; public Circle(){ } public Circle(double r) { this.r = r; } @Override public double getArea() { // return PI*r*r; } @Override public double getPerimeter() { return 2*PI*r; } } package day_15; public class Test { public static void main(String[] args) { Circle circle=new Circle(3); print(circle); Rectangle rectangle=new Rectangle(3,4); print(rectangle); } public static void print(PlaneFigure planeFigure){ //定义静态方法 System.out.println("当前平面图形的信息:"); System.out.println(" 面积:"+planeFigure.getArea()); System.out.println(" 周长:"+planeFigure.getPerimeter()); } }