Hello ive添加了兩個新功能來實現接口。Java實現一個接口。具有附加功能
這是實現文件...
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()) 有問題IM是,當我試圖調用這些函數從類文件它給我以下錯誤。
./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));
}
你能告訴我們你的界面'相冊'以及?!似乎這些方法在那裏丟失。 –
提供了Album類的代碼 –
@DanielDC'Album'既不指定getArtist也不指定getTrackList,只有CDAlbum可以。您可以通過選中「Album.getMediaType」來確定「Album」是「CDAlbum」還是「DVDAlbum」。那麼,如果*是* CD,則可以投射到'CDAlbum',然後調用'getArtist'和'getTrackList'。 – oldrinb