从未扩展JavaPlugin的类中引用"this" [英] Referencing 'this' from a class that is not extending JavaPlugin

查看:83
本文介绍了从未扩展JavaPlugin的类中引用"this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个使用以下方法启动Scheduler任务的插件:

I am trying to make a plugin that starts a Scheduler task with this method:

public void newCountdown() {
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        public void run() {
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                player.sendMessage("Hey");
            }
        }                   
}, 0, 20);
}

问题是,当我尝试调用该方法时,它说它必须是静态方法.然后,当我将其更改为静态时,第一个参数"this"表示它不能在静态上下文中使用.

The problem is, when I try to call the method, it says it needs to be a static method. Then, when i change it to static the first parameter "this" says it cannot be used in a static context.

如果方法不是静态的,则scheduleSyncRepeatingTask会显示此错误:

When the method is not static, the scheduleSyncRepeatingTask shows this error:

BukkitScheduler类型的scheduleSyncRepeatingTask(Plugin,Runnable,long,long)方法不适用于参数(activateDevMode,new Runnable(){},int,int)

The method scheduleSyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (activateDevMode, new Runnable(){}, int, int)

当我尝试它提供的任何快速修复程序时,总是会导致另一个错误.

When I try any of the quick fixes it gives me, it will always result in another error.

是否有一种方法可以从Main类中引用this而不必将我的方法设为静态?

Is there a way to reference this from the Main class without having to make my method static?

推荐答案

之所以不起作用,是因为 this 永远不要在一起.一种简单的思考方法是static删除Java的面向对象部分. this是指向类的当前实例的关键字,不能与static一起使用,因为使用static变量就像完全删除实例一样.

The reason this isn't working is because static and this never go together. An easy way to think about it is that static removes the object-oriented part of Java. this is a keyword that points to the current instance of your class, and cannot be used with static because using a static variable is like completely removing instances all together.

您将必须将this更改为Main类的实例(即extends JavaPlugin的实例).您可以初始化静态变量onEnable()来存储实例

You will have to change this to an instance of your Main class (the one that extends JavaPlugin). You can initialize a static variable onEnable() to store the instance

public static Main that; //"Main" will be replaced with the name of your Main class

@Override
public void onEnable(){
    //set that to an instance of your Main class (this)
    that = this;
}

@Override
public void onDisable(){
    //set that to null to prevent memory leaks
    that = null;
}

现在,您可以通过将that替换为this

Now, you can make your newCountdown() method static by replacing that with this

public static void newCountdown() {
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.that, new Runnable() {
        public void run() {
            for(Player player : Bukkit.getServer().getOnlinePlayers()){
                player.sendMessage("Hey");
            }
        }                   
    }, 0, 20);
}

这篇关于从未扩展JavaPlugin的类中引用"this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆