我在这个类中有一个名为“Artist”的类,我可以调用“Track”类的公共(public)方法 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());
}
我被困在这里了。 Song.setArtist(tracks.get(i).getArtists().getClass().getMethod("getName", ArtistSimplified.class).getReturnType());
我尝试了上面的行,但它不起作用。
最佳答案
您的代码正在尝试对数组本身调用getName()
。您需要告知索引,您将从哪个Artist
获取名称。
for (int a = 0; a < tracks.get(i).getArtists().length; a++) {
System.out.print(tracks.get(i).getArtists()[a].getName());
}
关于java - 如何通过从第一个类调用第二个类的方法来调用第三个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531325/