具有此功能的Timer中的类的无法访问变量

问题描述 投票:0回答:1

我想在方法中更改对象的变量y的值,但this.y引用计时器而不是对象

private void move(byte direction) {
        Timer projectilemovement = new Timer();
        projectilemovement.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                switch (direction){
                    case 1:
                        ***this.y -= speed;***
                        break;
                }
            }
        }, 0, 16);
    }
java timer this
1个回答
0
投票
据我所见,您无需使用this关键字就可以完成操作,因为y是一个没有被方法参数或局部变量覆盖的字段。也许我缺少更多信息?
© www.soinside.com 2019 - 2024. All rights reserved.