简体   繁体   中英

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'. I am in interested in the method getName which returns a string of ArtistSimplified class. I want to know how to do it. I can not directly call the getName method of ArtistSimplified class.

        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());

I tried the above line but it does not work.

Your code is trying to call getName() on the array itself. You need to inform an index to which Artist you're going to get the name from.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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