java设计图形Shape类及其子类圆Circle和矩形Rectangle类。图形Shape类中包括位置坐标(x,y),圆类Circle新增半径r,矩形Rectangle类新增宽w和高h,编写三个类的构造方法实现成员变量初始化。
时间: 2024-05-16 21:17:07 浏览: 80
下面是Java代码实现:
```java
public class Shape {
private int x;
private int y;
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Circle extends Shape {
private int r;
public Circle(int x, int y, int r) {
super(x, y);
this.r = r;
}
public int getR() {
return r;
}
}
public class Rectangle extends Shape {
private int w;
private int h;
public Rectangle(int x, int y, int w, int h) {
super(x, y);
this.w = w;
this.h = h;
}
public int getW() {
return w;
}
public int getH() {
return h;
}
}
```
其中,Shape是基类,Circle和Rectangle是Shape的子类。在Shape类中定义了位置坐标(x,y)两个成员变量和构造方法,Circle类和Rectangle类分别新增了半径r和宽高w、h两个成员变量,并且在构造方法中调用了父类的构造方法来初始化基类的成员变量。
阅读全文