Why isn't this working for me?

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() {

    }
}
有帮助吗?

解决方案

Beatles.addMember("John Lennon");

should be

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

I suspect, without seeing your actual Instrument class, based on your comment that this line should work.

The problem is that you are treating some objects as Strings. You need to create the object out of the class that defines it first.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top