在Java中调用HashMap中对象的方法
我有两类:卡车和轿车。当发生某些操作时,我想将一辆轿车添加到 Truck 类的 hashMap 中。该地图显示了卡车货物中目前有哪些轿车。我不希望 Truck 和 Sedan 彼此了解,因此我在 CarManager 中创建了一个 Sedan 可以调用的方法,该方法传递轿车的 id 以及 Sedan 想要添加到的卡车的 id。然后 CarManager 将通知 Truck 有一辆轿车想要添加到列表中。问题是我不知道 CarManager 将如何通知卡车以及我应该在 addSedan 方法中拥有什么。我在 CarManager 中有一个 HashMap,其中包含 CarEntities 的集合。 Truck 的 addCar 方法无法被 CarManager 访问,因为它不在界面中,而且我不想将其添加到界面中,因为并非所有 CarEntity 都会使用它。有人可以帮忙吗?
public interface CarEntity {
String getId();
double getSpeed();
void move();
}
public class CarManager {
private HashMap<String, CarEntity> hash = new HashMap<String, CarEntity>();
public void addSedan(String carId, String truckId) {
???
hash.get(truckId).addCarr(carId); //I don't think this will work
}
}
public class Truck implements CarEntity {
private HashMap<String, CarEntity> cargo = new HashMap<String, CarEntity>();
public void addCar(String id, CarEntity ce) {
cargo.put(id,ce);
}
public class Sedan implements CarEntity {
CarManager.addSedan("Car 1", "Truck 5");
}
I have 2 classes: truck and sedan. When some action occurs, I want to add a sedan to the hashMap in Truck class. The map tells what Sedans are currently in the Truck's cargo. I don't want Truck and Sedan to know about each other so I have made a method in CarManager that Sedan can call which passes the id of the sedan and the id of the Truck that Sedan wants to be added to. Then CarManager will inform Truck that a Sedan wants to be added to the list. The issue is I don't know how CarManager will inform the Truck and what I should have in that addSedan method. I do have a HashMap in CarManager that has a collection of CarEntities. The Truck's addCar method cannot be accessed by CarManager since it isnt in the interface and I dont want to add it in the interface because not all CarEntity will use it. Can anyone help?
public interface CarEntity {
String getId();
double getSpeed();
void move();
}
public class CarManager {
private HashMap<String, CarEntity> hash = new HashMap<String, CarEntity>();
public void addSedan(String carId, String truckId) {
???
hash.get(truckId).addCarr(carId); //I don't think this will work
}
}
public class Truck implements CarEntity {
private HashMap<String, CarEntity> cargo = new HashMap<String, CarEntity>();
public void addCar(String id, CarEntity ce) {
cargo.put(id,ce);
}
public class Sedan implements CarEntity {
CarManager.addSedan("Car 1", "Truck 5");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可能不使用强制转换和instanceof而必须使用多态性,那么请在您的CarEntity接口中添加两个方法:
卡车可以装载汽车,从而通过返回true来实现第一个方法。其他的返回 false。
Truck
的addCar
方法将汽车添加到其地图中,而其他实现则抛出 IllegalStateException,因为它们无法装载汽车。所以管理器的
addCar
方法就变成了If you may not use casts and instanceof and must use polymorphism, then add two methods to your CarEntity interface :
The trucks can be loaded with cars, and thus implement the first methof by returning true. The other ones return false.
The
addCar
method ofTruck
adds the car to their map, whereas the other implementations throw an IllegalStateException because they can't be loaded with cars.So the
addCar
method of the manager becomes我想你能做的一件事就是
I guess one thing that you can do is
答案取决于谁在执行该操作。如果
Sedan
将自己添加到卡车中,那么您应该有一个addTruck
方法,将所有卡车添加到管理器中。经理将Truck
存储在Map
中。那么管理器上的
addCar()
方法就会执行以下操作:如果卡车接收汽车,那么您可以注册这些汽车。如果您需要通过字符串 id 来获得两者,那么您需要注册汽车和卡车,并执行以下操作:
通常我们使用 Java 接口来完成解耦。
Truck
类应该能够将CarEntity
添加到其负载中,而无需知道它是Sedan
。在这种情况下,Truck
上的addCar(CarEntity car)
方法听起来不错。Sedan
永远不会知道它在Truck
上,卡车知道的只是通过CarEntity
接口公开的方法。在这种情况下,经理可能会离开。The answer depends on who is doing the action. If the
Sedan
s add themselves to the truck then you should have anaddTruck
method which adds all of the trucks into the manager. The manager would store theTruck
s in aMap
.Then the
addCar()
method on the manager would do:If, instead, the trucks take the cars then you could register the cars instead. If you need to have both be by string id then you'll need to register both cars and trucks and do something like:
Typically we use Java interfaces to accomplish the decoupling. The
Truck
class should be able to add aCarEntity
to its load without knowing that it is aSedan
. In this case anaddCar(CarEntity car)
method onTruck
sounds fine. TheSedan
will never know it is on aTruck
and all the truck knows are the methods exposed through theCarEntity
interface. In this case maybe the manager goes away.