1、定义一个接口Compartor,包含三个方法: ① boolean greater(Object a); 比较两对象的大小关系,如果当前对象大于a,则返回true,否则返回false ② boolean equal(Object a); 比较两对象的大小关系,如果当前对象等于a,则返回true,否则返回false ③ boolean less(Object a); 比较两对象的大小关系,如果当前对象小于a,则返回true,否则返回false 2、定义一个学生类Student实现上述接口,类成员包含学生的姓名、年龄,对于学生进行判断大小的依据是年龄。 3、定义一个圆类Circle实现上述接口,类成员包含圆的半径,对于圆进行判断大小的依据是面积。 4、定义一个车类Car实现上述接口,类成员包含车的品牌型号、价格,对于车进行判断大小的依据是价格。 5、jAVA程序进行测试。
时间: 2023-06-18 12:01:58 浏览: 71
以下是实现上述要求的代码:
Compartor接口:
```
public interface Compartor {
boolean greater(Object a);
boolean equal(Object a);
boolean less(Object a);
}
```
Student类:
```
public class Student implements Compartor {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean greater(Object a) {
if(a instanceof Student) {
Student s = (Student) a;
return this.age > s.age;
}
return false;
}
@Override
public boolean equal(Object a) {
if(a instanceof Student) {
Student s = (Student) a;
return this.age == s.age;
}
return false;
}
@Override
public boolean less(Object a) {
if(a instanceof Student) {
Student s = (Student) a;
return this.age < s.age;
}
return false;
}
}
```
Circle类:
```
public class Circle implements Compartor {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public boolean greater(Object a) {
if(a instanceof Circle) {
Circle c = (Circle) a;
return this.getArea() > c.getArea();
}
return false;
}
@Override
public boolean equal(Object a) {
if(a instanceof Circle) {
Circle c = (Circle) a;
return this.getArea() == c.getArea();
}
return false;
}
@Override
public boolean less(Object a) {
if(a instanceof Circle) {
Circle c = (Circle) a;
return this.getArea() < c.getArea();
}
return false;
}
private double getArea() {
return Math.PI * radius * radius;
}
}
```
Car类:
```
public class Car implements Compartor {
private String brand;
private double price;
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public boolean greater(Object a) {
if(a instanceof Car) {
Car c = (Car) a;
return this.price > c.price;
}
return false;
}
@Override
public boolean equal(Object a) {
if(a instanceof Car) {
Car c = (Car) a;
return this.price == c.price;
}
return false;
}
@Override
public boolean less(Object a) {
if(a instanceof Car) {
Car c = (Car) a;
return this.price < c.price;
}
return false;
}
}
```
测试代码:
```
public class Test {
public static void main(String[] args) {
Student s1 = new Student("Tom", 18);
Student s2 = new Student("Jerry", 20);
System.out.println("s1 > s2: " + s1.greater(s2));
System.out.println("s1 == s2: " + s1.equal(s2));
System.out.println("s1 < s2: " + s1.less(s2));
Circle c1 = new Circle(3);
Circle c2 = new Circle(4);
System.out.println("c1 > c2: " + c1.greater(c2));
System.out.println("c1 == c2: " + c1.equal(c2));
System.out.println("c1 < c2: " + c1.less(c2));
Car car1 = new Car("BMW", 300000);
Car car2 = new Car("Audi", 250000);
System.out.println("car1 > car2: " + car1.greater(car2));
System.out.println("car1 == car2: " + car1.equal(car2));
System.out.println("car1 < car2: " + car1.less(car2));
}
}
```
输出结果:
```
s1 > s2: false
s1 == s2: false
s1 < s2: true
c1 > c2: false
c1 == c2: false
c1 < c2: true
car1 > car2: true
car1 == car2: false
car1 < car2: false
```
阅读全文