gpt4 book ai didi

java - 实现继承的抽象方法?

转载 作者:行者123 更新时间:2023-12-01 13:38:23 26 4
gpt4 key购买 nike

我的代码中出现错误,但我不确定该错误意味着什么,我已尝试研究该错误,但似乎与我的代码无关,有人可以帮助我吗?

我的代码;

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)

我不确定错误是什么或如何修复它。

感谢任何帮助,谢谢。

最佳答案

ActionListener 接口(interface)包含方法 actionPerformed,因此当您实现该接口(interface)时,必须重写 actionPerformed 方法。该方法的正确定义是:

public void actionPerformed(ActionEvent e) 

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

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

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

关于java - 实现继承的抽象方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21061721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com