用Java定义一个形状类Shape
发布网友
发布时间:2022-05-27 10:06
我来回答
共1个回答
热心网友
时间:2023-10-16 08:35
publicabstractclass Shape {
publicabstractvoid area();
}
class Circle extends Shape {
privatedoubleradius;
privatedoubleS;
Circle(double radius) {
this.radius = radius;
}
publicvoid area() {
S = 3.14 * radius * radius;
System.out.println(S);
}
}
class Rect extends Shape {
privatedoublelength;
privatedoublewidth;
privatedoubleS;
Rect(double length,double width) {
this.length = length;
this.width = width;
}
publicvoid area() {
S = length * width;
System.out.println(S);
}
}
class Test {
publicstaticvoid main(String[] args) {
Circle a = new Circle(3);
a.area();
Rect b = new Rect(3,4);
b.area();
}
}