You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何用子类对象填充CandyBag的ArrayList并实现打印?

修正Java糖果盒ArrayList存储与打印问题

需求回顾

  • 父类CandyBox包含flavor(口味)和origin(产地)属性;
  • 子类Lindt、Baravelli、ChocoAmor额外拥有对应盒子类型的尺寸参数;
  • CandyBag类需通过ArrayList存储各一个上述子类的对象,并实现打印功能。

原代码核心错误

  1. 类继承逻辑错误:CandyBag是容器类,无需继承CandyBox;
  2. 代码执行位置错误:直接在类体中调用add方法,必须放在构造方法/初始化块内;
  3. 大小写拼写错误:Candybox应为CandyBox,ChocAmor应为ChocoAmor;
  4. 方法定义错误:printArray未指定参数类型,遍历类型误用String而非CandyBox;
  5. 方法调用冗余:printArray无需传参,可直接使用类内的ArrayList成员变量。

完整修正代码

1. 父类CandyBox

public class CandyBox {
    protected String flavor;
    protected String origin;

    public CandyBox(String flavor, String origin) {
        this.flavor = flavor;
        this.origin = origin;
    }

    // 重写toString,方便打印对象详情
    @Override
    public String toString() {
        return "口味: " + flavor + ", 产地: " + origin;
    }
}

2. 子类实现

// Lindt子类:参数对应高、宽、长
public class Lindt extends CandyBox {
    private double height;
    private double width;
    private double length;

    public Lindt(double height, double width, double length) {
        super("樱桃味", "奥地利");
        this.height = height;
        this.width = width;
        this.length = length;
    }

    @Override
    public String toString() {
        return super.toString() + ", Lindt盒子尺寸: 高=" + height + ", 宽=" + width + ", 长=" + length;
    }
}

// Baravelli子类:参数对应高、宽
public class Baravelli extends CandyBox {
    private double height;
    private double width;

    public Baravelli(double height, double width) {
        super("葡萄味", "意大利");
        this.height = height;
        this.width = width;
    }

    @Override
    public String toString() {
        return super.toString() + ", Baravelli盒子尺寸: 高=" + height + ", 宽=" + width;
    }
}

// ChocoAmor子类:参数对应宽度
public class ChocoAmor extends CandyBox {
    private double width;

    public ChocoAmor(double width) {
        super("咖啡味", "法国");
        this.width = width;
    }

    @Override
    public String toString() {
        return super.toString() + ", ChocoAmor盒子尺寸: 宽=" + width;
    }
}

3. 容器类CandyBag

import java.util.ArrayList;

public class CandyBag {
    private ArrayList<CandyBox> candyBag;

    // 构造方法初始化并填充ArrayList
    public CandyBag() {
        candyBag = new ArrayList<>();
        candyBag.add(new Lindt(20, 5.4, 19.2));
        candyBag.add(new Baravelli(6.7, 8.7));
        candyBag.add(new ChocoAmor(5.5));
    }

    // 打印方法:遍历输出所有糖果盒信息
    public void printCandyBoxes() {
        for (CandyBox box : candyBag) {
            System.out.println(box);
        }
    }
}

4. 主类测试

public class Main {
    public static void main(String[] args) {
        CandyBag giftBag = new CandyBag();
        giftBag.printCandyBoxes();
    }
}

关键修正说明

  • 调整继承关系:CandyBag作为容器独立存在,不再继承CandyBox;
  • 规范初始化逻辑:将ArrayList的填充操作放在构造方法中,确保对象创建时完成数据加载;
  • 重写toString:每个类自定义打印内容,避免输出默认内存地址;
  • 简化方法调用:printCandyBoxes直接使用类内成员变量,无需额外传参;
  • 修正拼写错误:统一Java类名大小写规范。

内容的提问来源于stack exchange,提问作者Aled

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:35:38