Java null构造函数

时间:2017-08-19 10:01:10

标签: java 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();
    }
}

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

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

}

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

public class StarOnline {

    static WebDriver driver;
    static String baseUrl;

    public static void main(String[] args) throws InterruptedException {

        baseUrl = "https://www.staronline.org.uk/demo_register.asp";

                System.setProperty("webdriver.chrome.driver", "C:\\Automation\\libs\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();

                driver.get(baseUrl);

        List<WebElement> checkbox = driver.findElements(By.className(".frm"));
        System.out.println("list size = " + checkbox.size());
        for (WebElement ell:checkbox)
        {
            if (ell.getText().contains("791"))
                ell.click();
            else System.out.println("Element not found");
            break;
        }

我肯定错过了一些东西,但我不确切知道在哪里,任何想法如何解决这个问题?谢谢! :d 问题是,当我使用.printSongs()方法时,它会打印第一个值

4 个答案:

答案 0 :(得分:2)

来自你的课堂歌曲:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

假设通过

songs.addSong(&#34;多兰基尔托&#34;,11.11); ,  你的意思是

Songs.addSong(&#34;多兰基尔托&#34;,11.11);

这种情况正在发生,因为您没有重新分配歌曲参考。替换每个陈述

songs.addSong(&#34;多兰基尔托&#34;,11.11);

歌曲= Songs.addSong(&#34;多兰基尔托&#34;,11.11);

它会起作用。 进一步的观察是你的逻辑在'findSong()&#39;因为平等总会产生错误。

答案 3 :(得分:0)

请使用以下修改后的主要课程。

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