类的设计:定义一个类Circle,定义多边体的类Cylinder(为Circle的子类),定义Test类(主类) 要求: ① 成员变量(private): Circle类:r表示半径,p表示周长,s表示面积,cirque表示圆环 Cylinder类:h表示高,
时间: 2024-03-17 22:39:40 浏览: 65
v表示体积 ② 构造方法:Circle类:无参构造方法、带参构造方法 Cylinder类:继承Circle类的构造方法、带参构造方法 ③ 成员方法: Circle类:getR()、getP()、getS()、show() Cylinder类:getH()、getV()、show() Test类:主方法main(),分别创建Circle和Cylinder对象,调用其show()方法,输出结果。
以下是示例代码:
```java
// Circle类
class Circle {
private double r;
private double p;
private double s;
private double cirque;
public Circle() {
r = 0;
p = 0;
s = 0;
cirque = 0;
}
public Circle(double r) {
this.r = r;
p = 2 * Math.PI * r;
s = Math.PI * r * r;
cirque = p;
}
public double getR() {
return r;
}
public double getP() {
return p;
}
public double getS() {
return s;
}
public void show() {
System.out.println("圆的半径为:" + r);
System.out.println("圆的周长为:" + p);
System.out.println("圆的面积为:" + s);
System.out.println("圆环的周长为:" + cirque);
}
}
// Cylinder类
class Cylinder extends Circle {
private double h;
private double v;
public Cylinder() {
super();
h = 0;
v = 0;
}
public Cylinder(double r, double h) {
super(r);
this.h = h;
v = super.getS() * h;
}
public double getH() {
return h;
}
public double getV() {
return v;
}
public void show() {
super.show();
System.out.println("圆柱的高为:" + h);
System.out.println("圆柱的体积为:" + v);
}
}
// Test类
public class Test {
public static void main(String[] args) {
Circle c = new Circle(5.0);
c.show();
System.out.println("----------------");
Cylinder cy = new Cylinder(5.0, 10.0);
cy.show();
}
}
```
阅读全文