简体   繁体   English

在Java中的HashMap中调用对象的方法

[英]Calling a method of an Object in a HashMap in Java

I have 2 classes: truck and sedan. 我有2节课:卡车和轿车。 When some action occurs, I want to add a sedan to the hashMap in Truck class. 发生某些操作时,我想将轿车添加到Truck类的hashMap中。 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. 我不希望Truck和Sedan彼此了解,所以我在CarManager中创建了Sedan可以调用的方法,该方法传递了轿车ID和想要添加到的Truck ID。 Then CarManager will inform Truck that a Sedan wants to be added to the list. 然后,CarManager将通知Truck轿车要添加到列表中。 The issue is I don't know how CarManager will inform the Truck and what I should have in that addSedan method. 问题是我不知道CarManager将如何通知卡车以及该addSedan方法中应包含的内容。 I do have a HashMap in CarManager that has a collection of CarEntities. 我在CarManager中确实有一个HashMap,它具有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. CarManager无法访问Truck的addCar方法,因为它不在界面中,并且我不想在界面中添加它,因为并非所有CarEntity都会使用它。 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");
}

If you may not use casts and instanceof and must use polymorphism, then add two methods to your CarEntity interface : 如果您可能不使用强制类型转换和instanceof,而必须使用多态性,则在CarEntity接口中添加两个方法:

boolean canBeLoadedWithCars();
void addCar(CarEntity c) throws IllegalStateException;

The trucks can be loaded with cars, and thus implement the first methof by returning true. 卡车可以装满汽车,并通过返回true来实现第一个方法。 The other ones return false. 其他的返回false。

The addCar method of Truck adds the car to their map, whereas the other implementations throw an IllegalStateException because they can't be loaded with cars. TruckaddCar方法将Truck添加到地图中,而其他实现则抛出IllegalStateException,因为它们无法加载汽车。

So the addCar method of the manager becomes 因此,管理器的addCar方法变为

CarEntity truck = hashMap.get(truckId);
if (truck.canBeLoadedWithCars() {
    truck.addCar(sedan);
}

I guess one thing that you can do is 我想你可以做的一件事是

CarEntity t = hash.get(truckId); 
if (t instanceof Truck)
   downcast car entity to truck
   call add car method

The answer depends on who is doing the action. 答案取决于谁在采取行动。 If the Sedan s add themselves to the truck then you should have an addTruck method which adds all of the trucks into the manager. 如果Sedan将自己添加到卡车中,则您应该具有addTruck方法,该方法会将所有卡车添加到管理器中。 The manager would store the Truck s in a Map . 经理将Truck存储在Map

private Map<String, Truck> trucks = new HashMap<String, Truck>();
public void registerTruck(Truck truck) {
    trucks.put(truck.getId(), truck);
}

Then the addCar() method on the manager would do: 然后,管理器上的addCar()方法将执行以下操作:

public void addCar(String truckId, CarEntity car) {
    Truck truck = trucks.get(truckId);
    // null handling needed here
    truck.addCar(car);
}

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: 如果您需要同时输入字符串ID,则需要同时注册汽车和卡车,并执行类似的操作:

private Map<String, Truck> trucks = new HashMap<String, Truck>();
private Map<String, Sedan> sedans = new HashMap<String, Sedan>();

public void registerTruck(Truck truck) {
    trucks.put(truck.getId(), truck);
}
public void registerSedan(Sedan sedan) {
    sedans.put(sedan.getId(), sedan);
}

public void addSedan(String sedanId, String truckId) {
    Sedan sedan = sedans.get(sedanId);
    Truck truck = trucks.get(truckId);
    // null handling needed here
    truck.addCar(sedan);
}

Typically we use Java interfaces to accomplish the decoupling. 通常,我们使用Java接口来完成去耦。 The Truck class should be able to add a CarEntity to its load without knowing that it is a Sedan . Truck类应该能够在其负载中添加CarEntity而不知道它是Sedan In this case an addCar(CarEntity car) method on Truck sounds fine. 在这种情况下, Truck上的addCar(CarEntity car)方法听起来不错。 The Sedan will never know it is on a Truck and all the truck knows are the methods exposed through the CarEntity interface. Sedan永远不会知道它在Truck并且所有卡车都知道这是通过CarEntity接口公开的方法。 In this case maybe the manager goes away. 在这种情况下,经理可能会离开。

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

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