我有代码:
public enum Type {
A,
B,
C,
D
}
我使用来自龙目岛的@Data提供公共获取器和设置器。
@Data
public class Animal {
/**
* The type of Animal.
*/
private Type type;
}
@Data
public class Cow extends Animal {
/**
* The type of Animal.
*/
private Type type = Type.A;
}
@Data
public class Fish extends Animal {
/**
* The type of Animal.
*/
private Type type = Type.B;
}
@Data
public class Pig extends Animal {
/**
* The type of Animal.
*/
private Type type = Type.C;
}
@Data
public class Chicken extends Animal {
/**
* The type of Animal.
*/
private Type type = Type.D;
}
还有另一个类中的静态方法:
public static Meat convertAnimalToMeat(
@NonNull final Animal animal) {
switch (animal.getType()) {
case A:
return cookCow((Cow) animal);
case B:
return cookFish((Fish) animal);
case C:
return cookPig((Pig) animal);
case D:
return cookChicken((Chicken) animal);
default:
throw new IllegalArgumentException("Unsupported type " + animal.getType());
}
}
参数animal是Animal的子类对象。
当我将鼠标移到IntelliJ中的A,B,C和D时,它将显示我要检查的正确的Enum类型。与将鼠标移至animal.getType()时相同。
那为什么我为什么不能在animal.getType()以及A和B以及C和D上找到符号?