简体   繁体   English

抽象类ArrayList toString

[英]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 我使用Sax和Trumpet类来扩展和实现抽象方法

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. 它不一定必须在arrayList中,我只是认为这会使事情更容易处理。 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 安娜(Sanna)演奏萨克斯:Dum da-da DUM
Leon playing the Trumpet : Dum da-da DUM 莱昂(Leon)吹小号:Dum da-da DUM
Ben playing the Sax : Dum da-da DUM 本萨克斯演奏: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. 我知道我需要一个循环来遍历数组列表,但是不确定如何从sax和trumpet类中获取play方法,它们都演奏相同的音符。

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. 您的Instrument类声明了一个带有String参数的abstract方法Play By doing so, all classes that extend from Instrument must either implement the method or be declared as abstract themselves. 这样,从Instrument扩展的所有类都必须实现该方法,或者自己声明为abstract

Because the type Instrument has declared such a method, you can call it on any reference of that type. 因为Instrument类型已经声明了这样的方法,所以您可以在该类型的任何引用上调用它。

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 . Play()的调用将通过多态性和后期绑定来解决。 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 . Java命名约定规定方法应以小写字母字符开头,并使用camelCase

You can simply loop through the arraylist and call the base (abstract) play() method. 您可以简单地遍历arraylist并调用基本(抽象) play()方法。

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. 我知道我需要一个循环来遍历数组列表,但是不确定如何从sax和trumpet类中获取play方法,它们都演奏相同的音符。

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 : 继承的美丽-您无需从小号或萨克斯类获取play方法,只需在每个Instrument上调用play方法:

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

Try it! 试试吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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