Java-如何使用返回类型调用方法

时间:2012-04-14 23:45:21

标签: java

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

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

1 个答案:

答案 0 :(得分:2)

由于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
//}