簡體   English   中英

類型中的方法不適用於參數

[英]Method in the type is not applicable to the arguments

我在這里查看過很多帖子,看不出我需要的解決方案......

我收到錯誤:

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

以上行是拋出錯誤的行,以下是函數:

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

在此先感謝任何可以幫助的人:)

附加信息:

runState是一個布爾值,只是因為你無法猜測而且character是Toon類的一個實例; 上面的方法在TimerClass類中,'timers'是該類的實例。

錯誤消息

該方法initTimer(untitled.Object, String, int int)在類型untitled.TimerClass不適用於參數(untitled.Toon, String, int, int)

是因為untitled.Toon沒有擴展untitled.Object 它當然擴展了java.lang.Object ,這就是為什么原因不是從源代碼中立即顯而易見的。

另外,另一個錯誤是initTimer(untitled.Object,String,int)被調用為(untitled.Toon,String,int,int) - 注意參數個數的差異 - 方法聲明中的1個int和調用時的2個int方法。

請記住也要糾正。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM