类型中的方法不适用于参数 [英] Method in the type is not applicable to the arguments

查看:52
本文介绍了类型中的方法不适用于参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了此处的许多帖子,但不太清楚我需要的解决方案...

i've looked through many posts on here and couldn't quite see the solution I need...

我收到错误:

the method initTimer(untitled.Object, String, int int) in the type untitled.TimerClass is not applicable for the arguments (untitled.Toon, String, int, int)

这让我发疯.

    timers.initTimer(character, "regenAdd", 0,3);

上面一行是抛出错误的那一行,下面是函数:

The above line is the one throwing the error and the following is the function:

public void initTimer(final Object obj, final String method, int delay, int period) {
delay*=1000;
period*=1000;

final Class<?> unknown = obj.getClass();

new Timer().schedule(new TimerTask() {
  public void run() {
    try {
      //get the method from the class 
      Method whatToDo = unknown.getMethod(method, null);
      try {
        //invoke() the object method
        whatToDo.invoke(obj);
      } catch(Exception e) {
        println("Exception encountered: " + e);
      }
    } catch(NoSuchMethodException e) {
      println("Exception encountered: " + e);
    }
    runState = getTimerState();

    if (!runState) {
      println("timer dead");
      this.cancel();
    }
  }
}
, delay, period);
}

预先感谢任何可以帮助解决此问题的人:)

Thanks in advance to anybody who can help with this :)

附加信息:

runState 是一个布尔值,以防万一你猜不出来character 是 Toon 类的一个实例;上述方法在TimerClass 类和 'timers' 是该类的一个实例.

runState is a boolean just incase you couldn't guess and character is an instance of the Toon class; the above method is within the TimerClass class and 'timers' is an instance of that class.

推荐答案

错误信息

untitled.TimerClass 类型中的方法 initTimer(untitled.Object, String, int int) 不是适用于参数 (untitled.Toon, String, int, int)

the method initTimer(untitled.Object, String, int int) in the type untitled.TimerClass is not applicable for the arguments (untitled.Toon, String, int, int)

是因为 untitled.Toon 没有扩展 untitled.Object.当然,它扩展了java.lang.Object,这就是为什么从源代码中看不出原因.

is due to the fact that untitled.Toon does not extend untitled.Object. It extends java.lang.Object of course, which is why the reason is not immediately obvious from the source code.

这篇关于类型中的方法不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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