سؤال

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