在覆盖的方法中访问声明的方法 java
Accessing a declared method inside an overridden method java
我在从另一个 class 访问在 main 中声明的对象时遇到了一些麻烦:
public static void main(String[]args)
{
Knight knight=new Knight();
Room2 room2=new Room2()
}
问题是我无法在这个特定方法中将对象作为参数传递,因为它是 actionListener
重写的方法,所以像这样 Accessing objects of other classes
不行。
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
我不能在 ActionEvent e
参数后传递任何额外的东西
所以我想问一下 knight
对象是否可以在此 class 中被识别,或者我是否必须做一些完全不同的事情?
谢谢
编辑:
主要方法是简单地创建像 knight
这样的字符对象,它是一个具体的 class:
public class Room2 extends JFrame implements ActionListener{
private JButton attack;
private JButton defend;
//private JLabel item1read;
CardLayout cl=new CardLayout();
GridBagConstraints gb=new GridBagConstraints();
JPanel panel =new JPanel();
public Room2()
{
//******************************
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
}
这一行...
Knight knight=new Knight();
...仅在 main 方法中有作用域。
要么您必须实例化应用程序的具体对象并通过 actionPerformed 处理程序中的 getKnight() 方法访问 Knight。
public class TestClass {
Knight knight;
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.run();
}
public Knight getKnight() {
return this.knight;
}
public void run() {
this.knight = new Knight();
// Do other stuff, add event handler, etc. Then you can access via getKnight() method.
}
}
或者您必须将 Knight 对象定义为主应用程序 class 的静态变量,并从 actionPerformed 处理程序静态访问它,例如
public static Knight knight;
这是不太可取的,因为它使其他 classes 更容易看到骑士,并且可以在包含它的主要 class 之外轻松更改。
根据您的代码,您可能希望将 Knight 实例传递到 Room 实例中,也许是通过其构造函数:
private Knight knight;
public Room2(final Knight knight) {
this.knight = knight;
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
public void actionPerformed(ActionEvent e) {
if(e.getSource()==attack){
knight.getHealth();
}
}
}
并在创建时传入:
Room room = new Room(knight);
例如,
public static void main(String[]args) {
Knight knight=new Knight();
Room2 room2=new Room2(knight);
}
更妙的是,如果 Room2 代表一个物理房间,而骑士代表一个真正的骑士,那么我会将我的模型建立在现实之上——骑士并不总是在一个房间内,所以我会建立我对这个现实的代码。也许给 Room2 一个 enter(...)
和 exit(...)
方法对,允许 GameManager class 调用这些方法的能力。 Room 也可以有一个 public List<Participants> listParticipants()
或类似的方法来列出所有骑士、怪物或任何碰巧在那个房间里的东西。那么房间的行为可能取决于它的居住者。
同样,您可以给所有参与者一个位置字段以及一个 setLocation(...)
方法和一个 getter 方法,这样所有参与者的行为(包括骑士的行为)都可以改变取决于他们的位置。
我在从另一个 class 访问在 main 中声明的对象时遇到了一些麻烦:
public static void main(String[]args)
{
Knight knight=new Knight();
Room2 room2=new Room2()
}
问题是我无法在这个特定方法中将对象作为参数传递,因为它是 actionListener
重写的方法,所以像这样 Accessing objects of other classes
不行。
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
我不能在 ActionEvent e
参数后传递任何额外的东西
所以我想问一下 knight
对象是否可以在此 class 中被识别,或者我是否必须做一些完全不同的事情?
谢谢
编辑:
主要方法是简单地创建像 knight
这样的字符对象,它是一个具体的 class:
public class Room2 extends JFrame implements ActionListener{
private JButton attack;
private JButton defend;
//private JLabel item1read;
CardLayout cl=new CardLayout();
GridBagConstraints gb=new GridBagConstraints();
JPanel panel =new JPanel();
public Room2()
{
//******************************
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
}
这一行...
Knight knight=new Knight();
...仅在 main 方法中有作用域。
要么您必须实例化应用程序的具体对象并通过 actionPerformed 处理程序中的 getKnight() 方法访问 Knight。
public class TestClass {
Knight knight;
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.run();
}
public Knight getKnight() {
return this.knight;
}
public void run() {
this.knight = new Knight();
// Do other stuff, add event handler, etc. Then you can access via getKnight() method.
}
}
或者您必须将 Knight 对象定义为主应用程序 class 的静态变量,并从 actionPerformed 处理程序静态访问它,例如
public static Knight knight;
这是不太可取的,因为它使其他 classes 更容易看到骑士,并且可以在包含它的主要 class 之外轻松更改。
根据您的代码,您可能希望将 Knight 实例传递到 Room 实例中,也许是通过其构造函数:
private Knight knight;
public Room2(final Knight knight) {
this.knight = knight;
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
public void actionPerformed(ActionEvent e) {
if(e.getSource()==attack){
knight.getHealth();
}
}
}
并在创建时传入:
Room room = new Room(knight);
例如,
public static void main(String[]args) {
Knight knight=new Knight();
Room2 room2=new Room2(knight);
}
更妙的是,如果 Room2 代表一个物理房间,而骑士代表一个真正的骑士,那么我会将我的模型建立在现实之上——骑士并不总是在一个房间内,所以我会建立我对这个现实的代码。也许给 Room2 一个 enter(...)
和 exit(...)
方法对,允许 GameManager class 调用这些方法的能力。 Room 也可以有一个 public List<Participants> listParticipants()
或类似的方法来列出所有骑士、怪物或任何碰巧在那个房间里的东西。那么房间的行为可能取决于它的居住者。
同样,您可以给所有参与者一个位置字段以及一个 setLocation(...)
方法和一个 getter 方法,这样所有参与者的行为(包括骑士的行为)都可以改变取决于他们的位置。