Circle c1 = new Circle(1, "BLUE", true);
Circle c2 = new Circle(2, "BLUE", true);
Circle c3 = new Circle(3, "BLUE", false);
Circle c4 = new Circle(4, "BLACK", true);
System.out.println( "c1 ? c1 " + c1.compareTo(c1) );
System.out.println( "c1 ? c2 " + c1.compareTo(c2) );
System.out.println( "c1 ? c3 " + c1.compareTo(c3) );
System.out.println( "c1 ? c4 " + c1.compareTo(c4) );
Rectangle r1 = new Rectangle(1,1,"BLUE", true);
Rectangle r2 = new Rectangle(2,2,"RED", true);
Rectangle r3 = new Rectangle(4,4,"GREEN", false);
Rectangle r4 = new Rectangle(4,4,"GREEN", true);
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "c1 ? r2 " + c1.compareTo(r2) );
System.out.println( "r3 ? r4 " + r3.compareTo(r4) );
用上面的代码来运行出来 这个
c1 ? c1 0
c1 ? c2 -1
c1 ? c3 -1
c1 ? c4 1
r1 ? r2 -1
r1 ? r2 -1
c1 ? r2 1
r3 ? r4 -1
我写了一些,但是怎么都搞不出来,求救。。。。
代码在下面
public abstract class GeometricObject implements Comparable<GeometricObject> {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject(){
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Created on " + dateCreated + "\ncolor: " + color +
"and filled: " + filled;
}
public static void main(String[] args) {
Circle1 c1 = new Circle1(1, "BLUE", true);
Circle1 c2 = new Circle1(2, "BLUE", true);
Circle1 c3 = new Circle1(3, "BLUE", false);
Circle1 c4 = new Circle1(4, "BLACK", true);
System.out.println( "c1 ? c1 " + c1.compareTo(c1) );
System.out.println( "c1 ? c2 " + c1.compareTo(c2) );
System.out.println( "c1 ? c3 " + c1.compareTo(c3) );
System.out.println( "c1 ? c4 " + c1.compareTo(c4) );
Rectangle r1 = new Rectangle(1,1,"BLUE", true);
Rectangle r2 = new Rectangle(2,2,"RED", true);
Rectangle r3 = new Rectangle(4,4,"GREEN", false);
Rectangle r4 = new Rectangle(4,4,"GREEN", true);
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "r1 ? r2 " + r1.compareTo(r2) );
System.out.println( "c1 ? r2 " + c1.compareTo(r2) );
System.out.println( "r3 ? r4 " + r3.compareTo(r4) );
}
public static GeometricObject max (GeometricObject o1, GeometricObject o2){
if (((Comparable)o1).compareTo(o2) > 0 )
return o1;
else
return o2;
}
private String compareTo(Circle1 c3) {
// TODO Auto-generated method stub
return null;
}
private String compareTo(Rectangle r2) {
// TODO Auto-generated method stub
return null;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}}
public class Rectangle extends GeometricObject implements Comparable{
private double width;