Java 在没有字符串的情况下调用超类构造函数?

Java 在没有字符串的情况下调用超类构造函数?,java,superclass,Java,Superclass,我目前正试图用super()调用超类相册的构造函数但它不起作用。这个问题似乎源于这样一个事实:我试图用一个类型为Artist的对象而不是字符串来调用它,但即使我使用字符串,它仍然不起作用。有人能指出我哪里出了问题吗?是否有更好的方法打电话: public class StudioAlbum extends Album { private Artist artist; public StudioAlbum(String name, Artist artist) {

我目前正试图用
super()调用超类相册的构造函数但它不起作用。这个问题似乎源于这样一个事实:我试图用一个类型为Artist的对象而不是字符串来调用它,但即使我使用字符串,它仍然不起作用。有人能指出我哪里出了问题吗?是否有更好的方法打电话:

public class StudioAlbum extends Album {

    private Artist artist;

    public StudioAlbum(String name, Artist artist) {
        super(artist);
        this.name = name;
        this.artist = artist;
        albumType = "Studio";
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public String getArtist() {
        return artist;
    }
}
如果这是有用的,则Album类如下所示(没有方法):

公共类相册{
受保护的字符串名称;
私有ArrayList跟踪列表;
私有整数长度;
私有int文件大小;
私人双重平均;
受保护的字符串类型;
公共相册(字符串名称){
this.name=名称;
trackList=newarraylist();
}
}

相册
没有无参数构造函数。请看这里的解释:

看起来您应该调用
super(name)
。超类构造函数使用
字符串
作为唱片集的名称,子类也使用
艺术家

但即使我使用字符串,它仍然不起作用


应该这样。如果没有,那么你就有一些糟糕的语法或其他东西。

你把字符串和艺术家搞混了。以下是更正后的代码:

public class StudioAlbum extends Album {

    private Artist artist;

    public StudioAlbum(String name, Artist artist) {
        super(name);
        this.name = name;
        this.artist = artist;
        albumType = "Studio";
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public Artist getArtist() {
        return artist;
    }
}

public class Album {

    protected String name;
    private ArrayList<Track> trackList;
    private int length; 
    private int fileSize;
    private double averageRating;
    protected String albumType;

    public Album(String name){
        this.name = name;
        trackList = new ArrayList<Track>();
    }
}
公共类StudioAlbum扩展了Album{
私人艺术家;
public StudioAlbum(字符串名称、艺术家){
超级(姓名);
this.name=名称;
这个艺术家=艺术家;
albumType=“Studio”;
}
公共艺术家(艺术家){
这个艺术家=艺术家;
}
公共艺术家{
回归艺术家;
}
}
公开课相册{
受保护的字符串名称;
私有ArrayList跟踪列表;
私有整数长度;
私有int文件大小;
私人双重平均;
受保护的字符串类型;
公共相册(字符串名称){
this.name=名称;
trackList=newarraylist();
}
}
还有艺术家和轨道课程需要


超类Album在其构造函数中需要一个字符串。StudioAlbum接受字符串(名称)和艺术家(艺术家)。让这项工作重新工作(这可能是合适的)的唯一方法是使用名称(字符串)调用super()。如果您想要超类来获取艺术家,您需要更改类相册以使用类艺术家而不是字符串来存储名称

因此,正如前面所说,
Album
没有无参数构造函数,因此您不能使用
super()
此外,您的
StudioAlbum
构造函数的第二行(
this.name=name
)是无用的,因为它已经在
相册的构造函数中完成了

以下是一种更常见的方法来实现您的愿望:

public Album(String name, String albumType) {
   this.name = name;
   this.albumType = albumType;
   this.trackList = new ArrayList<>(); // diamond operator from java7
}

public StudioAlbum(String name, Artist artist){
   super(name, "Studio");
   this.artist = artist ;
}
公共相册(字符串名称、字符串相册类型){
this.name=名称;
this.albumType=albumType;
this.trackList=newArrayList();//来自java7的菱形运算符
}
public StudioAlbum(字符串名称、艺术家){
超级(名称“工作室”);
这个艺术家=艺术家;
}

有了它,您可以设置
private
相册
的所有字段(这通常是最佳做法)。

您可以发布在使用
字符串
时遇到的错误吗?
相册
构造函数希望您为其提供相册的名称。所以你需要给它一个专辑的名字。当你调用
super
,就像你说
newalbum
一样,这一点也适用。你需要说
super(name)
。你传递了错误的参数吗?当我传递时,它会调出这个方法:
public String getArtist(){return artist;}
并因为artist而说“不兼容的类型”。这说明了什么吗?就这类问题提供提示。我只是在IDE中复制了您的代码(Eclipse就是我手边的那个),并为缺少的内容(艺术家和曲目)生成了模型(空)类。然后,在我尝试编译它之前,只有语法高亮显示和类型检查器指出错误并提供合理的更正。最好不要过于依赖IDE和工具,但有时这会使事情变得琐碎。
public Album(String name, String albumType) {
   this.name = name;
   this.albumType = albumType;
   this.trackList = new ArrayList<>(); // diamond operator from java7
}

public StudioAlbum(String name, Artist artist){
   super(name, "Studio");
   this.artist = artist ;
}