저는 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; }
}
인스턴스화 코드를 생성자로 이동해야합니다.
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);
}
};
자세한 내용 은 필드 이니셜 라이저가 비 정적 필드, 메서드 또는 속성 을 참조 할 수 없음을 참조 하세요.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다