Java 空构造函数



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

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());
}
}

当然我错过了一些东西,但我不知道确切的位置,有什么想法可以解决这个问题吗?谢谢!:D 问题是当我使用 .printSongs(( 方法时,它会打印第一个值

来自你的班级 歌曲:

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));

你的Song#addSong方法完全错误。您正在那里创建新的Sound对象。事实上,你不需要这样做。删除该方法并像这样使用它

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

等。

请在主类下面使用修改。

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

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

songs=songs.addSong("Dorisadsan",11.11);
albums.addSong(songs);
System.out.println(songs.getTitle());
albums.addSong(songs);
albums.printSongs();
}
}

假设

songs.addSong("Dorin",11.11);

,你的意思是

Songs.addSong("Dorin",11.11);

.发生这种情况是因为您没有重新分配歌曲引用。替换每个语句

songs.addSong("Dorin",11.11);  

songs=Songs.addSong("Dorin",11.11);  

它会起作用。 进一步的观察是,你在"findSong(("中表示相等的逻辑总是会产生假。

相关内容

  • 没有找到相关文章

最新更新