2016-07-23 5 views
0
public abstract class Shape{ 

    protected Point position; 

    public Shape (Point p) 
    { 
     this.position=new Point(p); 
    } 

    public abstract int getArea(); 
    public abstract int gerPerimeter(); 
    public abstract boolean overlap(Shape other); 

} 
public class Rectangle extends Shape 
{ 
    public int width; 
    public int height; 

    public Rectangle(Point position,int width,int height) 
    { 
     super(position); 
     this.width=width; 
     this.height=height; 
    } 
    @Override 
    public int getArea() 
    { 
     return width*height; 
    } 
    @Override 
    public int getPerimeter() 
    { 
     return width*2+height*2; 
    } 
    @Override 
    public boolean overlap(Rectangle other) 
    { 
     return false; 
    } 
} 

Rectangle.java:1: error: Rectangle is not abstract and does not override abstract method overlap(Shape) in Shape抽象クラスのエラーは、単純なクラス

public class Rectangle extends Shape
^

Rectangle.java:17: error: method does not override or implement a method from a supertype

@Override
^

Rectangle.java:22: error: method does not override or implement a method from a supertype

@Override
^

3 errors

答えて

0

Rectangleoverlap方法は、それを上書きするために、親のクラスメソッドと同じシグネチャを持つ必要があります。

@Override 
public boolean overlap(Shape other) 
{ 
    return false; 
} 

この方法public boolean overlap(Rectangle other)

1

をし、この

public abstract boolean overlap(Shape other);は事実です

ていても、同じではありません:Rectangle年代に渡さはRectangleことoverlap、あなたはinstanceofを使用して種類を確認することができます長方形延長/実装 ...

抽象クラスのの

ので、技術的にはあなたが上書きされていないすべてのメソッド...

オーバーライド注釈があなたに与えて、そのメソッドは、スーパークラスで見つけることができるので、文句を言う....

0

エラーメッセージ

Rectangle.java:17: error: method does not override or implement a method from a supertype

@Override

のために、あなたのRectangleクラスだけ何か

のようなことを意味形状クラスをサブクラス化されることがわかります

は、Javaがスーパークラスにメソッドoverlap(Rectangle other)を持つ必要があるために間違っています。代わりに、それはoverlap(Shape other)を見て、彼らは完全に異なっています。 解決方法:まだメソッドが必要な場合は、@Overrideアノテーションを削除してください。

エラーメッセージ

Rectangle.java:22: error: method does not override or implement a method from a supertype

@Override

についてさてあなたはまだメソッドをオーバーライドしていない、まだどのあなたがしなければなりません。 ソリューション:overlap(Shape other)overlap(Rectangle other)を変更したり、完全にこのような新しいオーバーライドメソッドを記述し、次のいずれか

@Override 
public boolean overlap(Shape other) 
{ 
    return false; 
} 

が、これは助けを願っています。