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 09:02:14 浏览: 89
以下是实现上述接口和类的代码:
```java
// 定义Compartor接口
interface Compartor {
boolean greater(Object a);
boolean equal(Object a);
boolean less(Object a);
}
// 学生类实现Compartor接口
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;
}
}
// 圆类实现Compartor接口
class Circle implements Compartor {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * 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;
}
}
// 车类实现Compartor接口
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.greater(s2)); // false
System.out.println(s1.equal(s2)); // false
System.out.println(s1.less(s2)); // true
Circle c1 = new Circle(3);
Circle c2 = new Circle(4);
System.out.println(c1.greater(c2)); // false
System.out.println(c1.equal(c2)); // false
System.out.println(c1.less(c2)); // true
Car car1 = new Car("Toyota", 20000);
Car car2 = new Car("Honda", 25000);
System.out.println(car1.greater(car2)); // false
System.out.println(car1.equal(car2)); // false
System.out.println(car1.less(car2)); // true
}
}
```
阅读全文