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

发布于 2024-12-11 16:37:39 字数 1081 浏览 0 评论 0原文

我有两类:卡车和轿车。当发生某些操作时,我想将一辆轿车添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-12-18 16:37:39

如果您可能不使用强制转换和instanceof而必须使用多态性,那么请在您的CarEntity接口中添加两个方法:

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

卡车可以装载汽车,从而通过返回true来实现第一个方法。其他的返回 false。

TruckaddCar 方法将汽车添加到其地图中,而其他实现则抛出 IllegalStateException,因为它们无法装载汽车。

所以管理器的 addCar 方法就变成了

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

If you may not use casts and instanceof and must use polymorphism, then add two methods to your CarEntity interface :

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

The trucks can be loaded with cars, and thus implement the first methof by returning true. The other ones return 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.

So the addCar method of the manager becomes

CarEntity truck = hashMap.get(truckId);
if (truck.canBeLoadedWithCars() {
    truck.addCar(sedan);
}
_蜘蛛 2024-12-18 16:37:39

我想你能做的一件事就是

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

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
说好的呢 2024-12-18 16:37:39

答案取决于谁在执行该操作。如果 Sedan 将自己添加到卡车中,那么您应该有一个 addTruck 方法,将所有卡车添加到管理器中。经理将Truck存储在Map中。

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

那么管理器上的 addCar() 方法就会执行以下操作:

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

如果卡车接收汽车,那么您可以注册这些汽车。如果您需要通过字符串 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);
}

通常我们使用 Java 接口来完成解耦。 Truck 类应该能够将 CarEntity 添加到其负载中,而无需知道它是 Sedan。在这种情况下,Truck 上的 addCar(CarEntity car) 方法听起来不错。 Sedan 永远不会知道它在 Truck 上,卡车知道的只是通过 CarEntity 接口公开的方法。在这种情况下,经理可能会离开。

The answer depends on who is doing the action. If the Sedans add themselves to the truck then you should have an addTruck method which adds all of the trucks into the manager. The manager would store the Trucks in a 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:

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:

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. The Truck class should be able to add a CarEntity to its load without knowing that it is a Sedan. In this case an addCar(CarEntity car) method on Truck sounds fine. The Sedan will never know it is on a Truck and all the truck knows are the methods exposed through the CarEntity interface. In this case maybe the manager goes away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文