[英]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.