3

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

这是代码:

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
4

4 回答 4

6

我同意 user1490835 的诊断,但我认为最好解释一下这个错误的原因,以防万一你不确定。

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

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


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

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

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


在您的代码中,抽象方法标头与其实现之间的区别是:

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

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

于 2012-10-11T03:05:22.413 回答
1

我能找到的第一个问题是您已经声明了一个不带参数的 Scale 抽象方法,但是当您扩展抽象类时,您正在实现带参数的 Scale 方法。这就是为什么在您的所有子类中都会引发此错误的原因:

Circle 不是抽象的,不会覆盖 Shape 中的抽象方法 scale()

还出现以下错误:

./Shape.java:46: ';' expected

return "Position: (" + getX() + ", " + getY() + ") Color: (" + 
getRed() + getBlue() + ", " getGreen() + ")";

您忘记在 getGreen 函数之前添加 + 符号,Java 期望它作为行尾,所以这就是错误 ';' 的原因

getRed 符号错误是因为您没有名为 getRed 的函数?等等!

于 2012-10-11T02:10:35.760 回答
0

在使用 Java 中的类之前,您必须先导入该类。

在 ShapeTest.java 中,您需要添加:

import Circle;
import Rectangle;
import java.awt.Color;

在 Circle.java 和 Rectangle.java 你需要添加:

import Shape;

此外,您还应该确保您的文件在一个包中。见http://en.wikipedia.org/wiki/Java_package

于 2012-10-11T02:11:37.493 回答
0

嗯,所以它告诉你的原因是,你继承自“Shape”类。该类定义了方法 scale()。

因此,要么在“Circle”和“Rectangle”中实现方法 scale(),要么将它们定义为 asbtract。我建议你只实现 scale() 方法,不要将它们变成抽象的,因为我认为这是你打算做的。

于 2012-10-11T02:14:10.780 回答