在“实验4”包中编写“实验2Point”和“实验 2Circle”类的子类“实验4Point” “实验 4.Circle”,并重写其构造方法、equals0方法和 toString0)方法; (2)在实验4”包中编写测试类实验4.Test,并测试上述方法的正确性。●
时间: 2024-03-17 17:41:44 浏览: 87
好的,根据您的要求,我提供以下Java代码示例:
实验2Point类:
```java
public class 实验2Point {
private int x;
private int y;
public 实验2Point(int x, int y) {
this.x = x;
this.y = y;
}
// getters and setters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof 实验2Point) {
实验2Point other = (实验2Point) obj;
return x == other.x && y == other.y;
}
return false;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
```
实验2Circle类:
```java
public class 实验2Circle {
private 实验2Point center;
private double radius;
public 实验2Circle(实验2Point center, double radius) {
this.center = center;
this.radius = radius;
}
// getters and setters
public 实验2Point getCenter() {
return center;
}
public void setCenter(实验2Point center) {
this.center = center;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof 实验2Circle) {
实验2Circle other = (实验2Circle) obj;
return center.equals(other.center) && radius == other.radius;
}
return false;
}
@Override
public String toString() {
return "Circle with center " + center.toString() + " and radius " + radius;
}
}
```
实验4Point类:
```java
public class 实验4Point extends 实验2Point {
public 实验4Point(int x, int y) {
super(x, y);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof 实验4Point) {
实验4Point other = (实验4Point) obj;
return getX() == other.getX() && getY() == other.getY();
}
return false;
}
@Override
public String toString() {
return "实验4Point" + super.toString();
}
}
```
实验4Circle类:
```java
public class 实验4Circle extends 实验2Circle {
public 实验4Circle(实验4Point center, double radius) {
super(center, radius);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof 实验4Circle) {
实验4Circle other = (实验4Circle) obj;
return getCenter().equals(other.getCenter()) && getRadius() == other.getRadius();
}
return false;
}
@Override
public String toString() {
return "实验4Circle with center " + getCenter().toString() + " and radius " + getRadius();
}
}
```
实验4.Test测试类:
```java
public class 实验4Test {
public static void main(String[] args) {
实验2Point p1 = new 实验2Point(1, 2);
实验2Point p2 = new 实验2Point(1, 2);
System.out.println(p1.equals(p2)); // expected output: true
System.out.println(p1.toString()); // expected output: (1, 2)
实验4Point p3 = new 实验4Point(1, 2);
实验4Point p4 = new 实验4Point(1, 2);
System.out.println(p3.equals(p4)); // expected output: true
System.out.println(p3.toString()); // expected output: 实验4Point(1, 2)
实验2Circle c1 = new 实验2Circle(new 实验2Point(1, 2), 3);
实验2Circle c2 = new 实验2Circle(new 实验2Point(1, 2), 3);
System.out.println(c1.equals(c2)); // expected output: true
System.out.println(c1.toString()); // expected output: Circle with center (1, 2) and radius 3.0
实验4Point center = new 实验4Point(1, 2);
实验4Circle c3 = new 实验4Circle(center, 3);
实验4Circle c4 = new 实验4Circle(center, 3);
System.out.println(c3.equals(c4)); // expected output: true
System.out.println(c3.toString()); // expected output: 实验4Circle with center 实验4Point(1, 2) and radius 3.0
}
}
```
阅读全文