简体   繁体   English

无法从内部行动到达现场

[英]Can't reach field from inside action

public class GameMenu extends JPanel{

    private final Core core;
    private final JButton loadGame;
    private final JButton saveGame;
    private final JButton exit;
    private final JButton newGame;

    public GameMenu(Core core){
        setPreferredSize(new Dimension(600, 600));
        setBackground(Color.BLACK);
        this.core = core;

        newGame = new JButton(newGameAction);
        loadGame = new JButton(loadGameAction);
        saveGame = new JButton(saveGameAction);
        exit = new JButton(exitGameAction);


        add(newGame);
        add(loadGame);
        add(saveGame);
        add(exit);
    }


  private Action newGameAction = new AbstractAction("New Game") {
        @Override
        public void actionPerformed(ActionEvent e) {          
            this.core.
        }
    };

HI, i want to call a method of core instead of call a method of this class which would call the proper method of core The problem is that i dont know how to reach the core field cos its cannot find thx 嗨,我想调用一个核心的方法,而不是调用这个类的方法,这将调用正确的核心方法问题是,我不知道如何达到核心领域cos它无法找到thx

In this context: 在这方面:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        this.core. // compiling error here: 'core' is not a member of the anonymous inner class
    }
};

The keyword this refers to the anonymous inner class generated when you create a new Action instance (the current object ). 关键字this指的是在创建新的Action实例( 当前对象 )时生成的匿名内部类。 This fact is explained here: Using the this Keyword : 这里解释了这个事实: 使用这个关键字

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. 在实例方法或构造函数中, this是对当前对象的引用 - 正在调用其方法或构造函数的对象。 You can refer to any member of the current object from within an instance method or a constructor by using this . 您可以使用this方法从实例方法或构造函数中引用当前对象的任何成员。

Since the anonymous inner class has no member called core then you get a compiling error. 由于匿名内部类没有名为core成员,因此您会收到编译错误。 In order to solve this problem take a look to this topic: Keyword for the outer class from an anonymous inner class? 为了解决这个问题,请看一下这个主题: 来自匿名内部类的外部类的关键字? .

For practical purposes you should replace the line above for this one to refer to your GameMenu class: 出于实用目的,您应该替换上面的行来引用您的GameMenu类:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        GameMenu.this.core. 
    }
};

Or you can remove this keyword and compiler will resolve the same for you: 或者您可以删除this关键字,编译器将为您解决相同的问题:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        core. // should be recognised as outer class member
    }
};

Given the anonymous inner class has no core member, the compiler will look up for the outer class (or eventually the hierarchy tree, with the appropriate visibility) and chek if it has such memeber, which is indeed the case. 鉴于匿名内部类没有core成员,编译器将查找外部类(或最终查找具有适当可见性的层次结构树)并且如果它具有这样的内容,那就是chek,这确实是这种情况。

You are in an inner class. 你是一个内在阶级。 Use: 采用:

GameMenu.this.core...

Otherwise, this refers to the anonymous inner class you are in. 否则, this指的是您所在的匿名内部类。

With the declaration 随着声明

private Action newGameAction = new AbstractAction("New Game") { ... }

you create an instance of an anonymous subclass of AbstractAction . 您创建AbstractAction匿名子类的实例。 The keyword this then refers to this instance and not to the outer instance of type GameMenu . 然后,关键字this引用此实例,而不引用GameMenu类型的外部实例。 So there is no core field available. 所以没有可用的core领域。

The syntax for accessing the core field of the outer GameMenu instance is: 访问外部GameMenu实例的core字段的语法是:

GameMenu.this.core

But - as you are still inside the GameMenu class - you can access private members at this point, too. 但是 - 当你还在GameMenu课程中时 - 你也可以访问私人成员。 So you simply can write: 所以你只需写:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        doSomethingWith(core);
    }
};

Remove this from your statement as you are defining an anonymous class where this would mean instance of your anonymous class itself and in your anonymous class you don't have field called core. 当您定义一个匿名类时,从您的语句中删除它,这将表示您的匿名类本身的实例,而在您的匿名类中,您没有名为core的字段。 so use it like: 所以使用它像:

private Action newGameAction = new AbstractAction("New Game") {
    @Override
    public void actionPerformed(ActionEvent e) {          
        //core.
    }
};

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

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