简体   繁体   English

如何通过从第1类调用第2类的方法来调用第3类的方法

[英]How to call a method of a 3rd class by calling the method of the 2nd class from the 1st class

I have one class naming 'Artist' from this class I can call the public method getArtists of class 'Track' which in the return value gives me an array of another class 'ArtistSimplified'. 我有一个类命名'Artist'来自这个类我可以调用类'Track'的公共方法getArtists,它在返回值中给我一个另一个类'ArtistSimplified'的数组。 I am in interested in the method getName which returns a string of ArtistSimplified class. 我对方法getName感兴趣,它返回一个ArtistSimplified类的字符串。 I want to know how to do it. 我想知道怎么做。 I can not directly call the getName method of ArtistSimplified class. 我无法直接调用ArtistSimplified类的getName方法。

        List<Track> tracks = new ArrayList<>();
        tracks = Arrays.asList(topTracksRequest.execute());
        if(tracks == null) {
            throw new ResourceNotFoundException(new ErrorMessage("No songs found for this id:" +artistId));
        }

        List<Song> songs = new ArrayList<Song>();
        for(int i = 0; i < 5; i++) {
            Song song = new Song();
            song.setTitle(tracks.get(i).getName());
            song.setArtist(tracks.get(i).getArtists().getClass().getMethod("getName", ArtistSimplified.class).getReturnType());
        }

I am stuck here. 我被困在这里。 song.setArtist(tracks.get(i).getArtists().getClass().getMethod("getName", ArtistSimplified.class).getReturnType()); song.setArtist(tracks.get(i).getArtists()。getClass()。getMethod(“getName”,ArtistSimplified.class)。getReturnType());

I tried the above line but it does not work. 我试过上面这行,但它不起作用。

Your code is trying to call getName() on the array itself. 您的代码试图在数组本身上调用getName() You need to inform an index to which Artist you're going to get the name from. 您需要通知索引您将从哪个Artist获取名称。

for (int a = 0; a < tracks.get(i).getArtists().length; a++) { 
        System.out.print(tracks.get(i).getArtists()[a].getName()); 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 class 中将方法从第一种方法调用到第二种方法 - How to call method from 1st method to 2nd method in a class 尝试从第一类活动中调用方法,并在第二类活动中保留相同的参数(android studio) - Trying to call a method from 1st class activity and keep the same argument in a 2nd class activity (android studio) 如何将代码从第二类添加到第一类 - How to add code from 2nd class to 1st class JComboBox仅在从第1到第2时才更改组件(而不是从第3到第2时) - JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd) Dagger NullPointerException第三类,具体取决于第二类 - Dagger NullPointerException 3rd class depending on 2nd class 当第一个 API 和第二个 API 在测试环境中工作时,如何模拟第三个 API 响应 - How to mock 3rd API response when 1st API and 2nd API is working on test environment 如何解决获取#2nd记录而不是#1st记录的光标(#3rd而不是#2nd等)? - How to fix cursor that is taking #2nd record instead of #1st (#3rd instead of #2nd etc.)? 以这样的方式更新 HashMap,使第二个值变成第一个,第三个变成第二个,依此类推 - Updating a HashMap in such a way that the 2nd value becomes 1st and 3rd becomes 2nd and so on 获取空对象引用如果我在第三项活动之前开始第二项活动(如果我直接从第一项移到第三项,则没有错误) - Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly) Java 8 LocalDate:确定每月的星期几(第1,第2,第3等) - Java 8 LocalDate: Determine What Monday of the Month its (1st, 2nd, 3rd, etc)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM