JAVA - 使用列表重构多个 "instanceof"

JAVA - Refactoring multiple "instanceof" using a List

目前我正在检查对象是否是 X、Y、Z 的实例并应用一些方法。 (这些只是插图:)

if (X instanceof Car || X instaceof Bus || ...) {
   X.color = RED;
}

但是,如果我要比较 300 个对象(例如:汽车、公共汽车、自行车、火车、飞机……),我将如何分解它?我想做一个列表或一个数组,但初始化它似乎有点长:

Car car = new Car();
Bus bus = new Bus();
...
List<Transportation> list_t = Arrays.asList(car, bus, ...);
for (Transportation t : list_t) {
    if (X instance of t)
         X.color = RED;
}

非常感谢任何建议,谢谢。

我要么使用通用的父接口。

class Car implements DisplayColour {
    public Color displayColor() {
          return Color.RED;
    }

或者如果您知道所有可能的情况,我会使用 HashMap 类。

static final Map<Class, Color> classToColorMap = new HashMap<>(); 
static {
    classToColorMap.put(Car.class, Color.RED);
}