簡體   English   中英

“不是抽象的,不會覆蓋抽象方法”錯誤

[英]“is not abstract and does not override abstract method” error

我收到這個錯誤,這讓我感到困惑。 我不知道如何解決它。 我知道那里還有其他一些錯誤,但我現在並不太擔心。

這是代碼:

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的診斷,但我認為解釋這個錯誤的原因可能是好的,以防你不確定。

抽象方法是對編譯器的一種承諾,即所有子類(使用抽象方法擴展類的子類)都能夠執行該方法 實現該承諾的一部分是子類必須使用完全相同的頭實現抽象方法。 所以在Java運行時,你可以調用任何子類的實例的抽象方法,你卻知道已經使用了正確的參數等等。

為什么? 因為如果子類為該方法使用稍微不同的頭(導致我們試圖避免的運行時錯誤),代碼將不會編譯(它會給出得到的錯誤)。


例如,想象一個抽象類A ,它有一個抽象方法f()和兩個具體的子類BC 在程序的某個地方我們有:

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

我們不知道avar在運行時是B還是C ,因為它取決於if語句的結果。 但我們知道avar將成為A的子類。 由於A所有子類必須使用正確的頭實現f (否則程序員會得到你得到的編譯錯誤)我們知道這個代碼在運行時不會中斷!


在您的代碼中,抽象方法頭與其實現之間的區別是:

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

要修復它,請將double參數添加到抽象方法中。

我能找到的第一個問題是你已經聲明了一個沒有參數的Scale抽象方法,但是在擴展抽象類時,你正在使用參數實現Scale方法。 這就是為什么在所有子類中拋出此錯誤的原因:

Circle不是抽象的,並且不會覆蓋Shape中的抽象方法scale()

還有以下錯誤:

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

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

你忘了在getGreen函數之前添加+符號,Java希望將其作為行尾,這就是為什么錯誤';'

getRed符號錯誤是因為你沒有一個名為getRed的函數? 等等!

在Java中使用類之前,必須首先導入該類。

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

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

在Circle.java和Rectangle.java中,您需要添加:

import Shape;

此外,您還應確保文件位於包中。 http://en.wikipedia.org/wiki/Java_package

嗯,所以它告訴你的原因是,你繼承了“Shape”這個類。 該類定義方法scale()。

因此,要么在“Circle”和“Rectangle”中實現方法scale(),要么將它們定義為asbtract。 我建議你只是實現scale()方法,不要把它們變成抽象,因為我認為這是你打算做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM