I am getting this error and it is confusing me. I do not know how to fix it. I know there are some other errors in there but i'm not too worried about those for now.

我犯了这个错误,这让我很困惑。我不知道怎么修理它。我知道还有其他的错误,但我现在不太担心这些。

Here is the code:

这是代码:

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() + ")";

} }

} }

Here are the class files:

以下是班级档案:

// 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();
}
}

And here is the test file:

这是测试文件:

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()); 


}
}

Here are the errors:

这里是错误的:

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 个解决方案

#1


6

I agree with user1490835's diagnosis, but I thought it might be good to explain what's going on with this error, just in case you're unsure.

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

An abstract method is a promise to the compiler that all child classes (ones extending the class with the abstract method) will be able to do that method. Part of fulfilling that promise is that the child classes have to implement the abstract method using the exact same header. So in the Java runtime, you can call the abstract method on an instance of any of the child classes and know you'll have used the correct parameters and so on.

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

Why? Because if the subclass used a slightly different header for that method (causing the runtime error we're trying to avoid) the code would not have compiled (it would've given the error you're getting).

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


For example, imagine an abstract class A that has an abstract method f() and two concrete subclasses B and C. Somewhere in the program we have:

例如,假设有一个抽象类A,它有一个抽象方法f()和两个具体的子类B和c。

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

We don't know wether avar will be a B or a C at runtime, because it depends on the result of the if statement. But we do know that avar will be a subclass of A. Since all subclasses of A must have implemented f with the correct header (otherwise the programmer would have gotten the compile error you're getting) we know this code won't break while it's running!

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


In your code, the difference between the abstract method header and its implementation is:

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

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

To fix it, add the double parameter to your abstract method.

要修复它,请向抽象方法添加双参数。

更多相关文章

  1. 手低眼高 初学者学习Hibernate的方法
  2. Java常用类及其常用方法
  3. 本地方法中printf如何传给java--java系统级命名管道
  4. 为泛型类的泛型方法的属性赋值 - Java
  5. java线程池使用场景和使用方法较详细文摘
  6. java中多线程安全问题产生&解决方案——同步方法
  7. java的学习方法(转自黑马程序员)
  8. 是否有缩放因子方法用于Android的多分辨率支持
  9. Java提高篇——equals()方法和“==”运算符

随机推荐

  1. Android(安卓)TableLayout中的使用说明
  2. Android入门介绍
  3. EditText中的几个常用属性
  4. Android 自带的一些可用于Activity的Them
  5. android的五大布局
  6. Android之新建项目
  7. 【Android】Android android:launchMode=
  8. Android编程: 调试方法
  9. Android 签名打包(cmd命令行)
  10. Android中的Layout_weight详解