有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在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().
}

根据我对运行时执行的理解,一旦运行了method1,就应该填充list,因为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");
       }
   }

}

public void setSongListResult(List<Song> songs){
    this.songs = songs;
}

public List<Song> getSongListResult(){
    return this.songs;
}

我希望在call.enqueue(callback)之后,列表将被填充,然而,情况并非如此

请注意这是否是一个java问题,setter-getter在大多数情况下都可以正常工作,或者这是否特定于api调用的响应。如果需要,请建议是否需要更改问题


共 (2) 个答案

  1. # 1 楼答案

    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()。如果不指定要从中调用该方法的对象,则假定是这样。list(),在这种情况下,您将获得未初始化的数据成员

  2. # 2 楼答案

    实际上,我更愿意写一篇评论,但因为我没有足够的声誉来做这件事,我会写我的意见作为回答。我希望我能帮上忙

    在我看来,范围似乎有问题。如果删除第二个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()什么也得不到,所以也许这就是你问题的原因