Java 空构造函数

标签 java null

所以我有两个类,称为歌曲和专辑,它们是:

    public class Songs {
    private String title;
    private Double duration;

    public Songs(String title, Double duration) {
        this.title = title;
        this.duration = duration;
    }

    public Songs(){}


    public String getTitle() {
        return title;
    }



    public Double getDuration() {
        return duration;
    }


    public static Songs addSong(String title, Double duration){
        return new Songs(title,duration);

    }
}



    import java.util.ArrayList;
import java.util.LinkedList;

public class Albums {
    private ArrayList<Songs> albums;
    private String name;
    private String title;

    public Albums(String name, String title) {
        this.albums = new ArrayList<>();
        this.name = name;
        this.title = title;
    }

    public boolean addSong(Songs songs){
        if(findSong(songs.getTitle())==null){
            this.albums.add(songs);
            return true;
        }else{
            System.out.println("Song alredy exist!");
            return false;
        }

    }

      private Songs findSong(String songName){
        for(int i=0;i<this.albums.size();i++){
            Songs currentSong = this.albums.get(i);
            if(currentSong.equals(songName)){
                return currentSong;
            }
        }
        return null;
    }
}

问题是主要的,也许我的逻辑不对,如果我遗漏了什么,请告诉我:

 public class Main {

    public static void main(String[] args) {
        Albums albums = new Albums("Alexandru", "Doi dsadcopii");
        Songs songs = new Songs("Patrascu",4.44);
        songs.addSong("Dorin",11.11);
        albums.addSong(songs);

        songs.addSong("Dorin",11.11);
        albums.addSong(songs);


        songs.addSong("Dorinsads",11.11);
        albums.addSong(songs);


        songs.addSong("Dorisadsan",11.11);
        albums.addSong(songs);



        System.out.println(songs.getTitle());

        albums.addSong(songs);

        albums.printSongs();
    }
}

为什么我得到相同的值?为什么 .addSong 中的那些值没有添加到列表中?这是我打印列表的代码:

    public void  printSongs() {
    for (int i = 0; i < this.albums.size(); i++) {
        System.out.println(i + 1 + "-->" + this.albums.get(i).getTitle() + "--->" + this.albums.get(i).getDuration());
    }

}

确实我遗漏了一些东西,但我不知 Prop 体在哪里,有什么想法可以解决这个问题吗?谢谢! :D 问题是当我使用 .printSongs() 方法时它会打印第一个值

最佳答案

来自类(class)的歌曲:

public static Songs addSong(String title, Double duration){
    return new Songs(title,duration);
}

这只是返回一首新歌曲,而不是将其添加到内部列表中。

您可以直接将它们添加到相册中,例如

Albums albums = new Albums("Alexandru", "Doi dsadcopii");
albums.addSong(new Songs("Patrascu",4.44));
albums.addSong(new Songs("Dorin",11.11));

关于Java 空构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45770183/

相关文章:

iphone - NSMutableDictionary 返回(null)有效键/忘记它的值?

java - Java 11下使用CXF的类转换异常

java - 为什么要覆盖父接口(interface)中的现有方法?

我们可以在 C 中将 (void *)0 称为空指针吗?

Javascript 在 JSP 中输入 Null 测试

ruby-on-rails - 检查 nil 警告意外的 nil

java - 创建 Java 应用程序以作为 Windows 服务运行

java - 在java中使用不同数据类型的枚举

Java:如何重置数组列表以使其为空

ruby-on-rails - SQLite3::ConstraintException: NOT NULL 约束失败: items.title: INSERT INTO "items"("image", "created_at", "updated_at") VALUES (?, ?, ?)