简体   繁体   中英

Abstract class ArrayList toString

Ok guys I have an abstract Instrument class

public abstract class Instrument {
String name;

public Instrument(String name) {
    this.name = name;
}
public void warmUp() {
    System.out.println("Do re mi"); 
}
public abstract String Play(String notes);
}

I use a Sax and Trumpet class to extend and implement the abstract method

public class Trumpet extends Instrument {

public Trumpet(String name) {
    super(name);
}
public String play(String notes) {
    String str;
    str = name + " plays Trumpet: " + notes;
    return str;
}

public String getNotes() {
    return notes;
}

public String toString(){
    String str = name + " plays Trumpet: " + notes ;
    return str;
}

Sax

public class Sax extends Instrument {

public Sax(String name) {
    super(name);
}
    public String play(String notes) {
    String str;
    str = name + " plays Trumpet: " + notes;
    return str;
}

public String getNotes() {
    return notes;
}

public String toString(){
    String str = name + " plays Sax: " + notes ;
    return str;
}

} Now I need to create a band class that creates a band composed of Trumpets and Saxs. It allows the user to add an instrument (sax or trumpet) and play an input note. When the band plays this means all instruments should play the input note.

public class Band extends Instrument{
ArrayList<Instrument> myBand = new ArrayList<Instrument>();

public Band(String name) {
    super(name);
}

public void addInstrument(Instrument a) {
    myBand.add(a);
}

public String play(String notes) {
    for(Instrument inst : myBand) {
        inst.play(notes);
        System.out.println(inst.play(notes));
    }
    return toString();

}

It doesn't necessarily have to be in an arrayList, I just thought it would make things easier to handle. I believe I have everything straight so far, but my problem comes in the Play method of band, it should display the play the name of instrument, with all instruments playing same note.

Sample Driver:

myBand.addInstrument (new Sax("Anna"));                        
myBand.addInstrument (new Trumpet("Leon"));  
myBand.addInstrument (new Sax("Ben"));

myBand.play("Dum da-da DUM");

Output:
Anna playing the Sax : Dum da-da DUM
Leon playing the Trumpet : Dum da-da DUM
Ben playing the Sax : Dum da-da DUM

I know that I need a loop to loop through the array list but not sure how to get the play method from the sax and trumpet class and them all play the same note.

Thanks for any and all help!

I updated my code with the loop but this is the output I'm getting:

Anna plays Trumpet: Dum da-da DUM
Leon plays Trumpet: Dum da-da DUM
Ben plays Trumpet: Dum da-da DUM 

Your Instrument class declares an abstract method called Play with a String parameter. By doing so, all classes that extend from Instrument must either implement the method or be declared as abstract themselves.

Because the type Instrument has declared such a method, you can call it on any reference of that type.

Instrument instrument = new ...// some class that extends from Instrument 
instrument.Play("some note")

The call to Play() would be resolved through polymorphism and late-binding . It would use the overriden method in the implementing classes.


Java naming conventions state that methods should start with a lowercase alphabetic character and use camelCase .

You can simply loop through the arraylist and call the base (abstract) play() method.

The whole point of virtual and abstract methods is that that will call the derived implementation.

I know that I need a loop to loop through the array list but not sure how to get the play method from the sax and trumpet class and them all play the same note.

The beauty of inheritance - you don't need to get the play method from the trumpet or sax classes, you just call the play method on each Instrument :

public String Play(String notes) {
    for(Instrument inst : myBand)
    {
        inst.play(notes);
    }
}

Try it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM