猿问

通过java中的setter和getter访问内部方法中的值

这似乎是一个简单的问题,但我受到 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 调用的响应。如果,那么建议是否需要更改问题。


慕婉清6462132
浏览 163回答 2
2回答

茅侃侃

实际上,我更愿意写评论,但由于我没有足够的声誉来做到这一点,所以我会写下我的意见作为答案。我希望我能有所帮助。在我看来,范围好像有问题。如果您删除所有“遥不可及”的代码,getList()它看起来像这样public void method1(){&nbsp; &nbsp; Object1 obj = new Object1(){}&nbsp; &nbsp; obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.&nbsp; &nbsp; System.out.println(getList().get(0).getName()); // This returns null for the getList().}似乎没有什么getList()可以得到的,所以也许这就是你的问题的原因。

海绵宝宝撒

public void method1(){&nbsp; &nbsp; Object1 obj = new Object1(){&nbsp; &nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp;public void innerMethod(Object response){&nbsp; &nbsp; &nbsp; &nbsp; setList(response.list);&nbsp; &nbsp; &nbsp; &nbsp; // Displaying the result of the getter is for sample purposes&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; };&nbsp; &nbsp; obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.&nbsp; &nbsp; System.out.println(obj.getList().get(0).getName()); // <-- ***********}我认为您从错误的对象调用 getList() 。如果您没有指定要从中调用方法的对象,则假定 this.list(),在这种情况下,您将获得未初始化的数据成员。
随时随地看视频慕课网APP

相关分类

Java
我要回答