【问题标题】:Why does my ArrayList of objects print out one variable stored inside the object, but not the other为什么我的对象 ArrayList 打印出存储在对象内的一个变量,而不是另一个
【发布时间】:2020-03-28 12:59:09
【问题描述】:
 HandDrawn class extends the super-class Car, as you can see. The problem is it doesn't print out the the String name when i try to print the ArrayList that stores the objects. Btw, the ArrayList stores objects of the class Car.

冷静点,如果我对这个问题做了一些冒犯了你的小感受的事情,请不要投反对票...告诉我发生了什么,以便我知道未来。

public class HandDrawn extends Card {
    private boolean niceDrawing;

    public HandDrawn(String name, boolean niceDrawing) {
        super(name);
        this.niceDrawing = niceDrawing;
    }

    @Override
    public String toString() {
        return "HandDrawn{" +
                "niceDrawing=" + niceDrawing +
                '}';
    }

    public void setNiceDrawing() {
        this.niceDrawing = niceDrawing;

    }
    public boolean getNiceDrawing(boolean niceDrawing) {
        return this.niceDrawing;
    }
}
public class Main {
static ArrayList<Card> cards = new ArrayList<>();

public static void main(String[] args) {
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
    Main myApp = new Main();
    myApp.cardList(cards);



}
public void cardList(ArrayList<Card> e) {
    for (int i = 0; i <e.size(); i++) {
        System.out.println(e.get(i));
    }
}

}

这是Main类和HandDrawn类

【问题讨论】:

  • 如果您将 Car 超类和使用 ArrayList 的特定案例的示例包含在内,这个问题会更清楚,但它似乎无法按您的预期工作。
  • @DatuPuti Ye 已修复
  • 一目了然,我认为这不会像您在此处列出的那样编译,但我可能是错的……使用当前格式(或缺少格式)很难阅读。也许问题是在调用 cardList 之前您从未调用静态方法“main”——ArrayList 中没有任何内容,因为您尚未执行以“cards.add...”开头的行。
  • 可以编译,可能是因为 HandDrawn 类里面的 toString 里面没有实例变量名?
  • 不,它在类定义中,所以你不需要使用“this”关键字来明确...对不起,我已经很久没有使用Java了——我现在回答你的问题。

标签: class object inheritance arraylist


【解决方案1】:

同样,我不知道你的超类“卡片”中有什么,但也许这会有所帮助:

在您的 HandDrawn.java 文件中:

public class HandDrawn extends Card {
  private boolean niceDrawing;

  public HandDrawn(String name, boolean niceDrawing) {
    super(name);
    this.niceDrawing = niceDrawing;
  }

  @Override
  public String toString() {
    return "HandDrawn{" + "niceDrawing=" + this.niceDrawing + "}";
  }

  // Notice I have modified the parameters of your getters
  // and setters because it looked as if you had swapped them:
  public void setNiceDrawing(boolean niceDrawing) {
    this.niceDrawing = niceDrawing;
  }

  public boolean getNiceDrawing() {
    return this.niceDrawing;
  }
}

在您的 Main.java 文件中:

public class Main {
  static ArrayList<Card> cards = new ArrayList();

  public static void main(String[] args) {
    // This is the entry point of your program... execution begins here.
    cards.add(new HandDrawn("Anna", true));
    cards.add(new HandDrawn("Kalle", false));
    cardList(cards);
  }

  // The following two lines are out of place and should not be in class definition:
  // Main myApp = new Main();  
  // myApp.cardList(cards);

  // I added static keyword so that it could be used in call above
  public static void cardList(ArrayList<Card> e) {
    for (int i = 0; i < e.size(); i++) {
      System.out.println(e.get(i));
  }
}

如果您想在代码的另一部分中使用“Main”类,那么这可能意味着执行从其他地方开始并且永远不会命中“main”方法。如果是这种情况,您将需要显式调用它(例如:Main.main(null);)或重新组织代码以在“Main”类的构造函数中执行此操作。那么您的 Main myApp = new Main();myApp.cardList(cards); 行在该上下文中是有意义的,并且 cardList 方法需要是您最初编写的非静态成员方法。

编辑:

我想我误解了你的格式......你的代码应该如下工作:

在您的 Main.java 文件中:

public class Main {
  static ArrayList<Card> cards = new ArrayList();

  public static void main(String[] args) {
    // This is the entry point of your program... execution begins here.
    cards.add(new HandDrawn("Anna", true));
    cards.add(new HandDrawn("Kalle", false));
    Main myApp = new Main();
    myApp.cardList(cards);
  }

  public void cardList(ArrayList<Card> e) {
    for (int i = 0; i < e.size(); i++) {
      System.out.println(e.get(i));
  }
}

你期待看到什么?我再次无法测试您的代码,因为我没有“Card”类,但是当我使用“HandDrawn”类代替“Card”(不扩展“Card”)时,它可以按预期工作并打印:

HandDrawn{niceDrawing=true}
HandDrawn{niceDrawing=false}

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2020-07-11
    相关资源
    最近更新 更多