简体   繁体   English

Java集合和对象分配

[英]Java collections and objects assignment

I have Floor class and it has following methods. 我有Floor类,它具有以下方法。

Passenger class has a constructor that sets current floor of residence to 1. 旅客舱位有一个构造函数,用于将当前居住楼层设置为1。

Following line adds passengers to the floor. 下一行将乘客添加到地板上。

This is where I am struggling. 这就是我在努力的地方。 Both of the following statements return true when I never loaded passenger on floor3. 当我从未将乘客装载在floor3上时,以下两个语句都返回true。

Again, thank you for all the help. 再次感谢您的所有帮助。

You only ever do anything with the ground floor... I suspect there are other problems in the code as well. 您只能在底层做任何事情……我怀疑代码中还有其他问题。

public boolean isResident(Passenger passenger) {
  int floor = building.getFloor();
  return building.floor(0).resident.contains(passenger);
}

This method calls builder.getFloor() , then proceeds to completely disregard the output, and check if the ground floor contains the passenger. 此方法调用builder.getFloor() ,然后继续完全忽略输出,并检查一楼是否包含乘客。

public void enter(Passenger passenger) {     
  Floor.enterGroundFloor(passenger);    
}

This method, also, summarily puts passengers on the ground floor. 这种方法也将乘客放在地面上。

So: 所以:

Building.floor(1).enter(p1) 

Building.floor(1).isResident(p1) --> true    
Building.floor(3).isResident(p1) --> true   

This is natural because you are putting p1 on the ground floor, and then checking if he is on the ground floor twice. 这很自然,因为您将p1放在一楼,然后检查了他是否在一楼。

You appear to have some fundamental misconceptions about what static means, and how object hierarchy should be structured. 您似乎对static含义以及应该如何构造对象层次结构有一些基本的误解。 If you link the rest of your code, I am sure we can give you additional advice. 如果您链接其余代码,我相信我们可以为您提供其他建议。

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

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