亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過java中的setter和getter訪問內部方法中的值

通過java中的setter和getter訪問內部方法中的值

慕婉清6462132 2022-01-12 16:35:59
這似乎是一個簡單的問題,但我受到 java 知識的限制??紤]以下代碼:public void method1(){    Object1 obj = new Object1(){       @Override       public void innerMethod(Object response){        setList(response.list);        // Displaying the result of the getter is for sample purposes        System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.       }    };    obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.    System.out.println(getList().get(0).getName()); // This returns null for the getList().}根據我對運行時執行的了解,list應該填充一次method1運行,因為innerMethod是第一個編寫的。請幫助我理解為什么第二次訪問getList()返回 null 以及如何從innerMethod.getList()另請注意,我在另一個類中使用它,它也返回 null。編輯:以下是上述場景的完整代碼。public void callMusicAPI(){   Callback<SongList> callback = new Callback<SongListResponse>(){       @Override       public void onResponse(Call<SongListResponse> call, Response<SongListResponse> response) {                   setSongListResult(response.body().getResults());                   Log.d(TAG, "Number of Songs received: " + Songs.size()); // This works.           Log.d(TAG, "Actual Response from API Call: " + response.raw() + "" + success + " " + getSongListResult().get(1).getTitle()+getSongListResult().get(1).getRelease_date() ); //This works.       }       @Override       public void onFailure(Call<SongListResponse> call, Throwable throwable) {           Log.e(TAG, throwable.toString());       }   };   call.enqueue(callback);   if(call.isExecuted()) {       if (getSongListResult() != null) {           Log.d(TAG, "Data received in setter and getter: " + getSongListResult().get(2).getTitle() + getSongListResult().get(2).getRelease_date());        } else {           Log.e(TAG, "List is super null");       }   }}我希望在call.enqueue(callback)填充列表之后,情況并非如此。請隨意注意這是否是一個 java 問題,并且 setter-getter 在大多數情況下都可以正常工作,或者這是否特定于 api 調用的響應。如果,那么建議是否需要更改問題。
查看完整描述

2 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

實際上,我更愿意寫評論,但由于我沒有足夠的聲譽來做到這一點,所以我會寫下我的意見作為答案。我希望我能有所幫助。


在我看來,范圍好像有問題。如果您刪除所有“遙不可及”的代碼,getList()它看起來像這樣


public void method1(){

    Object1 obj = new Object1(){}

    obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.

    System.out.println(getList().get(0).getName()); // This returns null for the getList().

}

似乎沒有什么getList()可以得到的,所以也許這就是你的問題的原因。


查看完整回答
反對 回復 2022-01-12
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

public void method1(){

    Object1 obj = new Object1(){

       @Override

       public void innerMethod(Object response){

        setList(response.list);

        // Displaying the result of the getter is for sample purposes

        System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.

       }

    };

    obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.

    System.out.println(obj.getList().get(0).getName()); // <-- ***********

}

我認為您從錯誤的對象調用 getList() 。如果您沒有指定要從中調用方法的對象,則假定 this.list(),在這種情況下,您將獲得未初始化的數據成員。


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號