用Java语言设计一个point类,包含xy2个属性,编写setxy0方法和showxy()输出方法。再设计一个circle类,circle类继承自point类,包含1个属性r。编写setr方法和show-xyr()输出方法。生成多个对象进行测试。
时间: 2024-05-14 20:15:02 浏览: 59
用java实现Point类
5星 · 资源好评率100%
Point类的实现:
```java
public class Point {
private int x;
private int y;
public void setxy0() {
x = 0;
y = 0;
}
public void showxy() {
System.out.println("x = " + x + ", y = " + y);
}
}
```
Circle类的实现:
```java
public class Circle extends Point {
private int r;
public void setr(int r) {
this.r = r;
}
public void showxyr() {
System.out.println("x = " + x + ", y = " + y + ", r = " + r);
}
}
```
测试代码:
```java
public class Test {
public static void main(String[] args) {
Point p1 = new Point();
p1.setxy0();
p1.showxy(); // 输出:x = 0, y = 0
Circle c1 = new Circle();
c1.setr(5);
c1.showxyr(); // 输出:x = 0, y = 0, r = 5
Circle c2 = new Circle();
c2.setxy0();
c2.setr(10);
c2.showxyr(); // 输出:x = 0, y = 0, r = 10
}
}
```
阅读全文