我确信这是一个愚蠢的事情,但我不能为我的生活弄清楚....在主要的方法中,当我试图创造新的艺术家时,我不断收到错误创造一个新的"录音"线(即:表面和弹出)。它说它需要String,String []但是得到了String,MusicCollection.Artist。它说"实际参数MusicCollection.Artist不能通过方法调用转换转换为String []。
public class MusicCollection {
private Artist[] artists = new Artist[100];
private Recording[] recordings = new Recording[200];
private int artistCount = 0;
private int recordingCount = 0;
//toString method for MusicCollection
public String toString() {
StringBuffer sb = new StringBuffer();
if (recordingCount > 0) {
sb.append(recordings[0].toString());
for (int i = 1; i < recordingCount; i++) {
sb.append("\n" + recordings[i]);
}
}
return sb.toString();
}
public class Artist {
private String name;
/**
* Construct an artist object and add it to the collection.
*
* @param name the name of the Artist
*/
public Artist(String name) {
this.name = name;
artists[artistCount++] = this;
}
/**
* Retrieve the artist as a string
*
* @return the string representation of the artist
*/
public String toString() {
return name;
}
}
public class Recording {
private String name;
private Artist[] artists = new Artist[100];
private Track[] tracks = new Track[200];
private int trackCount = 0;
public class Track {
private String name;
/**
* Construct track object and add it to the collection.
*
* @param name the name of the track
*/
public Track(String name) {
this.name = name;
tracks[trackCount++] = this;
}
/**
* Retrieve the track as a string
*
* @return the string representation of the track
*/
public String toString() {
return name;
}
}
public Recording(String name, String Artist[]) {
this.name = name;
this.artists = artists;
recordings[recordingCount++] = this;
}
public String toString() {
StringBuffer sb = new StringBuffer(name);
sb.append(" by " + artists + ": ");
if (trackCount > 0) {
sb.append(tracks[0].toString());
for (int i = 1; i < trackCount; i++) {
sb.append(", " + tracks[i]);
}
}
return sb.toString();
}
}
public static void main(String[] args) {
MusicCollection mc = new MusicCollection();
Artist sarahM = mc.new Artist("Sarah McLachlan");
Recording surfacing = mc.new Recording("Surfacing", sarahM);
Recording.Track surfacing1 = surfacing.new Track("Building a Mystery");
Recording.Track surfacing4 = surfacing.new Track("Adia");
Artist u2 = mc.new Artist("U2");
Recording pop = mc.new Recording("Pop", u2);
Recording.Track pop1 = pop.new Track("Discotheque");
Recording.Track pop5 = pop.new Track("Miami");
System.out.println(mc);
}
}
答案 0 :(得分:0)
需要:
public Recording(String name, Artist someArtist)
在我的录音课上,只有:
private Artist artist;
因为我已经将Artist声明为数组。
我还必须将this.artists = artists;
更改为this.artists = someArtist;
,因为那是我传递的变量。之后像魅力一样工作!