[英]what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean?
I hit a roadblock in my code. 我在代码中遇到了障碍。 this is the class in hand.
这是手头的课程。
public class StartRoom extends Room implements ActionListener {
JButton buttonTwo;
public StartRoom() {
start();
buttonOne = new JButton("Go to door.");
buttonTwo = new JButton("Look at skeleton.");
label = new JLabel("You walk into the dungeon, the room is covered with vines. There is a skeleton sitting near the northern door. What do you do?");
panelOne.add(label);
panelOne.add(buttonOne);
buttonOne.addActionListener(this);
buttonTwo.addActionListener(this);
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(java.awt.event.ActionEvent ae) {
}
}
public static void main( String[]args ) {
new StartRoom();
}
}
It says that The type StartRoom must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
on line five, but I can't figure it out what it's asking! 它说类型StartRoom必须在第五行实现继承的抽象方法
java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
,但我无法弄清楚它在问什么!
StartRoom implements ActionListener
means StartRoom
should assumes the contract of ActionListener
. StartRoom implements ActionListener
意味着StartRoom
应该承担ActionListener
的约定。 The method actionPerformed( ActionEvent)
must be implemented by yourself. 方法
actionPerformed( ActionEvent)
必须由您自己实现。
public class StartRoom extends Room implements ActionListener {
...
@Override
public void actionPerformed(java.awt.event.ActionEvent ae) {
// your code here....
}
}
If you want to delegate to another class, MyActionListener
, for example, you have to change the usage in buttonTwo.addActionListener(this);
例如,如果要委托给另一个类
MyActionListener
,则必须在buttonTwo.addActionListener(this);
更改用法buttonTwo.addActionListener(this);
, replacing this
by an instance of MyActionListener
. ,取代
this
通过的一个实例MyActionListener
。
MyActionListener toto = new MyActionListener();
buttonTwo.addActionListener( toto );
In the later case you mshould remove implements ActionListener
from StartRoom
class declaration. 在后一种情况下,您应该从
StartRoom
类声明中删除implements ActionListener
。
When you implement something you are using that class as an interface, which means you must use and redefine every method in the class you are implementing. 当您实现某些东西时,您将该类用作接口,这意味着您必须使用并重新定义要实现的类中的每个方法。 However, if you want to use that class's method(s) as-is then you may extend it and make that class a superclass of your class.
但是,如果您想按原样使用该类的方法,则可以对其进行扩展,并使该类成为该类的超类。 Keep in mind that you may only extend one class, but you can implement a large number of classes as interfaces.
请记住,您只能扩展一个类,但是您可以实现大量的类作为接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.