2017-06-02 31 views
0

私はC#の概念が初めてのJava開発者です。私は匿名の内部関数からクラス関数を呼び出そうとしています(Javaでは、C#で何が呼び出されているのかわかりません)。オブジェクトの内部関数からクラス関数を呼び出す方法

public void test() 
{ 
    this.apiManager.send (RequestMethod.GET, "/admin", "", callback1); 
} 


ApiCallback callback = new ApiCallback() { 
    onSuccess = (string response, long responseCode) => { 
     Debug.Log (response); 
     Debug.Log (responseCode + ""); 
     test(); 
    }, 
    onError = (string exception) => { 
     Debug.Log (exception); 
    } 
}; 

だからこれを行うには、私は次のエラーここ

A field initializer cannot reference the nonstatic field, method, or property "test()"

を取得していますあなたのコンストラクタにインスタンス化コードを移動する必要がありますApiCallback

public class ApiCallback 
{ 
    public delegate void SuccessCreater (string response, long responseCode); 

public delegate void ErrorCreater (string error); 

public SuccessCreater onSuccess { get; set; } 

public ErrorCreater onError { get; set; } 

} 
+0

異なるスコープを使用したい場合は、テストメソッドに 'static 'を割り当てる必要があります。 –

+0

'test'が宣言されている場所と' callback'をインスタンス化する場所を確認するのに役立ちます。しかし、私はそれがクリストフが正しいことを示すだろうと思う。 –

+0

テストメソッドをpublic static void test()として宣言します。 – Curious

答えて

2

の実装です:

public YourClassNameHere() 
{ 
    callback = new ApiCallback() 
    { 
     onSuccess = (string response, long responseCode) => { 
      Debug.Log(response); 
      Debug.Log(responseCode + ""); 
      test(); 
     }, 
     onError = (string exception) => { 
      Debug.Log(exception); 
     } 
    }; 
} 

代わりに(プロパティにフィールドから切り替えること)を使用します。

ApiCallback callback => new ApiCallback() 
    { 
     onSuccess = (string response, long responseCode) => { 
      Debug.Log(response); 
      Debug.Log(responseCode + ""); 
      test(); 
     }, 
     onError = (string exception) => { 
      Debug.Log(exception); 
     } 
    }; 

は詳細についてはA field initializer cannot reference the nonstatic field, method, or propertyを参照してください。

関連する問題