toString() 不适用于 super 和 sub 类
toString() not working with super and sub classes
我在正确显示 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();
}
这是因为在数组上使用 toString()
方法可以获得该数组的内存地址。您希望在自定义 toString()
方法中使用 Arrays.toString(publications)
。
此外,您循环显示数组中的每个对象,但打印整个数组而不是单个实例:
for(int i = 0; i < publications.length; i++)
{
System.out.println("Publication [" + i + "] is " + publications[i].toString());
}
对于输出仅为零的问题,请确保超级 class 中的变量是 protected
或 public
。然后在你的 subclass' toString()
方法中使用 this
关键字访问它们:
public String toString()
{
String display = "Title " + this.title + ",Price " + setPrice() + ", Cost " + this.cost + ", Quantity " + this.quantity;
return display;
}
您还应该在 setPrice()
方法中使用 this
关键字来访问 cost
。
编辑:
将零作为输出的问题可能是由于 Publication
class 中的构造函数造成的。您正在以错误的方式分配变量。它应该看起来像这样:
public Publication(String pubTitle, double pubCost, int pubQuantity)
{
title = pubTitle;
cost = pubCost;
quantity = pubQuantity;
}
我在正确显示 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();
}
这是因为在数组上使用 toString()
方法可以获得该数组的内存地址。您希望在自定义 toString()
方法中使用 Arrays.toString(publications)
。
此外,您循环显示数组中的每个对象,但打印整个数组而不是单个实例:
for(int i = 0; i < publications.length; i++)
{
System.out.println("Publication [" + i + "] is " + publications[i].toString());
}
对于输出仅为零的问题,请确保超级 class 中的变量是 protected
或 public
。然后在你的 subclass' toString()
方法中使用 this
关键字访问它们:
public String toString()
{
String display = "Title " + this.title + ",Price " + setPrice() + ", Cost " + this.cost + ", Quantity " + this.quantity;
return display;
}
您还应该在 setPrice()
方法中使用 this
关键字来访问 cost
。
编辑:
将零作为输出的问题可能是由于 Publication
class 中的构造函数造成的。您正在以错误的方式分配变量。它应该看起来像这样:
public Publication(String pubTitle, double pubCost, int pubQuantity)
{
title = pubTitle;
cost = pubCost;
quantity = pubQuantity;
}