0

您好,我在接口的实现中添加了两个新功能。

这是实现文件...

import au.edu.uow.Collection.Album;
import java.util.ArrayList;

public class CDAlbum implements Album {
    private String Title;
    private String Genre;
    private String Artist;
    private String MediaType;
    private ArrayList<String> trackList;

    public CDAlbum(String TempTitle, String TempGenre, String TempArtist, ArrayList<String> TempTracklist, String TempMediaType){
        //Set initail variable values
        Title = TempTitle;
        Genre = TempGenre;
        Artist = TempArtist;
        trackList = TempTracklist;
        MediaType = TempMediaType;
    }
    //Accessor Functions
    public String getMediaType(){
        //Return Media Type
        return MediaType;
    }
    public String getTitle(){
        //Return Title
        return Title;
    }
    public String getGenre(){
        //Return Genre
        return Genre;
    }
    public String getArtist(){
        //Return Artist
        return Artist;
    }
    public ArrayList<String> getTrackList(){
        //Return Tracklist
        return trackList;
    }
}

底部的两个函数是添加的函数(getArtist(),getTrackList())我遇到的问题是,当我尝试从类文件中调用这些函数时,它会给我以下错误。

./au/edu/uow/UserInterface/UserInterface.java:95: cannot find symbol
symbol  : method getArtist()
location: interface au.edu.uow.Collection.Album
            System.out.println(albumCollection.get(number).getArtist());
                                                          ^
./au/edu/uow/UserInterface/UserInterface.java:98: cannot find symbol
symbol  : method getTrackList()
location: interface au.edu.uow.Collection.Album
            ArrayList<String> trackList = albumCollection.get(number).getTrackList();

当我调用函数时

import au.edu.uow.Collection.Album;

System.out.println(albumCollection.get(number).getArtist());

//Access the track titles
ArrayList<String> trackList = albumCollection.get(number).getTrackList();
//Output collection
int arrayListSize = trackList.size();
for(int i = 0; i < arrayListSize; i++)
{
    System.out.println( i + ": " + trackList.get(i));
}
4

4 回答 4

0

Album 是 AlbumCollection.get(number) 的返回类型吗?如果是这样,那么您还需要将 getArtist 和 getTrackList 方法添加到您的界面中。

于 2012-08-26T06:01:53.557 回答
0

Album既不指定getArtist也不指定getTrackList

public interface Album {
    /**
     * This method returns the media type of the album. 
     * @return the media type of the album, either CD or DVD
     * @see #getTitle()
     * @see #getGenre()
     */
     String getMediaType();

    /**
     * This method returns the title of the album. 
     * @return the title of the album
     * @see #getMediaType()
     * @see #getGenre()
     */
     String getTitle();

    /**
     * This method returns the genre of the album. 
     * @return the genre of the album
     * @see #getTitle()
     * @see #getMediaType()
     */
    String getGenre();

}

......只有CDAlbum

您可以通过检查来确定Album是 aCDAlbum还是 然后,如果它一张 CD,您可以投射到并调用然后。DVDAlbumAlbum.getMediaTypeCDAlbumgetArtistgetTrackList

for (final Album album : albumCollection) {
  final String type = album.getMediaType();
  System.out.print(type + " album: " + album.getTitle()
      + " (" + album.getGenre() + ") - ");
  if (type.equals("CD")) {
    final CDAlbum cd = (CDAlbum) album;
    System.out.println(cd.getArtist());
    int n = 0;
    for (final String track : cd.getTrackList()) {
      System.out.printf("#%2d - %s\n", ++n, track);
    }
  } else {
    final DVDAlbum dvd = (DVDAlbum) album;
    System.out.println(dvd.getDirector());
    System.out.println(dvd.getPlotOutline());
  }
}
于 2012-08-26T06:18:07.257 回答
0

例如,创建一个超类并从中MasterAlbums扩展您的和。将方法&放在超类中。不要在子类中覆盖它。现在当您调用时,它将引用超类方法,您的艺术家和曲目列表将打印。CDAlbumDVDAlbumgetArtist()getTrackList()System.out.println(albumCollection.get(number).getArtist());

于 2012-08-26T06:28:11.547 回答
0

在接口中添加新方法时,必须在实现类中实现。

检查albumCollection.get(number) 的类型如果albumCollection.get(number) 返回Album 然后你可以调用getArtist()

于 2012-08-26T05:58:39.823 回答