简体   繁体   English

如何将Java中不同类的方法链接在一起?

[英]How would I link together methods from different classes in java?

This is the code for one of the methods I wrote: 这是我编写的方法之一的代码:

public void createReservation (String guestName, String roomType) {
  Reservation roomReservation;
  Room roomAvailability;
  roomAvailability = findAvailableRoom(hotelRooms, roomType);
  if(roomAvailability != null) {
  roomReservation = new Reservation(roomAvailability, guestName);
  }
  else {
    System.out.println("A room of this type is not available.");
  }
}

The error is coming from line 4. The error says that the symbol cannot be found. 该错误来自第4行。该错误表明找不到该符号。 Although I think I know why, I am not entirely sure how to fix the problem. 尽管我认为我知道为什么,但是我不确定如何解决该问题。

findAvailableRoom is a method from a different class and I am trying to transfer it into this class. findAvailableRoom是来自其他类的方法,我正尝试将其转移到此类中。 I thought that writing it like that would suffice but it doesn't seem to have worked. 我认为这样写就足够了,但似乎没有用。

This is the method that I am referring to. 这就是我所指的方法。 It is in a different class. 它是在另一个类别中。

public Room findAvailableRoom (Room [] roomList, String desiredType) {
    for (int i = 0; i < roomList.length; i++) {
      if (roomList[i].getAvailability() == true && roomList[i].getType() == desiredType) {
      return roomList[i];
      }
  }
    return null;
  }

Any help? 有什么帮助吗?

To achieve what you're asking, you need an instance of the object of which field is needed. 为了实现您的要求,您需要一个需要该字段的对象的实例。 Another possibility is to make that field static. 另一种可能性是使该字段静态。

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

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