简体   繁体   English

如何在Java中将在一个类中创建的对象传递给另一个类?

[英]How do I pass an object created in one class to another in java?

I'm trying to develop an online hotel booking system. 我正在尝试开发在线酒店预订系统。 I have the main class which takes input from the user such as their name, their payment information, and other data fields and makes a Reservation object using that information. 我有一个主类,它从用户那里获取输入信息,例如他们的姓名,他们的付款信息和其他数据字段,并使用该信息创建一个Reservation对象。 I have another class called Room that has a list of Reservations for each Room object. 我还有一个名为Room类,该类具有每个Room对象的Reservations列表。 The problem I am having is I can't figure out a way to add the Reservation object into the list in the Room object. 我遇到的问题是我想不出一种方法来将Reservation对象添加到Room对象的列表中。 Here is some of the code: 这是一些代码:

public class HotelReservationSystem
{
    private Reservation reservation;

    public void makeReservation(int checkIn, int checkOut)//Other parameters
    {
        reservation = new Reservation(checkIn, checkOut);
    }
}

public class Room
{
    private ArrayList<Reservation> reservations;

    public void addReservation(//parameters?)
    {
        reservations.add(//parameter?);
    }
}

I don't know how to get the new Reservation object to be passed as a parameter for the add method in the Room class. 我不知道如何获取新的Reservation对象作为Room类中add方法的参数来传递。 I just can't wrap my head around it and was hoping for someone to help jog my thinking process. 我只是束手无策,希望有人能帮助我慢跑。

Thanks for your help. 谢谢你的帮助。

Let makeReservation return the created Reservation object: 让makeReservation返回创建的Reservation对象:

 public Reservation makeReservation(int checkIn, int checkOut)//Other parameters
{
    reservation = new Reservation(checkIn, checkOut);
    return reservation;
}

(You could also create a getter for reservation ) (您也可以创建用于reservation的吸气剂)

Then change your addReservation like this: 然后像这样更改您的addReservation:

public void addReservation(Reservation res)
{
    reservations.add(res);
}

And then just add it like this: 然后像这样添加它:

HotelReservationSystem hrs = new HotelReservationSystem();
Reservation res = hrs.makeReservation();
Room room = new Room();
room.addReservation(res);

However, you might want to rethink your model. 但是,您可能需要重新考虑模型。 Right now your HotelReservationSystem is creating a reservation and only saves that one, overwriting old ones. 现在,您的HotelReservationSystem正在创建预订,并且仅保存该预订,并覆盖旧预订。 What happens if you create more than one? 如果创建多个对象会怎样? Also how can you get the reservations for a certain room given the HotelReservationSystem object? 另外,给定HotelReservationSystem对象,如何获得特定房间的预订? Just some things to think about... 只是要考虑的一些事情...

I believe you must have tried this 我相信您一定已经尝试过

public void addReservation(Reservation reservation)
{
    reservations.add(reservation);
}

but the problem here is that your list reservations is null and will throw null pointer exception. 但是这里的问题是您的列表reservations为空,并且将抛出空指针异常。 So better initialize it at declaration. 因此最好在声明时将其初始化。 So change this 所以改变这个

private ArrayList<Reservation> reservations;

to

private ArrayList<Reservation> reservations = new ArrayList<Reservation>();

And in your makeReservation method of Hotel class do this: 然后在Hotel类的makeReservation方法中执行以下操作:

Room room = new Room();
room.addReservation(reservation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM