📄 testinterface.java
字号:
// TestInterface.java: 使用接口:Comparable
// 使用Max类中的max()方法, 寻找最大的对象
public class TestInterface
{
// Main method
public static void main(String[] args)
{
// Create two comarable circles
ComparableCircle circle1 = new ComparableCircle(5);
ComparableCircle circle2 = new ComparableCircle(4);
// Display the max circle
Comparable circle = Max.max(circle1, circle2);
System.out.println("The max circle's radius is " +
((Circle)circle).getRadius());
System.out.println(circle);
// Create two comarable cylinders
ComparableCylinder cylinder1 = new ComparableCylinder(5, 2);
ComparableCylinder cylinder2 = new ComparableCylinder(4, 5);
// Display the max cylinder
Comparable cylinder = Max.max(cylinder1, cylinder2);
System.out.println();
System.out.println("cylinder1's volume is " +
cylinder1.findVolume());
System.out.println("cylinder2's volume is " +
cylinder2.findVolume());
System.out.println("The max cylinder's \tradius is " +
((Cylinder)cylinder).getRadius() + "\n\t\t\tlength is " +
((Cylinder)cylinder).getLength() + "\n\t\t\tvolume is " +
((Cylinder)cylinder).findVolume());
System.out.println(cylinder);
}
}
// ComparableCircle is a subclass of Circle, which implements the
// Comparable interface
class ComparableCircle extends Circle implements Comparable
{
// Construct a CompareCircle with specified radius
public ComparableCircle(double r)
{
super(r);
}
// Implement the compareTo method defined in Comparable
public int compareTo(Object o)
{
if (getRadius() > ((Circle)o).getRadius())
return 1;
else if (getRadius() < ((Circle)o).getRadius())
return -1;
else
return 0;
}
}
// ComparableCylinder is a subclass of Cylinder, which implements the
// CompareObject interface
class ComparableCylinder extends Cylinder implements Comparable
{
// Construct a CompareCylinder with radius and length
ComparableCylinder(double r, double l)
{
super(r, l);
}
// Implement the compareTo method defined in Comparable interface
public int compareTo(Object o)
{
if (findVolume() > ((Cylinder)o).findVolume())
return 1;
else if (findVolume() < ((Cylinder)o).findVolume())
return -1;
else
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -