Java 实现继承的抽象方法?
我的代码中有错误,但我不确定错误是什么意思,我尝试过研究错误,但似乎与我的代码无关,有人能帮我吗 我的代码强>Java 实现继承的抽象方法?,java,syntax,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
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
接口包含方法actionPerformed
,因此当您实现该接口时,必须覆盖actionPerformed
方法。该方法的正确定义为:
public void actionPerformed(ActionEvent e)
不确定它是否只是一个输入错误,但您有两个方法actionPerformed3
和actionPerformed4
,它们实际上并没有覆盖ActionListener
类actionPerformed
方法
为避免此类问题,在试图覆盖的方法之上使用@Override
注释总是很有帮助的。所以这是更好更安全的:
@Override
public void actionPerformed(ActionEvent e) {
// method body
}
是一个接口
因此,在实现此接口时,需要覆盖actionPerformed(ActionEvent e)
方法:
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// do something
// maybe call actionPerformed3(e)?
}
}
您需要在事件类定义中实现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);
}
}
因为您要实现一个接口,所以需要覆盖其中的所有方法
@override
public void actionPerformed(ActionEvent e)
{
//some code.
}
添加此代码并重试
规则-:如果类不是抽象的,则必须重写在接口中定义的所有方法。您需要实现方法public void actionPerformed(ActionEvent s)
。您可以阅读关于接口的内容:您正在使用Eclipse,对吗?因此,转到“窗口”菜单,打开名为“问题”的视图。应该始终打开此视图,并且当编译错误仍在此视图中列出时,您甚至不应该尝试启动应用程序。顺便说一句,Eclipse可能在启动程序时警告您有非编译类,但您可能选择忽略该警告。不要。并在启动程序之前修复编译错误。您应该使用@Override注释,这有助于尽早检测此类错误。您好,谢谢您的回复,我理解你在这里的意思,但我正在尝试为我的菜单创建多个功能,但我必须将操作执行3
或操作执行4
的名称更改为操作执行
,以便@覆盖
。但我只能执行一个操作
。我如何创建多个执行的操作
,并且仍然能够@覆盖所有方法以确保它们都正常工作?