Java 类不是抽象的,不会覆盖抽象方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23452097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:03:46  来源:igfitidea点击:

Class is not abstract and does not override abstract method

javainheritanceabstract-classdraw

提问by user063

So I've been working on a homework on abstraction for my programming class and fell into a problem. The goal for me right now is to be able to use abstraction, then later be able to draw with rectangles and ovals a simple city, like a rectangular building or a oval light on a light post.

所以我一直在为我的编程课做一个关于抽象的作业,但遇到了一个问题。我现在的目标是能够使用抽象,然后能够用矩形和椭圆形绘制一个简单的城市,例如矩形建筑物或灯柱上的椭圆形灯。

The error I am receiving when I compile is: MyTestApp.Rectangle is not abstract and does not override abstract method drawEllipse(java.awt.Graphics) in MyTestApp.Shape. This Error shows up on the line "class Rectangle extends Shape{" right below the class Shape.

我在编译时收到的错误是:MyTestApp.Rectangle 不是抽象的,并且不会覆盖 MyTestApp.Shape 中的抽象方法 drawEllipse(java.awt.Graphics)。此错误显示在类 Shape 正下方的“class Rectangle extends Shape{”行上。

My question is what am I doing wrong with my abstraction? I've been messing with the constructors and draw() methods in classes Rectangle and Ellipse for a while now and still to no luck happen to find a solution.

我的问题是我的抽象做错了什么?我一直在搞乱 Rectangle 和 Ellipse 类中的构造函数和 draw() 方法有一段时间了,但仍然没有运气找到解决方案。

Code is below:

代码如下:

import java.awt.*;
import javax.swing.*;

public class MyTestApp extends JPanel {
    Rectangle rect;
    Ellipse oval;
    public static void main(String [] args) {
        MyTestApp myTestApp = new MyTestApp ();
        myTestApp.test();
    }

    public MyTestApp () { //creates the jframe
        JFrame frame = new JFrame("MyClass Driver");
        setBackground(new Color(200, 250, 200));
        setPreferredSize(new Dimension(500, 400));
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

    public void delay(int msecs) {
        try {
            Thread.sleep(msecs);
        } catch (InterruptedException e) {
        }
    }

    public void paint(Graphics g) {//paints the rectangle and ellipse
        super.paint(g);
        if (rect != null)
            rect.drawRectangle(g);
        if (oval != null)
            oval.drawEllipse(g);
    }

    public void test() {//gives the x/y position, width/height, and fill/outline color for the rectangle and oval
        delay(1000);
        rect = new Rectangle(20, 30, 23, 75, Color.GREEN, Color.BLUE);
        oval = new Ellipse(10, 10, 10 , 34, Color.RED, Color.MAGENTA);
        repaint();
    }

    public abstract class Shape{//abstract class Shape that sets the x/y, width/height, and colors for the shapes
        private int x, y, width, height;
        private Color fillColor;
        private Color outlineColor;
        public Shape(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            setXY(x, y);
            setSize(width, height);
            setFillColor(fillColor);
            setOutlineColor(outlineColor);  
        }

        public boolean setXY(int x, int y) {
            this.x = x;
            this.y = y;
            return true;
        }

        public void setSize(int width, int height) {
            if (width > 0)
                this.width = width;
            if (height > 0)
                this.height = height;
        }

        public boolean setFillColor(Color fillColor){
            if (fillColor == null) return false;
            this.fillColor = fillColor; 
            return true;
        }

        public boolean setOutlineColor(Color outlineColor){
            if (outlineColor == null) return false;
            this.outlineColor = outlineColor; 
            return true;
        }

        public Color getFillColor() {
            return fillColor;
        } 

        public Color getOutlineColor() {
            return outlineColor;
        } 

        public abstract void drawRectangle(Graphics g);//do i need two?
        public abstract void drawEllipse(Graphics g);//do i need both?
    }
    class Rectangle extends Shape{//!!!!!!!!!! where the error shows
        public Rectangle(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            super(x, y, width, height, fillColor, outlineColor);
        }

        public void drawRectangle(Graphics g){//draws the retangle
            g.setColor(fillColor);
            g.fillRect(x, y, width, height);
            g.setColor(outlineColor);
            g.drawRect(x, y, width, height);
        }
    }
    class Ellipse extends Shape{
        public Ellipse(int x, int y, int width, int height, Color fillColor, Color outlineColor) {
            super(x, y, width, height, fillColor, outlineColor);
        }

        public void drawEllipse(Graphics g){//draws the ellipse
            g.setColor(fillColor);
            g.fillOval(x, y, width, height);
            g.setColor(outlineColor);
                g.drawOval(x, y, width, height);
            }
        }
}

Thanks for reading and helping!

感谢阅读和帮助!

采纳答案by Breeze

Both classes Rectangle and Ellipse need to override both of the abstract methods.

Rectangle 和 Ellipse 类都需要覆盖这两个抽象方法。

To work around this, you have 3 options:

要解决此问题,您有 3 个选择:

  • Add the two methods
  • Make each class that extends Shape abstract
  • Have a single method that does the function of the classes that will extend Shape, and override that method in Rectangle and Ellipse, for example:

    abstract class Shape {
        // ...
        void draw(Graphics g);
    }
    
  • 添加两个方法
  • 使每个扩展 Shape 的类抽象
  • 有一个方法来执行将扩展 Shape 的类的功能,并在 Rectangle 和 Ellipse 中覆盖该方法,例如:

    abstract class Shape {
        // ...
        void draw(Graphics g);
    }
    

And

    class Rectangle extends Shape {
        void draw(Graphics g) {
            // ...
        }
    }

Finally

最后

    class Ellipse extends Shape {
        void draw(Graphics g) {
            // ...
        }
    }

And you can switch in between them, like so:

您可以在它们之间切换,如下所示:

    Shape shape = new Ellipse();
    shape.draw(/* ... */);

    shape = new Rectangle();
    shape.draw(/* ... */);

Again, just an example.

再次,只是一个例子。

回答by Platinum Azure

If you're trying to take advantage of polymorphic behavior, you need to ensure that the methods visible to outside classes (that need polymorphism) have the same signature. That means they need to have the same name, number and order of parameters, as well as the parameter types.

如果您试图利用多态行为,则需要确保对外部类(需要多态性)可见的方法具有相同的签名。这意味着它们需要具有相同的名称、数量和参数顺序,以及参数类型。

In your case, you might do better to have a generic draw()method, and rely on the subclasses (Rectangle, Ellipse) to implement the draw()method as what you had been thinking of as "drawEllipse" and "drawRectangle".

在您的情况下,您最好拥有一个泛型draw()方法,并依靠子类 ( Rectangle, Ellipse) 将draw()方法实现为您一直认为的“drawEllipse”和“drawRectangle”。