我在使用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();
}
答案 0 :(得分:1)
这是因为在数组上使用<Match ContentType="Page">
<Match DisplayType="Detail">
<Match Path="~/">
...
</Match>
</Match>
</Match>
方法会为您提供该数组的内存地址。您希望在自定义toString()
方法中使用Arrays.toString(publications)
。
此外,您循环显示数组中的每个对象,但打印整个数组而不是单个实例:
toString()
如果您的输出问题仅为零,请确保超类中的变量为for(int i = 0; i < publications.length; i++)
{
System.out.println("Publication [" + i + "] is " + publications[i].toString());
}
或protected
。然后在您的子类'public
方法中使用toString()
关键字访问它们:
this
您还应该使用public String toString()
{
String display = "Title " + this.title + ",Price " + setPrice() + ", Cost " + this.cost + ", Quantity " + this.quantity;
return display;
}
关键字访问this
方法中的cost
。
修改强>
将零作为输出的问题可能是由于setPrice()
类中的构造函数。您正在以错误的方式分配变量。它应该是这样的:
Publication