gpt4 book ai didi

java - "is not abstract and does not override abstract method"错误

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:30:13 34 4
gpt4 key购买 nike

我遇到了这个错误,这让我很困惑。我不知道如何解决它。我知道那里还有一些其他错误,但我现在不太担心这些错误。

代码如下:

import java.awt.*;

public abstract class Shape {
// Instance variables
private int x;
private int y;
private Color color;

// Constructor
protected Shape(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}

// Abstract methods
public abstract void draw(Graphics g);
public abstract int getHeight();
public abstract int getWidth();
public abstract void scale();
public abstract double area();

// Other instance methods
public Color getColor() {
return color;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void move(int dx, int dy) {
x += dx;
y += dy;
}

public void setColor(Color color) {
this.color = color;
}

public String toString() {
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";

}

类文件如下:

// Represents a circle that can be displayed in a graphics
// context

import java.awt.*;

public class Circle extends Shape {
// Instance variables
private int diameter;
private double scaleFactor = 2;
private double area;

// Constructor
public Circle(int x, int y, Color color, int diameter) {
super(x, y, color);
this.diameter = diameter;
}

// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillOval(getX(), getY(), diameter, diameter);
}

public int getHeight() {
return diameter;
}

public int getWidth() {
return diameter;
}

public void scale(double scaleFactor) {
(double) diameter *= scaleFactor;
}

public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}

public String toString() {
return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " + getHeight();
}
}

import java.awt.*;

public class Rectangle extends Shape {
// Instance variables
private int width;
private int height;
private double scaleFactor = 2;
private double diameter = width * height;
private double area;

// Constructor
public Rectangle(int x, int y, Color color,
int width, int height) {
super(x, y, color);
this.width = width;
this.height = height;
}

// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(getX(), getY(), width, height);
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public void scale(double scaleFactor){
width *= scaleFactor;
height *= scaleFactor;
}

public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}

public String toString() {
return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " +
getHeight();
}
}

这是测试文件:

public class ShapeTest
{
public static void main(String[] args)
{
Color myC = new Color(10, 30, 20);
Circle myCircle = new Circle(0, 0, myC, 5);
Rectangle myRectangle = new Rectangle(0, 0, myC, 2, 3);

System.out.println(myCircle.toString());
System.out.println(myRectangle.toString());


}
}

错误如下:

javac ShapeTest.java
ShapeTest.java:5: cannot find symbol
symbol : class Color
location: class ShapeTest
Color myC = new Color(10, 30, 20);
^
ShapeTest.java:5: cannot find symbol
symbol : class Color
location: class ShapeTest
Color myC = new Color(10, 30, 20);
^
./Shape.java:46: ';' expected
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: not a statement
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + g etBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getRed()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getBlue()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getGreen()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Circle.java:16: Circle is not abstract and does not override abstract method scale() in Shape
public class Circle extends Shape {
^
./Circle.java:43: unexpected type
required: variable
found : value
(double) diameter *= scaleFactor;
^
./Rectangle.java:3: Rectangle is not abstract and does not override abstract method scale() in Shape
public class Rectangle extends Shape {
^
10 errors

最佳答案

我同意 user1490835 的诊断,但我认为最好解释一下这个错误是怎么回事,以防你不确定。

抽象方法是对编译器的 promise ,即所有子类(使用抽象方法扩展类的子类)都将能够执行该方法。实现该 promise 的一部分是子类必须使用完全相同的 header 来实现抽象方法。因此,在 Java 运行时中,您可以在任何 子类的实例上调用抽象方法,并知道您将使用正确的参数等等。

为什么?因为如果子类为该方法使用稍微不同的 header (导致我们试图避免的运行时错误),代码将不会编译(它会给出得到的错误) .


例如,假设一个抽象类 A 有一个抽象方法 f() 和两个具体子类 BC 。在程序的某处我们有:

    A avar;
if (somevar < othervar) {
avar = new B();
} else {
avar = new C();
}
avar.f();

我们不知道 avar 在运行时是 B 还是 C,因为它取决于 if 的结果陈述。但是我们知道 avar 将是 A 的子类。由于 A 的所有子类都必须使用正确的 header 实现 f(否则程序员会得到您遇到的编译错误)我们知道这段代码不会中断当它运行时!


在你的代码中,抽象方法头和它的实现的区别是:

public abstract void scale(); // no parameter
public void scale(double scaleFactor) { // one parameter

要修复它,请将双参数添加到您的抽象方法中。

关于java - "is not abstract and does not override abstract method"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831141/

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