新手在这里做Java作业。我有一个名为Album的类,它包含以下构造函数:
public class Album {
private String title;
private String artist;
private String genre;
private Song favoriteTrack;
private int trackNumber;
private static int numAlbums;
//Constructors
public Album(String title, Song favoriteTrack, int trackNumber) {
this.title = title;
this.favoriteTrack = favoriteTrack;
this.trackNumber = trackNumber;
artist = favoriteTrack.getArtist();
genre = favoriteTrack.getGenre();
numAlbums++;
}
public Album(String title, Song favoriteTrack) {
this(title, favoriteTrack, 1);
}
...}
然后我有一个第二类MusicCollection,在其主方法中实例化Album类三次......
public static void main (String[] args) {...
Album album1 = new Album("Debut", "Venus as a Boy", 3);
Album album2 = new Album("Homework", "Around the World", 7);
Album album3 = new Album("Ghost in the Machine", "Invisible Sun", 3);
...}
但是,当我尝试编译MusicCollection.java时,我收到错误:
cannot find symbol
symbol : constructor Album(java.lang.String,java.lang.String,int)
location : class Album
每次我尝试调用构造函数。 Album和MusicCollection类位于同一目录中,Album.java编译。 我想我正在做些傻事,但我无法弄清楚这一点。 任何帮助将不胜感激!
答案 0 :(得分:6)
您定义的构造函数的第二个参数是Song
,而不是String
,但在您的main中,您尝试使用String
作为第二个参数进行实例化。
答案 1 :(得分:1)
当您声明它应该收到String
的实例时,您正在将Album
传递给Song
构造函数的第二个参数。
答案 2 :(得分:1)
Binyamin说的是什么!如果你有一个歌曲课,你需要说new Album("Debut", new Song("venus as a boy"), 3);
之类的内容,具体取决于你的Song课程的工作方式,如果没有,请将歌曲改为字符串类型
答案 3 :(得分:0)
您正尝试使用2个字符串而不是字符串和某个类Song
来实例化相册,也许您应该将构造函数更改为
public Album(String title, String favoriteTrack, int trackNumber) {
this.title = title;
this.favoriteTrack = favoriteTrack;
this.trackNumber = trackNumber;
artist = favoriteTrack.getArtist();
genre = favoriteTrack.getGenre();
numAlbums++;
}
或者这样做:
new Album("Debut", new Song("Venus as a boy"), 3);
或者歌曲已创建。