繁体   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

我有一个类命名'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());
        }

我被困在这里。 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()); 
} 

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM