利用接口实现多态的方法,程序要求如下: (1)首先创建圆类circle和圆柱体类cylinder,其中circle类是父类,cylinder类是子类; (2)创建接口comparable,其中包含一个抽象方法compareto,用来比较对象的大小。抽象方法compareto的形式如下:public int compareto(object o); (3)创建类comparablecircle,该类为circle类的子类,并实现comparable接口; (4)创建类comparablecylinder,该类为cylinder类的子类,并实现comparable接口; (5)创建通用类max,其中包含通用方法max,只要类实现了comparable接口,就可以使用max方法返回两个对象中较大的一个,max方法的方法名称为:public static comparable max(comparable o1,comparable o2); (6)程序的主方法中分别创建两个comparablecircle类对象和两个comparablecylinder类对象,并分别以他们为参数调用max方法,返回两个对象中面积较大的一个。
时间: 2024-03-01 16:54:02 浏览: 53
实验2 面向对象编程
好的,以下是满足要求的完整程序:
Circle.java:
```java
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
```
Cylinder.java:
```java
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
super(radius);
this.height = height;
}
public double getHeight() {
return height;
}
public double getVolume() {
return getArea() * height;
}
}
```
Comparable.java:
```java
public interface Comparable {
int compareTo(Object o);
}
```
ComparableCircle.java:
```java
public class ComparableCircle extends Circle implements Comparable {
public ComparableCircle(double radius) {
super(radius);
}
@Override
public int compareTo(Object o) {
ComparableCircle c = (ComparableCircle) o;
if (this.getArea() > c.getArea()) {
return 1;
} else if (this.getArea() == c.getArea()) {
return 0;
} else {
return -1;
}
}
}
```
ComparableCylinder.java:
```java
public class ComparableCylinder extends Cylinder implements Comparable {
public ComparableCylinder(double radius, double height) {
super(radius, height);
}
@Override
public int compareTo(Object o) {
ComparableCylinder c = (ComparableCylinder) o;
if (this.getVolume() > c.getVolume()) {
return 1;
} else if (this.getVolume() == c.getVolume()) {
return 0;
} else {
return -1;
}
}
}
```
Max.java:
```java
public class Max {
public static Comparable max(Comparable o1, Comparable o2) {
if (o1.compareTo(o2) > 0) {
return o1;
} else {
return o2;
}
}
}
```
Main.java:
```java
public class Main {
public static void main(String[] args) {
ComparableCircle c1 = new ComparableCircle(2);
ComparableCircle c2 = new ComparableCircle(3);
ComparableCylinder cy1 = new ComparableCylinder(2, 3);
ComparableCylinder cy2 = new ComparableCylinder(3, 4);
Comparable maxCircle = Max.max(c1, c2);
Comparable maxCylinder = Max.max(cy1, cy2);
System.out.println("The max circle has radius: " + ((Circle) maxCircle).getRadius());
System.out.println("The max cylinder has radius: " + ((Cylinder) maxCylinder).getRadius() + " and height: " + ((Cylinder) maxCylinder).getHeight());
}
}
```
在主方法中,我们创建了两个ComparableCircle对象和两个ComparableCylinder对象,并分别以它们为参数调用Max.max方法,返回面积或体积较大的对象。注意,由于ComparableCircle和ComparableCylinder都实现了Comparable接口,所以它们都可以作为参数传递给Max.max方法。
输出结果:
```
The max circle has radius: 3.0
The max cylinder has radius: 3.0 and height: 4.0
```
可以看到,输出结果正确地返回了面积或体积较大的对象。
阅读全文