Работа с приватными объектами в других классахJAVA

Программисты JAVA общаются здесь
Anonymous
Работа с приватными объектами в других классах

Сообщение Anonymous »

Я работаю с этими двумя классами:

Код: Выделить всё

public class Journey
{
private int journeyID;                 // the id of the journey
private String transportMode;          // the public transport mode of the journey (can be only “train”, “bus” or “tram”)
private int startOfJourney;            // the starting point of the journey. It can be only a number between [1..10]
private int endOfJourney;              // the ending point of the journey. It can be only a number between [1..10] (should be different from the starting point of the journey)
private int distanceOfJourney;

Код: Выделить всё

public class SmartCard
{
private int cardID;           // the id of the smartcard
private char type;            // the type of the smartcard (it can be "C", "A" or "S")
private float balance;       // the balance available on the smartcard (should have a minimum balance of $5)
private Journey journey1 = null;             // journey object
private Journey journey2 = null;             // journey object - can only be used if the type of the smartcard is "A" or "S"
private Journey journey3 = null;
и мне нужно написать метод для добавления новых путешествий к одной из существующих смарт-карт. Однако я не могу понять, как получить доступ к объектам travel1, не изменив их на общедоступные. Мне не разрешено использовать ArrayList в этой задаче, могу ли я дать какие-либо предложения о том, как работать с объектами в классах? Спасибо.
Я пытаюсь получить доступ к объекту Journey с помощью

Код: Выделить всё

smartCard1.journey1 == null;
но он заявил, что я не могу получить доступ к частному объекту. Вместо этого я пытаюсь создать метод getJourneyID() и использовать

Код: Выделить всё

smartCard1.journey1.getJourneyID()==0;
но это тоже не сработает.

Подробнее здесь: https://stackoverflow.com/questions/784 ... r-classess

Вернуться в «JAVA»