为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的 displayAll() 方法不打印存储在 ArrayList 中的值?

为什么我的 displayAll() 方法不打印存储在 ArrayList 中的值?

MYYA 2022-10-12 10:15:37
我一直致力于创建一个 GUI 来存储手机和 MP3 等小工具的详细信息。在将这些小工具的一些对象存储到数组列表中,然后点击 displayAll() 按钮后,它不会显示它们的值,而是显示 Null 和 0。我已经尝试了一切到放弃的地步。就在下方,您将找到 GUI 类,然后是小工具超类和移动子类。以下是在 ArrayList 中创建、存储和显示对象的方法    public void addMobile()    {        Mobile mobile = new Mobile(myCredit, theModel, theSize, theWeight, thePrice);        gadgets.add(mobile);    }    public void addMp3()    {        Gadget mp3 = new MP3(theMemory, theModel, theSize, theWeight, thePrice);        gadgets.add(mp3);    }    public void displayAll()    {        for(Gadget gadget : gadgets) {            gadget.print();            System.out.println();   // empty line between items        }    }    public void makeCall()    {        //Nothing here atm    }    public void downloadMusic()    {        //Nothing here atm    }    public void clear()    {        modelTextField.setText("");        sizeTextField.setText("");        phoneNumberTextField.setText("");        weightTextField.setText("");        priceTextField.setText("");        initialCreditTextField.setText("");        initialMemoryTextField.setText("");        durationTextField.setText("");        downloadSizeTextField.setText("");        displayNumberTextField.setText("");    }}
查看完整描述

1 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

您在代码中犯了一个重大错误。根据您的代码 addMobile 方法必须是


public void addMobile()

{

    Mobile mobile = new Mobile(getCredit(), getModel(), getSize(), getWeight(), getPrice());

    gadgets.add(mobile);

}

的值myCredit,theModel,theSize,theWeight,thePrice,theMemory永远不会在代码中的任何位置分配。已经编写了各种 get 方法来从 TextField 中获取值,但从未调用过这些方法。


方法也是如此addMP3。确保使用正确的值调用 Mobile 和 MP3 的构造函数。您的其余代码没有任何问题。


查看完整回答
反对 回复 2022-10-12
  • 1 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号