简体   繁体   English

访问在重写的方法java中声明的方法

[英]Accessing a declared method inside an overridden method java

I am having a bit of trouble with accessing an object declared in the main from a different class:我在访问来自不同类的 main 中声明的对象时遇到了一些麻烦:

public static void main(String[]args)
{ 
  Knight knight=new Knight();
  Room2 room2=new Room2()
}

The problem is that I can't pass the object as an argument in this particular method as its an actionListener overridden method so something like this Accessing objects of other classes wouldn't work.问题是我不能在这个特定方法中将对象作为参数传递为它的actionListener覆盖方法,所以像这样访问其他类的对象将不起作用。

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==attack)
    {
        knight.getHealth();
    }
}

I can't pass anything extra after the ActionEvent e argument so I wanted to ask is this possible for the knight object to be recognized in this class or do I have to do something completely different?我不能在ActionEvent e参数之后传递任何额外的东西,所以我想问一下这是否有可能在这个类中识别knight对象,或者我必须做一些完全不同的事情吗? thanks谢谢

Edit: The main method is simply creating character objects like knight which is a concrete class:编辑:主要方法只是创建像knight这样的角色对象,这是一个具体的类:

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();    
        }
    }
}

This line...这条线...

Knight knight=new Knight();

... only has scope within the main method. ...仅在主要方法内具有范围。

Either you are going to have to instantiate a concrete object of your application and access the Knight via a getKnight() method in the actionPerformed handler.要么您必须实例化应用程序的具体对象并通过 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.
    }
}

Or you're going to have to define your Knight object as a static variable of the main application's class and access it statically from the actionPerformed handler eg或者,您将不得不将 Knight 对象定义为主应用程序类的静态变量,并从 actionPerformed 处理程序静态访问它,例如

public static Knight knight;

This is less preferable, as it makes the Knight more visible to other classes and can easily be altered outside of the main class that contains it.这不太可取,因为它使骑士对其他类更可见,并且可以在包含它的主类之外轻松更改。

Based on your code, you will want to pass your Knight instance into the Room instance, perhaps via its constructor:根据您的代码,您可能希望通过其构造函数将 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();
        }
    }
}

And when creating it, pass it in:并在创建时传入:

Room room = new Room(knight);

eg,例如,

public static void main(String[]args) { 
  Knight knight=new Knight();
  Room2 room2=new Room2(knight);
}

Better still, if the Room2 represents a physical room, and the knight an actual knight, then I would base my model on reality -- the knight isn't always inside of a room, and so I'd base my code on this reality.更好的是,如果 Room2 代表一个物理房间,而骑士是一个真正的骑士,那么我会根据现实建立我的模型——骑士并不总是在房间里,所以我的代码基于这个现实. Perhaps give Room2 an enter(...) and exit(...) method pair, allowing the the GameManager class the ability to call these methods.也许给 Room2 一个enter(...)exit(...)方法对,允许 GameManager 类调用这些方法的能力。 The Room could also have a public List<Participants> listParticipants() or something similar type method that would list all knights, monsters or whatever that happen to be in that room.房间也可以有一个public List<Participants> listParticipants()或类似类型的方法,可以列出所有骑士、怪物或发生在那个房间里的任何东西。 Then room behaviors could depend on its occupants.然后房间的行为可能取决于它的居住者。

Likewise you could give all your participants a location field as well as a setLocation(...) method with a getter method as well, so that all participant behaviors (including that of the knight) can change depending on their location.同样,您可以为所有参与者提供一个位置字段以及带有 getter 方法的setLocation(...)方法,以便所有参与者的行为(包括骑士的行为)可以根据他们的位置而改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM