gpt4 book ai didi

java - 抽象类错误简单类

转载 作者:行者123 更新时间:2023-12-01 09:39:57 24 4
gpt4 key购买 nike

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

最佳答案

这个方法public boolean Overlap(Rectangle other)和这个

publicabstractbooleanoverlap(Shapeother);不一样,

即使矩形扩展/实现形状是真的...

所以从技术上讲,你并没有重写抽象类的所有方法......

Override 注释给你一个提示,因为该方法可以在父类(super class)中找到......

关于java - 抽象类错误简单类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com