toString 方法的问题

Trouble with toString method

为什么变量 basica (Crank) 和 basicb (Pedal) 不与其他值一起显示在我的数组列表中?还有另一个 class 称为 Part,如果需要我可以 post,但它似乎忽略了那些变量。

主要Class

    package main;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

public class TestAssembledPart {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner sc = new Scanner(System.in);
    List<AssembledPart> aparts = new ArrayList<AssembledPart>();
    aparts.add(new AssembledPart("a200", "Crank & Pedal", 10, 3.5, "Crank", "Pedal"));

     System.out.println("part before stock level change - start");
     System.out.println(AssembledPart.toAssembledString(aparts));

}

}

组装部件class

 package main;
 import java.util.*;

public class AssembledPart extends Part {

private String basica;
private String basicb;

public AssembledPart(String id, String name, int stocklevel, double unitprice, 
                    String basica, String basicb) {

    super(id, name, stocklevel, unitprice);

    this.basica = basica;
    this.basicb = basicb;
}


public String toAssembledString(String id, String name, int stocklevel, double unitprice) {
    return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb;
}

public static String toAssembledString(Collection<AssembledPart> aparts){
    String s = "";
    for (AssembledPart apart: aparts){
        s += apart + "\n";
    }
    return s;
}
}

部分Class

  package main;
  import java.util.*;

  public class Part {

private String id;
private String name;
private int stocklevel;
private double unitprice;

private int qty = 6000;

public Part(String id, String name, int stocklevel, double unitprice){
    this.id = id;
    this.name = name;
    this.stocklevel = stocklevel;
    this.unitprice = unitprice;
}


String partsAvailable()
{
    //String newLine = System.getProperty("line.separator");
    return (id + "\t" + name + "\t    " + stocklevel + "\t\t   " + unitprice);
}


public String getID() {
    return id;
}

public void setID(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getStockLevel(){
    return stocklevel - qty;
}

public void setStockLevel(int stocklevel){
    this.stocklevel = stocklevel;
}

public double getUnitPrice(){
    return unitprice;
}

public void setUnitPrice(double unitprice){
    this.unitprice = unitprice;
}

public void replenish(int qty){
    this.stocklevel = stocklevel + qty;
}

public double supply(int qty){
    return unitprice * qty;
}

public String toString() {
    return id + " | " + name + " | " + stocklevel + " | " + unitprice;
}


public static String toString(Collection<Part> parts){
    String s = "";
    for (Part part: parts){
        s += part + "\n";
    }
    return s;
}

}

因为您在 'toAssembledString' 方法中将空值分配给字符串;

  String s = "";

无论你做什么,它总是 return 1 个组合,因为你正在做 s="";

将其声明为 class 变量并将其附加到旧变量。

用户 StringBuilder,这里更好。

此外,创建一个方法 s=""; 以防您需要清除字符串的数据,以便您可以将该静态字符串重新用于其他对象,否则它会将数据附加到旧的。

因为您没有调用 toAssembledString() 方法,所以您正在调用 toString() 方法,这是(我预计)在第 class.

部分中实现的

改为:

for (AssembledPart apart: aparts){
    s += apart + "\n";

这样做:

for (AssembledPart apart: aparts){
    s += apart.toAssembledString() + "\n";

那么,你应该避免实例方法中的参数:

public String toAssembledString(String id, String name, int stocklevel, double unitprice) {
    return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb;
}

按如下操作:

public String toAssembledString() {
    return super.toString() + " | " + basica + " | " + basicb;
}

感谢@Parker_Halo 的评论

是因为调用了Part.toString(),部分没有变量basica和basicb。打印前必须调用 toAssembledString() 而不是简单的部分。

如果您不想更改 toAssembledString(Collection<AssembledPart> aparts)'

的实现,您可以覆盖 toString() 而不是编写自己的重载 toAssembledString(String id, String name, int stocklevel, double unitprice)
public String toString() {
    return id + " | " + name + " | " + stocklevel + " | " + unitprice + " | " + basica + " | " + basicb;
}

看起来你的部分 class 看起来像这样

public class Part {
    String a, b;
    int c;
    double d;

    public Part(String a, String b, int c, double d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    @Override
    public String toString() {
        StringJoiner j = new StringJoiner(" | ");
        j.add(a);
        j.add(b);
        j.add(Integer.toString(c));
        j.add(Double.toString(d));
        return j.toString();
    }
}

通过调用 s += apart + "\n";,您会自动调用 class 的 toString 方法,它看起来会被 Part class 覆盖。要为您的 AssembledPart class 获得所需的输出,您应该覆盖 toString 方法并添加像这样的附加信息。

@Override
public String toString() {
    StringJoiner j = new StringJoiner(" | ");
    j.add(super.toString());
    j.add(basica);
    j.add(basicb);
    return j.toString();
}