java-如何使用添加了toString的arraylist访问类中的成员
作者:互联网
我有一个类,可以说是CargoShip,它是“ Starcraft”的派生类,它实现了IStarcraft接口.
我有一个函数public static ArrayList< String>. getSpacecraftDescriptionsByCommissionYear(ArrayList< ISpacecraft>舰队)
问题:CargoShip具有toString,可打印名称,佣金年份等.
我想做两件事:首先,我想使用每个Ship的toString(类似于CargoShip中的toString),其次,我希望按CommissionYear对它进行排序.
问题:将toString添加到arrayList之后,我不知道如何访问CommissionYear字段.
ArrayList<String> strCommissions = new ArrayList<String>();
for(ISpacecraft flee : fleet)
{
strCommissions.add(flee.toString());
}
//Collections.sort(//What to write here??//);
return strCommissions;
}
如果需要,这是CargoShip类:
package starfleet;
public class CargoShip extends Spacecraft{
private int numberOfSpaceCranes;
static int count = 0;
public CargoShip(String name, int commissionYear, float maximalSpeed,int cargoCapacity, int numberOfSpaceCranes)
{
this.name = name;
this.commissionYear = commissionYear;
if(MaximalSpeed())
this.maximalSpeed = maximalSpeed;
this.cargoCapacity = cargoCapacity;
this.numberOfSpaceCranes = numberOfSpaceCranes;
count++;
}
public int getNumberOfSpaceCranes ()
{
return this.numberOfSpaceCranes;
}
@Override
public String getName() {
return this.name;
}
@Override
public int getCommissionYear() {
return this.commissionYear;
}
@Override
public float getMaximalSpeed() {
if(MaximalSpeed())
return this.maximalSpeed;
else
return 0f;
}
@Override
public int getCargoCapacity() {
return this.cargoCapacity;
}
@Override
public int getFirePower() {
return this.firePower;
}
@Override
public int getAnnualMaintenanceCost() {
int cost = 0;
this.commissionYear = 2000;
cost += getCommissionYear();
cost += (500 * this.numberOfSpaceCranes);
cost += (2 * getCargoCapacity()); //To check: TotalCargoWeightCapacity?
// TODO Auto-generated method stub
return 0;
}
public String toString()
{
return
"Name = " + getName() + System.lineSeparator() +
"CommissionYear = " + getCommissionYear() + System.lineSeparator() +
"MaximalSpeed = " + getMaximalSpeed() + System.lineSeparator() +
"CargoCapacity = " + getCargoCapacity() + System.lineSeparator() +
"FirePower = " + getFirePower() + System.lineSeparator() +
"AnnualMaintenanceCost = " + getAnnualMaintenanceCost() + System.lineSeparator() +
"numberOfSpaceCranes = " + getNumberOfSpaceCranes() + System.lineSeparator();
}
}
解决方法:
首先,您可以将清单舰队复制到一个新的舰队中:
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
然后您可以按佣金年份对其进行排序:
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
然后创建字符串列表:
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
因此您的功能变为:
public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet){
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
return strCommissions;
}
读:
> Java 8 Lambda : Comparator example
标签:arraylist,polymorphism,java 来源: https://codeday.me/bug/20191111/2019282.html