Java 如何将(对象引用或对象类型)用作Hashmap中的值?
我正在进行一项编程挑战,问题如下:Java 如何将(对象引用或对象类型)用作Hashmap中的值?,java,Java,我正在进行一项编程挑战,问题如下: 有一个“Vehicle”对象作为“Car”、“Motorcycle”、“Bus”对象的父类,每个子对象都有一个char类型的代码(id) 有一个“车库”对象,应该用车辆填充 对象以数组列表的形式,将其代码作为字符串传递给garage构造函数。 例如,以下声明创建了一个包含2辆摩托车、一辆汽车和一辆公共汽车的车库: 其中摩托车代码为“M”,汽车代码为“C”,公交车代码为“B” 所以我创建了一个实用程序类,我称之为“Garage Service”,它将负责初始
- 有一个“Vehicle”对象作为“Car”、“Motorcycle”、“Bus”对象的父类,每个子对象都有一个char类型的代码(id)
- 有一个“车库”对象,应该用车辆填充 对象以数组列表的形式,将其代码作为字符串传递给garage构造函数。 例如,以下声明创建了一个包含2辆摩托车、一辆汽车和一辆公共汽车的车库:
我希望我没有详细描述这个问题,但如果有人能提供一些帮助,我将不胜感激。要做您想做的事情,您需要一个类的映射(而不是实例),即
映射。地图中的实际值将是特定类型的类别(汽车
,摩托车
…)。然后您可以执行类似于Vehicle c=map[theType].newInstance()的操作代码>注意,这是我脑子里想不出来的,名字可能有点不对劲:-)
我仍然会使用一个简单的工厂模式(记住,现在你可以打开字符串)要做你想做的事情,你需要一个到类(而不是实例)的映射,即映射。地图中的实际值将是特定类型的类别(汽车
,摩托车
…)。然后您可以执行类似于Vehicle c=map[theType].newInstance()的操作代码>注意,这是我脑子里想不出来的,名字可能有点不对劲:-)
我仍然会使用一个简单的工厂模式(记住,现在你可以打开字符串).也许这就是你想要的:
import java.util.*;
public class VehicleGarageSample {
interface Vehicle { }
static class Bus implements Vehicle {}
static class Car implements Vehicle {}
static class Motorcycle implements Vehicle {}
//////////////////////////////////////////////////////////////
static class Garage {
private static Map<Character, Class<? extends Vehicle>> vehicleTypeReg = new HashMap<>();
public static void registerVehicleType(Character c, Class<? extends Vehicle> type) {
vehicleTypeReg.put(c, type);
}
//////////////////////////////////////////////////////////////
private Map<Class<? extends Vehicle>, List<Vehicle>> vehicleSlots = new HashMap<>();
private Map<Class<? extends Vehicle>, Integer> vehicleLimits = new HashMap<>();
public Garage(String chars) {
for (char c : chars.toCharArray()) {
Class<? extends Vehicle> vType = vehicleTypeReg.get(c);
if (vType == null) {
throw new IllegalArgumentException("Unknown vehicle type '" + c + "'");
}
// Initialize vehicleSlots
vehicleSlots.computeIfAbsent(vType, k -> new ArrayList<>());
// Initialize vehicleLimits
if (vehicleLimits.containsKey(vType)) {
vehicleLimits.put(vType, vehicleLimits.get(vType) + 1);
} else {
vehicleLimits.put(vType, 1);
}
}
}
public void parkVehicle(Vehicle v) {
Integer limit = vehicleLimits.getOrDefault(v.getClass(), 0);
int parked = vehicleSlots.getOrDefault(v.getClass(), Collections.emptyList()).size();
if (parked >= limit) {
throw new IllegalStateException("No more space for " + v.getClass().getSimpleName());
}
vehicleSlots.get(v.getClass()).add(v);
}
}
//////////////////////////////////////////////////////////////
// How to use Garage
public static void main(String[] args) {
Garage.registerVehicleType('B', Bus.class);
Garage.registerVehicleType('C', Car.class);
Garage.registerVehicleType('M', Motorcycle.class);
// add more if you want
Garage garage = new Garage("MMBC");
garage.parkVehicle(new Motorcycle());
garage.parkVehicle(new Motorcycle());
garage.parkVehicle(new Bus());
garage.parkVehicle(new Car());
garage.parkVehicle(new Car()); // this fails
}
}
import java.util.*;
公共类车辆样本{
接口车辆{}
静态类总线实现车辆{}
静态类Car实现车辆{}
静态类摩托车执行车辆{}
//////////////////////////////////////////////////////////////
静态车库{
私有静态映射也许这就是您想要的:
import java.util.*;
public class VehicleGarageSample {
interface Vehicle { }
static class Bus implements Vehicle {}
static class Car implements Vehicle {}
static class Motorcycle implements Vehicle {}
//////////////////////////////////////////////////////////////
static class Garage {
private static Map<Character, Class<? extends Vehicle>> vehicleTypeReg = new HashMap<>();
public static void registerVehicleType(Character c, Class<? extends Vehicle> type) {
vehicleTypeReg.put(c, type);
}
//////////////////////////////////////////////////////////////
private Map<Class<? extends Vehicle>, List<Vehicle>> vehicleSlots = new HashMap<>();
private Map<Class<? extends Vehicle>, Integer> vehicleLimits = new HashMap<>();
public Garage(String chars) {
for (char c : chars.toCharArray()) {
Class<? extends Vehicle> vType = vehicleTypeReg.get(c);
if (vType == null) {
throw new IllegalArgumentException("Unknown vehicle type '" + c + "'");
}
// Initialize vehicleSlots
vehicleSlots.computeIfAbsent(vType, k -> new ArrayList<>());
// Initialize vehicleLimits
if (vehicleLimits.containsKey(vType)) {
vehicleLimits.put(vType, vehicleLimits.get(vType) + 1);
} else {
vehicleLimits.put(vType, 1);
}
}
}
public void parkVehicle(Vehicle v) {
Integer limit = vehicleLimits.getOrDefault(v.getClass(), 0);
int parked = vehicleSlots.getOrDefault(v.getClass(), Collections.emptyList()).size();
if (parked >= limit) {
throw new IllegalStateException("No more space for " + v.getClass().getSimpleName());
}
vehicleSlots.get(v.getClass()).add(v);
}
}
//////////////////////////////////////////////////////////////
// How to use Garage
public static void main(String[] args) {
Garage.registerVehicleType('B', Bus.class);
Garage.registerVehicleType('C', Car.class);
Garage.registerVehicleType('M', Motorcycle.class);
// add more if you want
Garage garage = new Garage("MMBC");
garage.parkVehicle(new Motorcycle());
garage.parkVehicle(new Motorcycle());
garage.parkVehicle(new Bus());
garage.parkVehicle(new Car());
garage.parkVehicle(new Car()); // this fails
}
}
import java.util.*;
公共类车辆样本{
接口车辆{}
静态类总线实现车辆{}
静态类Car实现车辆{}
静态类摩托车执行车辆{}
//////////////////////////////////////////////////////////////
静态车库{
私有静态映射应该可以。听起来太复杂了。使用工厂模式;…
@John3136实际上,问题中还有更多的车辆,为了简化起见,我刚刚将它们设为3个,再加上使用“如果”检查次数如此之多的代码不会给我一个好的评分!为什么会有字符串?为什么不新建车库(bike1、bike2、car、bus)
(直接输入所有车辆实例?@Thilo也许这是填写车库对象的快捷方式Map Map=new HashMap()
应该可以。听起来太复杂了。使用工厂模式if(ch==“M”)还新摩托车();if(ch==“C”)还新汽车();…
@John3136实际上,问题中还有更多的车辆,为了简化起见,我刚刚将它们设为3个,再加上使用“如果”检查次数如此之多的代码不会给我一个好的评分!为什么会有字符串?为什么不新建车库(bike1、bike2、car、bus)
(直接输入所有车辆实例?@Thilo也许这是填写车库对象的一种快速方法这帮助我解决了问题,但是事实证明,我们不能像使用对象引用那样使用父类作为子类的引用,所以我必须定义我的map-like map或简单的map这帮助我解决了问题,呵呵不管怎样,我们不能像使用对象引用那样使用父类作为子类的引用,所以我必须定义我的map-like map或简单地映射你编写的garage构造函数非常有用,非常感谢。你编写的garage构造函数非常有用,非常感谢。