实现继承的抽象方法?

时间:2014-01-11 11:33:22

标签: java syntax

我的代码中出现错误,但我不确定错误意味着什么,我已经尝试过研究错误,但似乎没有任何与我的代码相关的内容,是否有人可以帮助我?

我的代码;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;

import javax.swing.*;


public class Assignment4Test {

public static void main(String[] args) throws IOException {
    Scanner console = new Scanner(System.in);
    BufferedReader in = new BufferedReader(new FileReader("shop-account"));

    final int Username = 3387;
    final int Password = 5183;
    final int AccountNumber = 22334455;

    int EnteredUsername;
    int EnteredPassword;
    int EnteredAccountNumber;
    for (int s = 0; s <= 3; s++) {
        if (s < 3) {
            System.out.println("Enter Username");
            EnteredUsername = console.nextInt();
            System.out.println("Username Entered is " + EnteredUsername);
            System.out.println("Enter Password");
            EnteredPassword = console.nextInt();
            System.out.println("Password Entered is " + EnteredPassword);
            System.out.println("Enter Account Number");
            EnteredAccountNumber = console.nextInt();
            System.out.println("Account Number Entered is " + EnteredAccountNumber);
            if (Username == EnteredUsername && (Password == EnteredPassword)
                    && (AccountNumber == EnteredAccountNumber)) {
                System.out.println("Welcome");
                System.out.println("Account username, password, account number and current balance and shown below;");
                String line;
                while((line = in.readLine()) != null)
                {
                    System.out.println(line);
                }
                new MyFrame().displayGui();
                break;
            } else {
                System.out.println("Wrong Username, Password or Account Number. Please try again.");
            }
        } else {
            System.out.println("3 incorrect enteries detected. Program is terminating, goodbye!");
        }
    }
}

static class MyFrame extends JFrame {

    JMenuBar menubar;

    JMenu TransferAnAmount;
    JMenuItem TransferAnAmountToAnotherAccount;

    JMenu ListRecentTransactions;
    JMenuItem ShowList;

    JMenu DisplayCurrentBalance;
    JMenuItem ShowBalance;

    JMenu ExitProgram;
    JMenuItem Exit;

    public MyFrame() {

        setLayout(new FlowLayout());

        menubar = new JMenuBar();
        setJMenuBar(menubar);

        TransferAnAmount = new JMenu("Transfer An Amount");
        menubar.add(TransferAnAmount);

        ListRecentTransactions = new JMenu("List Recent Transactions");
        menubar.add(ListRecentTransactions);

        DisplayCurrentBalance = new JMenu("Display Current Balance");
        menubar.add(DisplayCurrentBalance);

        ExitProgram = new JMenu("Exit Program");
        menubar.add(ExitProgram);

        TransferAnAmountToAnotherAccount = new JMenuItem("Transer an amount to another account");
        TransferAnAmount.add(TransferAnAmountToAnotherAccount);

        ShowList = new JMenuItem("Show List");
        ListRecentTransactions.add(ShowList);

        ShowBalance = new JMenuItem("Show Balance");
        DisplayCurrentBalance.add(ShowBalance);
        event s = new event();
        ShowBalance.addActionListener(s);

        Exit = new JMenuItem("Exit Program");
        ExitProgram.add(Exit);
        event e = new event();
        Exit.addActionListener(e);

    }

    class event implements ActionListener  {
        public void actionPerformed3(ActionEvent s) throws IOException {
            BufferedReader in = new BufferedReader(new FileReader("shop-account"));
            String line;
            while((line = in.readLine()) != null)
            {
                System.out.println(line);
            }
        }
        public void actionPerformed4(ActionEvent e) {
            System.exit(0);

        }

    }

    public void displayGui() {
        MyFrame gui = new MyFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(600, 300);
        gui.setVisible(true);

    }

}
}

我的代码中的这一行给出了错误;

    }

    class event implements ActionListener  { // <<< This line
        public void actionPerformed3(ActionEvent s) throws IOException {
            BufferedReader in = new BufferedReader(new FileReader("shop-account"));
            String line;
            while((line = in.readLine()) != null)
            {
                System.out.println(line);
            }
        }
        public void actionPerformed4(ActionEvent e) {
            System.exit(0);

        }

    }

给出的错误是;

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    The type Assignment4Test.MyFrame.event must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

我不确定错误是什么或如何解决。

感谢任何帮助,谢谢。

4 个答案:

答案 0 :(得分:1)

ActionListener接口包含方法actionPerformed,因此在实现该接口时,必须覆盖actionPerformed方法。该方法的正确定义是:

public void actionPerformed(ActionEvent e) 

不确定这只是一个拼写错误,但您有两种actionPerformed3actionPerformed4方法实际上并没有覆盖ActionListeneractionPerformed方法。

为避免此类问题,在您尝试覆盖的方法之上使用@Override注释总是有帮助的。所以这更好更安全:

@Override        
public void actionPerformed(ActionEvent e)  {
           // method body
}

答案 1 :(得分:0)

ActionListener是一个界面。

因此,在实施此界面时,您需要override actionPerformed(ActionEvent e)方法:

class MyListener implements ActionListener  { 
    @Override
    public void actionPerformed(ActionEvent e) {
        // do something
        // maybe call actionPerformed3(e)?
    }
}

答案 2 :(得分:0)

您需要在事件类定义

中实现public void actionPerformed(ActionEvent e)
class event implements ActionListener  {

    @Override   
    public void actionPerformed(ActionEvent e) {
    // you need to implement this
    }
            public void actionPerformed3(ActionEvent s) throws IOException {
                BufferedReader in = new BufferedReader(new FileReader("shop-account"));
                String line;
                while((line = in.readLine()) != null)
                {
                    System.out.println(line);
                }
            }
            public void actionPerformed4(ActionEvent e) {
                System.exit(0);

        }

    }

答案 3 :(得分:0)

因为您正在实现一个接口,所以需要覆盖其中的所有方法。

    @override
    public void actionPerformed(ActionEvent e) 

    {

    //some code.

    }

添加此代码并尝试。

规则 - :如果您的类不是抽象的,则必须覆盖您正在实现的接口中定义的所有方法。

相关问题