设计一个shape接口和它的两个实现类square和circle
时间: 2023-04-28 19:05:47 浏览: 137
java代码-使用java解决Square,Circle继承Shape类,Rectangle继Square的源代码
5星 · 资源好评率100%
interface Shape {
double getArea();
double getPerimeter();
}
class Square implements Shape {
double sideLength;
Square(double sideLength) {
this.sideLength = sideLength;
}
double getArea() {
return sideLength * sideLength;
}
double getPerimeter() {
return sideLength * 4;
}
}
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
double getArea() {
return Math.PI * radius * radius;
}
double getPerimeter() {
return 2 * Math.PI * radius;
}
}
阅读全文