Arraylist不能添加元素

时间:2013-12-10 20:16:08

标签: arraylist implements

为什么这不适合我?

public class Band {

    public void addMember(Musician musician) {      
    musicians.add(musician);
    System.out.println("Muscian: " + musician + "was successfully added");
    }
}

public static void main(String[] args) {

        Band Beatles = new Band("Beatles");
        Beatles.addMember("John Lennon");
}

public class Musician {


private String name;

    public Musician(String name, Instrument instrument) {
        this.name = name;
        Instrument Guitar = instrument;
    }

    public void play() {

    }
}

1 个答案:

答案 0 :(得分:0)

Beatles.addMember("John Lennon");

应该是

Beatles.addMember(new Musician("John Lennon", new Instrument(new Guitar()));

我怀疑,没有看到你的实际乐器类,根据你的评论,该行应该有效。

问题在于您将某些对象视为字符串。您需要首先从定义它的类中创建对象。