尝试创建一个包含另一个类中的对象的类

时间:2020-01-08 19:33:35

标签: java arrays oop

我创建了一个名为Album的类,这是一个

public class Album {

    private String Titulo;
    private int temas;
    private int ano;

    public Album(String Titulo2, int temas2, int ano2) {
        this.Titulo = Titulo2;
        this.temas = temas2;
        this.ano = ano2;
    }

    public Album(String Titulo2, int temas2) {
        this.Titulo = Titulo2;
        this.temas = temas2;
    }

    public int getAno() {
        return this.ano;
    }

    public int getTemas() {
        return this.temas;
    }

    public String getTitulo() {
        return this.Titulo;
    }

    public void setAno(int ano) {
        this.ano = ano;
    }

    public boolean foiEditadoNesteSeculo() {
        if (this.ano > 2000) {
            return true;
        } else {
            return false;
        }
    }

    public void adicionaTemasBonus(int x) {
        this.temas += x;
    }

    public void mostraAlbum() {
        System.out.println(this.Titulo + " (editado em " + this.ano + "; tem " + this.temas + " temas)");
    }
}

工作正常。问题是老师要求我创建一个名为Band的新课程,它必须具有一系列的相册。 Band对象应使用一个表示专辑数量(数组长度)限制的int声明。我已经对如何使用数组有了一些想法,但是对于如何创建一个包含另一个类的对象的数组类型,以及如何使用对象的属性返回某些东西,我还是一无所知。我想我可以在正确创建类之后弄清楚其余部分。

道歉,正如葡萄牙语中描述的那样,我在翻译方面经验不足。

4 个答案:

答案 0 :(得分:1)

public class Band {
    private Album[] albums;
    private numberOfAlbums;
    //...

    // create an empty constructor
    Band(){
        albums = new Album[];
        numberOfAlbums = 0;
    }
    // constructor that receives the albums
    Band(Album[] albums){
        this.albums = albums;
        this.numberOfAlbums = albums.length;
    }

    // constructor that receives the number of albums
    Band(int numOfAlbums){
        this.numberOfAlbums = numOfAlbums;
        this.albums = new Album[numOfAlbums];
    }

    // add getters and setters
    // example of adding a new album
    public void addNewAlbum(Album album){
        if(this.numOfAlbums == this.albums.length){
            // you need to create a new array with a bigger size, copy the existing data and insert the album
            // or whatever you'd like
        } else {
             this.albums[this.numOfAlbums] = album;
             // increment the numOfAlbums
             this.numOfAlbums++;
        }
    }
}

private class Album {
    //...
}

答案 1 :(得分:1)

我认为使用List可以更容易地管理它,因此您可以随时根据需要添加任意数量的Albums,因为问题陈述需要Array我举例说明了Band类。

我还在main类的底部添加了Band方法来测试程序:

public class Band {
    private int totalAlbums;
    private Album[] albums;
    private int currentNumberOfAlbums;

    public Band(int totalAlbums) {
        this.totalAlbums = totalAlbums;
        this.albums = new Album[totalAlbums];
        this.currentNumberOfAlbums = 0;
    }

    public Band(Album[] albums) {
       this.totalAlbums = albums.length;
       this.albums = albums;
       this.currentNumberOfAlbums = this.totalAlbums;
    }

    public void addNewAlbum(String titulo, int temas, int ano) {
        if (this.currentNumberOfAlbums == totalAlbums) {
            System.out.println("Warning: Cannot add any more albums, limit reached.");
            return;
        }
        this.albums[this.currentNumberOfAlbums++] = new Album(titulo, temas, ano);

    }

   public void printAlbums() {
       for (Album a : this.albums) {
           a.mostraAlbum();
       }
   }

    public static void main(String [] args) {
        Band b = new Band(3);
        b.addNewAlbum("The First", 4, 2001);
        b.addNewAlbum("The Second", 98, 2055);
        b.addNewAlbum("The Finale", 12, 2011);
        b.addNewAlbum("The Extra", 12, 2111);

        b.printAlbums();
    }
}

此代码中有一些需要注意的地方。

首先,要解决您的直接问题,您可以像使用Album[]一样将自定义类用作数组,就像其他任何类/基元一样。

第二,您将需要一个Band构造函数,该构造函数基于传递给它的整数实例化Album的数组,因此您知道有多少张专辑是有限的。您可以在this.albums = new Album[totalAlbums];行中看到它。

接下来,您需要一种向Album数组中添加新Album[] 的方法。可以通过几种不同的方法来完成此操作,但是我选择的方法是创建一个方法addNewAlbum(String, int, int)来完成此示例,每次创建新专辑时,方法也会使currentNumberOfAlbums增加1被添加。这很有用,因此即使Album已满,您也知道何时尝试添加totalAlbums!如果调用ArrayIndexOutOfBoundsException的时间过多,这将防止代码中出现addNewAlbum

最后,在addNewAlbum中,您需要使用Album来调用new Album(titulo, temas, ano)构造函数。

在我的示例main中,创建了一个限制为Band个相册的3,并尝试将4个相册添加到其中,并添加了前三个成功,并且没有添加第4个,而是打印警告消息,提示超出限制。

我还添加了一个printAlbums()方法,该方法将使用您的mostraAlbum()打印Album数组中的每个albums

输出:

Warning: Cannot add any more albums, limit reached.
The First (editado em 2001; tem 4 temas)
The Second (editado em 2055; tem 98 temas)
The Finale (editado em 2011; tem 12 temas)

编辑:

我添加了Band(Album[] albums)构造函数,可以使用以下命令调用它:

Album[] albums = new Album[3];
//Add your albums into this variable
Band b = new Band(albums);

答案 2 :(得分:0)

您只需要添加[]即可定义该字段为数组。

public class Band {
    private int totalAlbums;
    private Album[] albums;
    //...
}

private class Album {
    //...
}

答案 3 :(得分:0)

我希望这个例子对您有帮助。

    private Album[] albums; // array of album
    private int albumLimit; // limit for album

    public Band(int albumLimit) {
        this.albumLimit = albumLimit; // initialize limit
        this.albums = new Album[albumLimit]; // set limit of album array
    }

    // here it creates a new Album every time the loop runs
    // you can fill the array in other ways too
    public void fillAlbum() {
        for (int i = 0; i < albumLimit; i++) {
            String name = "name_" + i;
            int team = i;

            albums[i] = new Album(name, team);
        }
    }

    public void printAlbum() {
        for (int i = 0; i < albumLimit; i++) {
            System.out.println("Name :" + albums[i].getTitulo());
            System.out.println("Team :" + albums[i].getTemas());
            System.out.println();
        }
    }
}