java如何通过从第一个类调用第二个类的方法来调用第三个类的方法
我从这个类中有一个名为“Artist”的类,我可以调用类“Track”的公共方法getArtists,它在返回值中为我提供了另一个类“ArtistSimplified”的数组。我对getName方法感兴趣,它返回ArtistSimplified类的字符串。我想知道怎么做。我不能直接调用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());
}
我被困在这里了。 歌曲setArtist(tracks.get(i))。getArtists()。getClass()。getMethod(“getName”,ArtistSimplified.class)。getReturnType())
我试过上面这句话,但不起作用
# 1 楼答案
您的代码试图调用数组本身的
getName()
。你需要通知一个Artist
你将从中获取名字的索引