gpt4 book ai didi

Java-如何调用具有返回类型的方法

转载 作者:行者123 更新时间:2023-12-02 07:51:38 24 4
gpt4 key购买 nike

我试图从另一个类调用返回类型方法,但我不断收到该类类型未定义的错误。这是我尝试调用的代码:

   public MyTask() {

id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if(id == -1)
{
plugin.getLogger().warning("BLARG");
}
}

我如何从另一个类调用该方法?

 public class DamageListener implements Listener{
private antirelog plugin;
public DamageListener (antirelog plugin) {
this.plugin = plugin;
}
Player realplayer1;
Player realplayer2;
boolean playeradd = false;
private MyTask task;
public static boolean isDamaged = false;
public static boolean timerTask = true;
static Set<Player> Damagelist = Collections.newSetFromMap(new WeakHashMap<Player,Boolean>());

@EventHandler
public void damage (EntityDamageEvent event) {
Entity victim = event.getEntity();
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent edbeEvent = (EntityDamageByEntityEvent) event;
if(edbeEvent.getDamager() instanceof Player && victim instanceof Player) {
EntityDamageByEntityEvent edbeEvent1 = (EntityDamageByEntityEvent) event;
Entity attacker = edbeEvent1.getDamager();
Player player = (Player) victim;
realplayer1 = player;
Player player2 = (Player) attacker;
realplayer2 = player2;
if(Damagelist.contains(realplayer1) || Damagelist.contains(realplayer2)) {
isDamaged = true;
timerTask = false;
System.out.println("Cancel");
MyTask();
} else {
Damagelist.add(realplayer1);
Damagelist.add(realplayer2);
isDamaged = true;
Pause ps = new Pause(plugin);
MyTask();
}

}

}

这是 MyTask 类。

public class MyTask implements Runnable
{

private antirelog plugin;
private int id = -1;

/**
* Generic constructor
* @param Plugin task is associated with
* @return
*/
public MyTask (antirelog plugin) {
this.plugin = plugin;
}
public void MyTask() {

id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if(id == -1)
{
plugin.getLogger().warning("BLARG");
}
}


/**
* Do stuff when scheduler tells task to run
*/
@Override
public void run()
{
if (DamageListener.timerTask == true) {
DamageListener dl = new DamageListener(plugin);
dl.Then();
}
else if (DamageListener.timerTask == false) {
plugin.getServer().getScheduler().cancelTask(id);
DamageListener.timerTask = true;
}
}

/**
* Remove task from scheduler
* @return True if successfully stopped. Else false.
*/
public boolean stopTask()
{
if(id != -1)
{
plugin.getServer().getScheduler().cancelTask(id);
return true;
}
return false;
}
}

最佳答案

由于MyTask()是一个构造函数,对于类MyTask来说,要在另一个类中实例化,它必须以new为前缀> 关键字,如下所示:

...
isDamaged = true;
Pause ps = new Pause(plugin);
task = new MyTask(plugin); //Note the `new` keyword
...

我们还可以修复一些其他问题。

public MyTask (antirelog plugin)
{
this.plugin = plugin;
if (plugin != null) {
id = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,this,plugin.getConfig().getInt("StunDuration") * 20);
if (id == -1)
{
plugin.getLogger().warning("BLARG");
}
}
}

//public MyTask() //Remove the void, this is a constructor
//{
// this(null); //Call the extended constructor
//}

关于Java-如何调用具有返回类型的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com