2011-11-07 3 views
1
package geometricobject; 

public abstract class GeometricObject implements Comparable { 
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) { 
    GeometricObject circle1 = new Circle(1, "Red", true); 
    GeometricObject circle2 = new Circle(2, "Blue", false); 
    GeometricObject maxCircle = new Circle(); 
    GeometricObject rect1 = new Rectangle(1, 1, "Red", true); 
    GeometricObject rect2 = new Rectangle(2, 2, "Blue", false); 
    GeometricObject maxRect = new Rectangle(); 

    maxCircle = GeometricObject.max(circle1, circle2); 
    maxRect = GeometricObject.max(rect1, rect2); 

    System.out.println(maxCircle.toString()); 
    System.out.println(maxRect.toString()); 

} 

public static GeometricObject max (GeometricObject o1, GeometricObject o2){ 
    if (((Comparable)o1).compareTo(o2) > 0) 
     return o1; 
    else 
     return o2; 
} 

public interface Comparable { 
     public int compareTo(GeometricObject o); 
    } 

public int compareTo(GeometricObject o) { 
     if (this.getArea() > o.getArea()) 
      return 1; 
     else if (this.getArea() < o.getArea()) 
      return -1; 
     else 
      return 0; 
    } 

    public abstract double getArea(); 

} 

서클 클래스구체적인 서브 클래스를 가지는 abstract 클래스로부터의 Comparable 인터페이스의 구현

class Circle extends GeometricObject { 
    private double radius; 

    public Circle() { 
    } 

    public Circle(double radius){ 
     this.radius = radius; 
    } 

    public Circle(double radius, String color, boolean filled){ 
     this.radius = radius; 
     setColor(color); 
     setFilled(filled); 
    } 

    public double getRadius(){ 
     return radius; 
    } 

    public void setRadius(double radius){ 
     this.radius = radius; 
    } 

    public double getArea(){ 
     return radius * radius * Math.PI; 
    } 

    public double getDiameter(){ 
     return 2 * radius; 
    } 

    public double getPerimeter(){ 
     return 2 * radius * Math.PI; 
    } 

    public void printCircle() { 
     System.out.println("The circle is created " + getDateCreated() + 
      " and the radius is " + radius); 
    }  

} 

사각형 클래스

class Rectangle extends GeometricObject { 
    private double width; 
    private double height; 

    public Rectangle() { 
    } 

    public Rectangle(double Width, double Height){ 
     this.width = Width; 
     this.height = Height; 
    } 

    public Rectangle(double Width, double Height, String Color, boolean Filled){ 
     this.width = Width; 
     this.height = Height; 
     setColor(Color); 
     setFilled(Filled); 
    } 

    public double getWidth(){ 
     return width; 
    } 

    public double getHeight(){ 
     return height; 
    } 

    public void setHeight(double Height){ 
     this.height = Height; 
    } 

    public double getArea(){ 
     return width * height; 
    } 

    public double getPerimeter(){ 
     return 2 * (width + height); 
    }   
} 

그래서이 별도의 클래스를 시도하고 지속적으로이 오류가 발생했습니다.

Exception in thread "main" java.lang.ExceptionInInitializerError 
     at geometricobject.GeometricObject.main(GeometricObject.java:44) 
Caused by: java.lang.RuntimeException: Uncompilable source code - geometricobject.Circle is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable 
     at geometricobject.Circle.<clinit>(Circle.java:12) 
     ... 1 more 
Java Result: 1 
+1

이 'Comparable' 인터페이스 란 무엇입니까? 직접 만들었습니까? 만약 그렇다면, 여러분은 안됩니다. stdlib에 이미 'Comparable' 인터페이스가 있습니다. – pajton

답변

3

변경 라인

public abstract class GeometricObject implements Comparable { 

제네릭없이

public abstract class GeometricObject implements Comparable<GeometricObject> { 

에, 당신이 구현해야하는 방법입니다 compareTo(Object)하지 compareTo(GeometricObject)

+0

제안 된 변경 후 ... "main"스레드의 예외 java.lang.ClassCastException : geometricobject.Circle을 geometricobject.GeometricObject에 캐스팅 할 수 없습니다. $ Comparable at geometricobject.GeometricObject.max (GeometricObject.java:61) at geometricobject.GeometricObject.main (GeometricObject.java:52) Java 결과 : 1 – MISMajorDeveloperAnyways

+0

왜 Comparable을 직접 구현 했습니까? –

+0

이 클래스는 Comparable을 구현해야하는 클래스에서 수행하고있는 작업에 따라 지정되며 compareTo를 선언합니다. Comparable 인터페이스 선언은 불필요한가요? – MISMajorDeveloperAnyways

2

그것은 @Override를 사용하는 것이 좋다 주석을 구현할 때마다 또는 어떤 메소드를 오버라이드 (override)한다.

public abstract class GeometricObject implements java.lang.Comparable<GeometricObject> { 

    @Override 
    public int compareTo(GeometricObject o) { 
     int result = 0; 
     //do comparision 
     return result; 
    } 
}