package com.hanqi.test; //创建接口 public interface ShapePara { //获取面积的方法 double getArea(); //获取周长的方法 double getCircumference(); }
package com.hanqi.test; public class Circle implements ShapePara { //定义圆心 public double radius; //定义圆心的横向坐标 private double X; //定义圆心的纵向坐标 protected double Y; public double getX() { return X; } public void setX(double x) { X = x; } public double getY() { return Y; } public void setY(double y) { Y = y; } //完善接口的方法 @Override public double getArea() { return Math.PI*Math.pow(radius, 2); } @Override public double getCircumference() { return 2*Math.PI*radius; } public Circle(double radius) { this.radius=radius; } public double getRadius() { return radius; } public void setCenter(double x, double y) { } public void setRadius(double radius) { } }
package com.hanqi.test; public class Shape { public void getArea() { } }
package com.hanqi.test; public class Rect extends Shape { private double a; private double b; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } //对父类方法进行重写 public void getArea() { System.out.println("矩形的长a="+a+",b="+b+"的矩形的面积是:"+a*b); } }
package com.hanqi.test; public class Test02 { public static void main(String[] args) { Rect re=new Rect(); re.setA(20); re.setB(30); re.getArea(); Circle cc=new Circle(30); System.out.println("radius=30圆 的面积是:"+cc.getArea()); } }
时间: 2024-12-06 19:16:55