【问题标题】:toString() not working with super and sub classestoString() 不适用于超类和子类
【发布时间】:2015-10-29 02:43:14
【问题描述】:

我的 toString() 方法无法正确显示。我在我的每个子类中都重载了它,我在 main 方法中调用它。我得到了所需输出的开始,但之后我得到的只是一个哈希。我也尝试使用这个 toString() 方法来显示数组的结果。

    //main method

    public class PublicationArray
{
public static void main(String[] args)
{
  Publication[] publications = new Publication[10];

  publications[0] = new Magazine("American Fisherman", 2.0, 100);      
  publications[1] = new Book("C++ Programming", 25.0, 70);
  publications[2] = new Magazine("Time", 1.5, 100);
  publications[3] = new Book("Morning Dove", 15.0, 39);
  publications[4] = new Magazine("Nerd", 3.0, 10);
  publications[5] = new Book("Walking and Chewing Gum for Dummies",14.0,40);
  publications[6] = new Magazine("New Yorker", 2.25, 28);
  publications[7] = new Book("J++ Programming", 30.0, 55);
  publications[8] = new Magazine("Devorced", 1.75, 33);
  publications[9] = new Book("Doing Nothing for Dummies", 10.0, 10);

  for(int i = 0; i < publications.length; i++)
  {
     System.out.println("Publication [" + i + "] is " + publications.toString());
  }


}
}


   //one of the subclasses, the other is identical except for the formula

public class Magazine extends Publication
{
public Magazine(String title, double cost, int quantity)
{
  super(title, cost, quantity);
}

String month;

public void setMonth(String pubMonth)
{
  month = pubMonth;
}
@Override
public double setPrice()
{
  double price = cost * 1.6;
  return price;
} 

@Override
public String toString()
{
  String display = "Title " + title + ",Price " + setPrice() +
     ", Cost " + cost + ", Quantity " + quantity;
  return display;
}
}



//Output

Publication [0] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [1] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [2] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [3] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [4] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [5] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [6] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [7] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [8] is Title ,Price 0.0, Cost 0.0, Quantity 0
Publication [9] is Title ,Price 0.0, Cost 0.0, Quantity 0

public abstract class Publication
{
String title = "";
double cost;
int quantity;

//Constructor
public Publication(String pubTitle, double pubCost, int pubQuantity)
{
  pubTitle = title;
  pubCost = cost;
  pubQuantity = quantity;
} 

//Set Methods
public void setTitle(String pubTitle)
{
  title = pubTitle;
}
public void setCost(double pubCost)
{
  cost = pubCost;
}
public void setQuantity(int pubQuantity)
{
  quantity = pubQuantity;
}

//Get Methods
public String getTitle()
{
  return title;
}
public double getCost()
{
  return cost;
}
public int getQuantity()
{
  return quantity;
}

public abstract double setPrice();


}

【问题讨论】:

    标签: java arrays loops overriding tostring


    【解决方案1】:

    这是因为在数组上使用toString() 方法会为您提供该数组的内存地址。您想在自定义 toString() 方法中使用 Arrays.toString(publications)

    此外,您正在循环显示数组中的每个对象,但打印整个数组而不是单个实例:

    for(int i = 0; i < publications.length; i++)
    {
        System.out.println("Publication [" + i + "] is " + publications[i].toString());
    }
    

    对于您的输出仅为零的问题,请确保超类中的变量为protectedpublic。然后在您的子类的toString() 方法中使用this 关键字访问它们:

    public String toString()
    {
        String display = "Title " + this.title + ",Price " + setPrice() + ", Cost " + this.cost + ", Quantity " + this.quantity;
        return display;
    }
    

    您还应该在setPrice() 方法中使用this 关键字来访问cost

    编辑:

    将零作为输出的问题可能是由于Publication 类中的构造函数造成的。您以错误的方式分配变量。它应该是这样的:

    public Publication(String pubTitle, double pubCost, int pubQuantity)
    {
        title = pubTitle;
        cost = pubCost;
        quantity = pubQuantity;
    }
    

    【讨论】:

    • 谢谢。这可以让它正确显示,现在我只需要它来显示值。我全是零
    • @Kyle 你在哪里得到全零?您可以编辑您的问题以显示您得到的输出吗?
    • 我添加了输出,它在问题的底部
    • @Kyle 你的输出修复了吗?
    • 不,我没有。我在问题中添加了构造函数和 get/set 方法。这可能与它有关。这些事情总是让我感到困惑
    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 2020-02-28
    • 2014-11-20
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多