Java 将对象存储到ArrayList中,并打印出给定制造商在给定年份制作的详细信息

Java 将对象存储到ArrayList中,并打印出给定制造商在给定年份制作的详细信息,java,Java,所以基本上我需要创建类MusicShop,它将乐器对象存储在名为 仪器。然后定义一种方法来打印出给定钢琴制作的所有钢琴的细节 在给定的年份之前 这就是我到目前为止所做的 public class Piano extends Instrument { // instance variables - replace the example below with your own private int year; /** * Constructor for obj

所以基本上我需要创建类MusicShop,它将乐器对象存储在名为 仪器。然后定义一种方法来打印出给定钢琴制作的所有钢琴的细节 在给定的年份之前

这就是我到目前为止所做的

public class Piano extends Instrument
{
    // instance variables - replace the example below with your own
    private int year;

    /**
     * Constructor for objects of class Piano
     */
    public Piano(int year, String maker)
    {
        // initialise instance variables
        super(maker);
        this.year = year;
        
    }

     /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public int getyear()
    {
        // put your code here
        return year;
    }
    
    public String toString()
    {
        return super.toString() + " Year: " + year;
    }
    
    
    
    public void play(String music)
    {
        System.out.println("Playing piano: " + music);
    }
    
    
}
公共类音乐商店
{
//实例变量-将下面的示例替换为您自己的
私人ArrayList文书;
/**
*MusicShop类对象的构造函数
*/
公共音乐商店()
{
Instruments=新的ArrayList();
}
公共int getTotal()
{
返回仪器。尺寸();
} 
/**
*方法示例-将此注释替换为您自己的注释
*
*@param y方法的示例参数
*@返回x和y的总和
*/
公共无效打印详细信息()
{
//把你的代码放在这里
用于(仪表A仪表:仪表){
System.out.println(Instruments.getTotal);
}
}
}

不确定如何将抽象类中的对象存储到arraylist中并打印其详细信息。

可以将类型为
Piano
的对象添加到乐器中,而不会出现问题:

public class MusicShop
{
    // instance variables - replace the example below with your own
    private ArrayList<Instrument> Instruments;
    
    /**
     * Constructor for objects of class MusicShop
     */
    public MusicShop()
    {
        Instruments = new ArrayList<Instrument>();
    }
    
    public int getTotal() 
    {
        return Instruments.size();
    } 

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public void printDetailsOfPiano()
    {
        // put your code here
       
        for(Instrument aInstrument : Instruments) {
             System.out.println(Instruments.getTotal);
        }
    }
}
当您想要打印仪器时,一种方法可以是:

public abstract class Instrument { 
...
   abstract public void print();
}

public class Piano extends Instrument {
  ...
  @Override
  public void print() {
     System.out.println(getMaker() + " " + getYear() );
  }
}
顺便说一下,在Java中,惯例是变量名不大写:

public void printDetailsOfPiano() {
     Instruments.forEach( Instrument::print );
}
List instruments=new ArrayList();

我特别需要使用ArrayList,这并不重要。。。但这就是我正在使用的…为什么你认为我没有使用ArrayList?你是对的,误读了,我该如何在指定的年份之前打印指定制造商生产的所有钢琴。
public abstract class Instrument { 
...
   abstract public void print();
}

public class Piano extends Instrument {
  ...
  @Override
  public void print() {
     System.out.println(getMaker() + " " + getYear() );
  }
}
public void printDetailsOfPiano() {
     Instruments.forEach( Instrument::print );
}
List<Instrument> instruments = new ArrayList<>();