现有类:圆 Circle 、正方形 Square 、长方形 Rectangle 等他们有共同父类 Shape 。编码实现:1求实现:圆、正方形、长方形面积的 getAera 方法
public abstract class Shape { public abstract double getArea(); }
public class Circle extends Shape { private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
public class Square extends Shape { private double side;
public Square(double side) {
this.side = side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double getArea() {
return side * side;
}
}
public class Rectangle extends Shape { private double length; private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
原文地址: https://www.cveoy.top/t/topic/cvgl 著作权归作者所有。请勿转载和采集!