2016-07-23 124 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; 
} 

如果您需要Shape传递给RectangleoverlapRectangle,您可以检查使用instanceof类型:

1

这种方法public boolean overlap(Rectangle other)

public abstract boolean overlap(Shape other);是不一样的,

即使是真的, 矩形延伸/实现形状 ...

所以在技术上你不覆盖所有的抽象类的方法...

覆盖诠释给你一个抱怨,因为该方法可以在超类中找到....

0

对于错误信息

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

@Override

,你会发现你的Rectangle类只是子类的形状类,这意味着像

@Override 
public boolean overlap(Rectangle other) 

会出错,因为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(Rectangle other)overlap(Shape other)或完全写新的替换方法是这样的:

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

希望这有助于。