Java字符串toString方法null错误
请告诉我,我正在尝试使用以下方法获取订单中的项目,这些项目是咖啡松饼和提姆饼 公共字符串toString() 每件事情都正常工作,除了我没有获得订单的项目,我为它们获取null,而不是以下内容:Java字符串toString方法null错误,java,Java,请告诉我,我正在尝试使用以下方法获取订单中的项目,这些项目是咖啡松饼和提姆饼 公共字符串toString() 每件事情都正常工作,除了我没有获得订单的项目,我为它们获取null,而不是以下内容: Muffin "Bran" ,3 Coffee "Latte" , 1 TimBits "Assorted" , 24 Muffin "Chocolate" , 1 Co
Muffin "Bran" ,3
Coffee "Latte" , 1
TimBits "Assorted" , 24
Muffin "Chocolate" , 1
Coffee "Decaf" , 2
TimBits "Chocolate" , 12
Muffin "PeanutButter" , 2
Muffin "Blueberry" , 5
上面的数字表示订单中每个项目的数量
class Item
{
protected String description;
protected int quantity;
protected String kind;
private double cost;
public double getCost()
{
return this.cost;
}
public Item (String description, int quantity)
{
this.description = description;
this.quantity = quantity;
}
public String toString()
{
return "Item: " + " " +kind + " " + ": description: " + " " +description +"quantity:" +" " + quantity ;
}
class Coffee extends Item
{
protected double cost1Coffee;
String kind = "Coffee";
public Coffee (String description, int quantity)
{
super(description, quantity);
cost1Coffee = 4 ;
}
}
}
class Muffin extends Item
{
protected double cost1Muffin;
protected double cost2Muffin;
protected double cost3Muffin;
String kind = "Muffin";
public Muffin (String description, int quantity)
{
super(description,quantity);
cost1Muffin = 1;
cost2Muffin = 0.75;
cost3Muffin = 0.50;
}
}
class TimBits extends Item
{
protected double cost1TimBits ;
String kind = "TimBits";
public TimBits (String description, int quantity)
{
super(description, quantity);
cost1TimBits = 0.25;
}
}
/***************************************************************/
/***************************************************************/
class A4Q1Util
{
private static ArrayList<Item> order;
private static int count = 0;
public static Item getItem()
{
Item item;
if (order==null)
{
order = new ArrayList<Item>();
order.add(new Muffin("Bran", 3));
order.add(new Coffee("Latte", 1));
order.add(new TimBits("Assorted", 24));
order.add(new Muffin("Chocolate", 1));
order.add(new Coffee("Decaf", 2));
order.add(new TimBits("Chocolate", 12));
order.add(new Muffin("PeanutButter", 2));
order.add(new Muffin("Blueberry", 5));
}
item = null;
if (count<order.size())
{
item = order.get(count);
count++;
}
{
return item;
}
}
}
output:
Item: null : description: Branquantity: 3
Item: null : description: Lattequantity: 1
Item: null : description: Assortedquantity: 24
Item: null : description: Chocolatequantity: 1
Item: null : description: Decafquantity: 2
Item: null : description: Chocolatequantity: 12
Item: null : description: PeanutButterquantity: 2
Item: null : description: Blueberryquantity: 5
Program completed normally.
类项目
{
保护字符串描述;
保护整数数量;
保护串类;
私人双重成本;
公共成本
{
退还此费用;
}
公共项(字符串描述,整数数量)
{
this.description=描述;
这个。数量=数量;
}
公共字符串toString()
{
返回“项目:”+“+种类+”+“+”:说明:“+”+说明+”数量:“+”+数量;
}
类咖啡扩展项
{
受保护的双重成本1份;
String kind=“咖啡”;
公共咖啡(字符串描述,整数数量)
{
超级(说明、数量);
成本1费用=4;
}
}
}
类Muffin扩展项
{
受保护的双成本1 Muffin;
受保护的双cost2Muffin;
保护双成本3 Muffin;
String kind=“Muffin”;
公共松饼(字符串描述,整数数量)
{
超级(说明、数量);
cost1Muffin=1;
cost2Muffin=0.75;
cost3Muffin=0.50;
}
}
类及其扩展项
{
受保护的双重成本;
String kind=“TimBits”;
公共音色(字符串描述,整数数量)
{
超级(说明、数量);
成本1分位数=0.25;
}
}
/***************************************************************/
/***************************************************************/
A4Q1Util类
{
私有静态数组列表顺序;
私有静态整数计数=0;
公共静态项getItem()
{
项目;
if(order==null)
{
订单=新的ArrayList();
订购。添加(新松饼(“麸皮”,3));
添加(新咖啡(“拿铁”,1));
订单。添加(新音色(“什锦”,24));
订购。添加(新松饼(“巧克力”),1);
添加(新咖啡(“无咖啡因”,2));
添加(新的TimBits(“巧克力”,12));
订购。添加(新松饼(“花生酱”,2));
订购。添加(新松饼(“蓝莓”,5));
}
item=null;
if(count不要在每个子类中声明字段kind
。将赋值添加到构造函数中,例如:
public TimBits (String description, int quantity) {
super(description, quantity);
kind = "TimBits";
cost1TimBits = 0.25;
}
不要在每个子类中声明字段kind
。将赋值添加到构造函数中,例如:
public TimBits (String description, int quantity) {
super(description, quantity);
kind = "TimBits";
cost1TimBits = 0.25;
}
在项#toString
方法中:
public String toString() {
return "Item: " + " " +kind + " " + ": description: " + " " +description +"quantity:" +" " + quantity ;
}
您使用kind
变量,但从不通过应用程序初始化它
这是因为您是每个子类上的kind
字段。相反,在父类中将其声明为protected
,并在每个子类上相应地初始化它
class Coffee extends Item {
protected double cost1Coffee;
//drop this
//String kind = "Coffee";
public Coffee(...) {
super(...);
kind = "Coffee";
}
}
您甚至可以通过将kind
字段标记为final
并禁止在执行构造函数时修改其子类以外的任何其他类来限制该字段。此示例:
class Item {
//other fields...
protected final String kind;
protected Item (String description, int quantity, String kind) {
this.description = description;
this.quantity = quantity;
this.kind = kind;
}
public Item (String description, int quantity) {
this(description, quantity, "uncategorized");
}
}
class Coffee extends Item {
public Coffee(String description, int quantity) {
//similar in other subclasses of Item
super(description, quantity, "Coffee");
}
}
在项#toString
方法中:
public String toString() {
return "Item: " + " " +kind + " " + ": description: " + " " +description +"quantity:" +" " + quantity ;
}
您使用kind
变量,但从不通过应用程序初始化它
这是因为您是每个子类上的kind
字段。相反,在父类中将其声明为protected
,并在每个子类上相应地初始化它
class Coffee extends Item {
protected double cost1Coffee;
//drop this
//String kind = "Coffee";
public Coffee(...) {
super(...);
kind = "Coffee";
}
}
您甚至可以通过将kind
字段标记为final
并禁止在执行构造函数时修改其子类以外的任何其他类来限制该字段。此示例:
class Item {
//other fields...
protected final String kind;
protected Item (String description, int quantity, String kind) {
this.description = description;
this.quantity = quantity;
this.kind = kind;
}
public Item (String description, int quantity) {
this(description, quantity, "uncategorized");
}
}
class Coffee extends Item {
public Coffee(String description, int quantity) {
//similar in other subclasses of Item
super(description, quantity, "Coffee");
}
}
您正在项的每个子类内分配字符串种类
。但是,此种类
与超类项
的种类
字段不同,因为子类字段屏蔽了超类的种类。您有两个选项来解决此问题
展开项
构造函数以接受字符串类型
,并添加到对super
的调用中
super(description,quantity,"Muffin");
在子类构造函数中分配给kind
,以便将正确的kind
分配给(在项中声明的一个,由toString()
使用)
您正在项的每个子类内分配字符串种类
。但是,此种类
与超类项
的种类
字段不同,因为子类字段屏蔽了超类的种类。您有两个选项来解决此问题
展开项
构造函数以接受字符串类型
,并添加到对super
的调用中
super(description,quantity,"Muffin");
在子类构造函数中分配给kind
,以便将正确的kind
分配给(在项中声明的一个,由toString()
使用)
看起来您从未初始化变量kind
,即使您从超类“Item”扩展到所有子类,也只是重载变量名kind
并重新定义它。因此,当您调用toString()方法时,您的父类(Item)从不初始化kind
变量。您需要做的是在构造函数中设置kind
,以便可以通过super()传入它。看起来您从未初始化变量kind
,即使您是从超类“Item”进行扩展在所有子类中,只需重载变量名kind
,并重新定义它。因此,当调用toString()方法时,父类(项)永远不会初始化它的kind
变量。你需要做的是在构造函数中设置kind
,这样你就可以通过super()传入它。实际上,为什么当前的解决方案不起作用呢?也就是说,为什么