它不是抽象的,不会覆盖抽象方法 [英] It's not abstract does not override abstract method

查看:137
本文介绍了它不是抽象的,不会覆盖抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我从下面的代码中收到此编译错误消息?

Why am I getting this compile error message from the code below?

(程序根据按下的箭头键在4个方向上移动箭头$ b键盘上的$ b:d)

(The program moves an arrow round in 4 directions depending on which arrow key is pressed on the keyboard:d)


Direction.java:41:错误:DirectionPanel.DirectionListener不是
abstract和不重写抽象方法keyReleased(KeyEvent)
in KeyListener

Direction.java:41: error: DirectionPanel.DirectionListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener



private class DirectionListener implements KeyListener  {

    //Direction.java

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

    class DirectionPanel extends JPanel  {

      private final int WIDTH = 300, HEIGHT = 200;
      private final int JUMP = 10; //increment for image movement
      private final int IMAGE_SIZE = 31;
      private ImageIcon up, down, right, left, currentImage;
      private int x, y;

      //constructor:
      public DirectionPanel ()  {

      addKeyListener (new DirectionListener());
      x =WIDTH / 2;
      y = HEIGHT / 2;
      up = new ImageIcon ("arrowUp.gif");
      down = new ImageIcon ("arrowDown.gif");
      left = new ImageIcon ("arrowLeft.gif");
      right = new ImageIcon ("arrowRight.gif");

      currentImage = right;

      setBackground (Color.black);
      setPreferredSize (new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      }

      //draws
      public void paintComponent (Graphics page)  {
        super.paintComponent (page);
        currentImage.paintIcon (this, page, x, y);
      }

      //represents the listener for keyboard activity
      private class DirectionListener implements KeyListener  {
        public void keyPressed (KeyEvent event)  {
          switch (event.getKeyCode())  {
            case KeyEvent.VK_UP:
                currentImage = up;
                y -= JUMP;
                break;
            case KeyEvent.VK_DOWN:
                currentImage = down;
                y += JUMP;
                break;
            case KeyEvent.VK_LEFT:
                currentImage = left;
                x -= JUMP;
                break;
            case KeyEvent.VK_RIGHT:
                currentImage = right;
                x += JUMP;
                break;
          }
        repaint();
        }
      //empty definitions for unused methods
      private void KeyTyped (KeyEvent event) {}
      private void KeyReleased (KeyEvent event) {}

      }
    }


    public class Direction  {

      public static void main (String[] args)  {

      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add (new DirectionPanel());
      frame.pack();
      frame.setVisible(true);
      }
    }


推荐答案

错误meesage是

DirectionPanel.DirectionListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener private class DirectionListener implements KeyListener

很明显,你的类不会覆盖它需要的所有方法,例如的keyReleased 。如果你看你的班级,你有 KeyReleased ,它是私人

So obviously your class does not override all the methods it needs to, for example keyReleased. If you look at your class, you have KeyReleased and it's private.

重命名这些

private void KeyTyped (KeyEvent event) {}
private void KeyReleased (KeyEvent event) {}

keyTyped keyReleased (注意小写 k )并将它们设为 public ,因为Java不允许子类型降低重写方法的可见性。然后使用 @Override 注释每个。

to keyTyped and keyReleased (note the lowercase k) and make them public since Java doesn't allow sub types to reduce the visibility of an overriden method. Then annotate each with @Override.

使用 @Override 注释方法会导致编译错误(如果它实际上没有覆盖)。 JLS的第9.6.1.4节说:

Annotating a method with @Override causes a compile error if it doesn't actually override. Section 9.6.1.4 of the JLS says:


注释类型覆盖支持早期检测此类问题。如果使用注释@Override注释方法声明,但该方法实际上不会覆盖超类中声明的任何方法,则会发生编译时错误。

The annotation type Override supports early detection of such problems. If a method declaration is annotated with the annotation @Override, but the method does not in fact override any method declared in a superclass, a compile-time error will occur.

这篇关于它不是抽象的,不会覆盖抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆