2011-10-10 2 views
1

두 개의 클래스 Circle()과 다른 GeometricObject()가 있습니다. GeometricObject를 원형으로 확장 한 다음 GeometricObject에서 비슷한 기능을 구현해야합니다. 그렇게 할 때 Circle() 클래스에서 추상 메서드를 무시할 수 없다는 오류가 발생하고 Circle()이 추상 메서드가 아닙니다. 나는 또한 테스트/메인 클래스에서 두 가지를 비교해야하지만, 누구든지 내가 어떻게 그 오류를 수정하고 두 가지를 비교할 수있는 아이디어가 있습니까? 미리 감사드립니다.두 객체를 비교, java

package chapter_14; 

public class Circle extends GeometricObject{ //here is where i get an error 
    private double radius; 

    public Circle() { 
    } 

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

    /** Return radius */ 
    public double getRadius() { 
    return radius; 
    } 

    /** Set a new radius */ 
    public void setRadius(double radius) { 
    this.radius = radius; 
    } 

    /** Return area */ 
    @Override 
    public double getArea() { 
    return radius * radius * Math.PI; 
    } 

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

    /** Return perimeter */ 
    @Override 
    public double getPerimeter() { 
    return 2 * radius * Math.PI; 
    } 

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

} 

GeometricObject 클래스 : 슈퍼 클래스의 메소드가 추상적이기 때문에

package chapter_14; 


public abstract class GeometricObject implements Comparable{ 

    private String color = "white"; 
    private boolean filled; 
    private java.util.Date dateCreated; 

    /** Construct a default geometric object */ 
    protected GeometricObject() { 
    dateCreated = new java.util.Date(); 
    } 

    /** Return color */ 
    public String getColor() { 
    return color; 
    } 

    /** Set a new color */ 
    public void setColor(String color) { 
    this.color = color; 
    } 

    /** Return filled. Since filled is boolean, 
    * so, the get method name is isFilled */ 
    public boolean isFilled() { 
    return filled; 
    } 

    /** Set a new filled */ 
    public void setFilled(boolean filled) { 
    this.filled = filled; 
    } 

    /** Get dateCreated */ 
    public java.util.Date getDateCreated() { 
    return dateCreated; 
    } 

    /** Return a string representation of this object */ 
    @Override 
    public String toString() { 
    return "created on " + dateCreated + "\ncolor: " + color + 
     " and filled: " + filled; 
    } 

    /** Abstract method getArea */ 
    public abstract double getArea(); 

    /** Abstract method getPerimeter */ 
    public abstract double getPerimeter(); 

} 

답변

5

문제는 GeometricObjectComparable을 구현하는 선언이다 JDK 7에서 나를 위해.

+0

그래서 하위 클래스도 compareTo 메소드를 상속해야합니다. 고쳐서 이제는 테스트 부분을 알아 내야 만합니다. 도움을 많이 주셔서 감사합니다. – Gmenfan83

+0

@ Jason :'GeometricObject'는 그렇게하지 않기 때문에'Comparable'에 선언 된'compareTo' 메쏘드를 구현해야합니다. –

3

당신은 두 가지 방법을 대체하지 않습니다. 그러므로 당신은 그것들을 구현해야하고 그것들을 오버라이드하지 않아야한다 ("추상적 인 메소드를 무시할 수 없다"와 상응한다).

이 방법을 Circle으로 제공하지 않아도됩니다.

@Override 
public int compareTo(Object o) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

GeometricObject는 클래스가 추상 클래스이므로이 메서드를 구현할 필요가 없습니다. 그러나 Circle은 없기 때문에 모든 메소드를 구현해야합니다 (Comparable은이 메소드를 강제 실행하며 "Circle() is abstract"와 상응 함).

GeometricObject가 추상이므로이 클래스의 인스턴스를 만들 수 없기 때문에 GeometricObject를 Circle과 비교할 수 없습니다.

어떤 구상 서브 클래스는 예를 들어, compareTo 메소드를 구현하는 것을 의미
public abstract class GeometricObject implements Comparable { 

: 그 변화와

@Override public int compareTo(Object o) { 
    Circle circle = (Circle) o; 
    return Double.compare(getArea(), circle.getArea()); 
} 

, 그것은 컴파일

3

이것은 ComparableObject 클래스가을 구현하기 때문입니다. 이인터페이스 :

int compareTo(Object o) 

당신은 아닌 추상 클래스의 모든 추상 메소드를 구현해야합니다. 이 경우, ComparableObject에서 파생 된 모든 클래스에서 구현은 comparTo이므로 추상 메소드이기 때문에 추가해야합니다.