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

如何在不修改权限的情况下操作类中的私有Journey对象

如何在不修改私有成员权限的前提下给SmartCard添加Journey

现有类定义

Journey类

public class Journey
{
    private int journeyID;                 // 旅程ID
    private String transportMode;          // 交通方式(仅支持"train"、"bus"或"tram")
    private int startOfJourney;            // 起始站点(仅允许1-10之间的数字)
    private int endOfJourney;              // 结束站点(仅允许1-10之间的数字,且需与起始站点不同)
    private int distanceOfJourney; 
}

SmartCard类

public class SmartCard
{
    private int cardID;           // 智能卡ID
    private char type;            // 卡类型(仅支持"C"、"A"或"S")
    private float balance;       // 卡内余额(最低需为$5)
    private Journey journey1 = null;             // 旅程对象
    private Journey journey2 = null;             // 旅程对象 - 仅"A"或"S"类型卡可使用
    private Journey journey3 = null;
}

问题描述

需要给已有的SmartCard对象添加新的Journey,但不能将journey1等私有成员改为public,且不允许使用ArrayList。之前尝试直接访问私有成员smartCard1.journey1 == null,因权限问题失败;尝试通过smartCard1.journey1.getJourneyID()==0访问,也因无法获取私有成员journey1而失败。

可行解决方案

1. 在SmartCard类内部实现添加旅程的方法

私有成员只能在类内部访问,因此直接在SmartCard中封装添加逻辑,外部只需调用方法即可:

public class SmartCard {
    // 原有成员变量...

    public boolean addJourney(Journey newJourney) {
        // 根据卡类型判断可添加的旅程数量
        if (type == 'C') {
            if (journey1 == null) {
                journey1 = newJourney;
                return true;
            } else {
                System.out.println("普通卡(C)最多只能添加1段旅程");
                return false;
            }
        } else if (type == 'A' || type == 'S') {
            if (journey1 == null) {
                journey1 = newJourney;
            } else if (journey2 == null) {
                journey2 = newJourney;
            } else if (journey3 == null) {
                journey3 = newJourney;
            } else {
                System.out.println("该卡已达到最大旅程数上限");
                return false;
            }
            return true;
        } else {
            System.out.println("无效的卡类型");
            return false;
        }
    }
}

外部调用示例:

SmartCard smartCard1 = new SmartCard();
// 假设已初始化smartCard1的类型等属性
Journey newJourney = new Journey();
// 初始化newJourney的属性
smartCard1.addJourney(newJourney);

2. 提供获取/设置指定旅程的方法

给SmartCard添加针对指定索引旅程的get/set方法,内部做权限和合法性校验:

public class SmartCard {
    // 原有成员变量...

    public Journey getJourney(int index) {
        switch(index) {
            case 1:
                return journey1;
            case 2:
                if (type == 'A' || type == 'S') return journey2;
                else {
                    System.out.println("普通卡(C)无法访问第2段旅程");
                    return null;
                }
            case 3:
                if (type == 'A' || type == 'S') return journey3;
                else {
                    System.out.println("普通卡(C)无法访问第3段旅程");
                    return null;
                }
            default:
                System.out.println("无效的旅程索引(仅支持1/2/3)");
                return null;
        }
    }

    public boolean setJourney(int index, Journey journey) {
        switch(index) {
            case 1:
                journey1 = journey;
                return true;
            case 2:
                if (type == 'A' || type == 'S') {
                    journey2 = journey;
                    return true;
                } else {
                    System.out.println("普通卡(C)无法设置第2段旅程");
                    return false;
                }
            case 3:
                if (type == 'A' || type == 'S') {
                    journey3 = journey;
                    return true;
                } else {
                    System.out.println("普通卡(C)无法设置第3段旅程");
                    return false;
                }
            default:
                System.out.println("无效的旅程索引(仅支持1/2/3)");
                return false;
        }
    }
}

外部调用示例:

if (smartCard1.getJourney(1) == null) {
    smartCard1.setJourney(1, newJourney);
}

3. 封装创建并添加旅程的完整逻辑

如果不想外部处理Journey对象的初始化,可以在SmartCard中直接提供创建旅程的方法,内部完成Journey实例化和添加:

public class SmartCard {
    // 原有成员变量...

    public boolean createAndAddJourney(int journeyID, String transportMode, int start, int end, int distance) {
        // 先校验Journey参数合法性
        if (!transportMode.equals("train") && !transportMode.equals("bus") && !transportMode.equals("tram")) {
            System.out.println("无效的交通方式,仅支持train/bus/tram");
            return false;
        }
        if (start < 1 || start > 10 || end < 1 || end > 10 || start == end) {
            System.out.println("无效的起止站点,需为1-10之间的不同数字");
            return false;
        }

        // 注意:Journey类需要添加带参构造方法,或者setter方法来初始化私有成员
        Journey newJourney = new Journey(journeyID, transportMode, start, end, distance);
        
        // 调用之前的addJourney方法完成添加
        return addJourney(newJourney);
    }
}

对应的Journey类需补充构造方法:

public class Journey {
    // 原有私有成员...

    public Journey(int journeyID, String transportMode, int startOfJourney, int endOfJourney, int distanceOfJourney) {
        this.journeyID = journeyID;
        this.transportMode = transportMode;
        this.startOfJourney = startOfJourney;
        this.endOfJourney = endOfJourney;
        this.distanceOfJourney = distanceOfJourney;
    }
}

外部调用示例:

smartCard1.createAndAddJourney(1001, "bus", 3, 7, 15);

补充说明

你之前的尝试失败原因很明确:

  • 直接访问smartCard1.journey1:journey1是SmartCard的私有成员,外部类没有访问权限;
  • 调用smartCard1.journey1.getJourneyID():连journey1这个私有成员都无法获取,自然无法调用它的方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:22:54