gpt4 book ai didi

java - Enum 的参数始终设置为 null

转载 作者:行者123 更新时间:2023-12-01 19:29:13 33 4
gpt4 key购买 nike

我有两个类(class),FruitPlantFruit ,并且每个人都有自己的Type枚举:

public class FruitPlant extends Plant {
public static enum Type {
// Dry-hot
PEACH_TREE("peach", 3, 80, Fruit.Type.PEACH),
CACTUS("cactus", 10000, 120, Fruit.Type.PRICKLYPEAR),

// Dry-cool
BLUEBERRY_BUSH("blueberry", 3, 40, Fruit.Type.BLUEBERRY),
CHERRY_TREE("cherry", 3, 60, Fruit.Type.CHERRY),

// Wet-hot
ORANGE_TREE("orange", 2, 80, Fruit.Type.ORANGE),
MELON_VINE("melon", 1.5, 80, Fruit.Type.MELON),

// Wet-cool
APPLE_TREE("apple", 2, 60, Fruit.Type.APPLE),
STRAWBERRY_BUSH("strawberry", 1.5, 60, Fruit.Type.STRAWBERRY);

public double waterHardiness;
public int preferedTemp;
public int[] babyImage;
public int[] childImage;
public int[] subAdultImage;
public int[] adultImage;
public int[] pregnantImage;
public Fruit.Type fruitType;

Type (String imagePath, double waterHardiness, int preferedTemp, Fruit.Type fruitType){
this.waterHardiness = waterHardiness;
this.preferedTemp = preferedTemp;
babyImage = Image.loadImage("/res/plants/" + imagePath + "/baby.png");
childImage = Image.loadImage("/res/plants/" + imagePath + "/child.png");
subAdultImage = Image.loadImage("/res/plants/" + imagePath + "/subadult.png");
adultImage = Image.loadImage("/res/plants/" + imagePath + "/adult.png");
pregnantImage = Image.loadImage("/res/plants/" + imagePath + "/pregnant.png");
this.fruitType = fruitType;
}
}

Type type;
public FruitPlant(Type type) {
this.type = type;
System.out.println(type.fruitType); // Prints whatever Fruit.Type the FruitPlant.Type has
}
}
public class Fruit implements Element, Holdable, Plantable {
public static enum Type {
PRICKLYPEAR(FruitPlant.Type.CACTUS),
PEACH(FruitPlant.Type.PEACH_TREE),
CHERRY(FruitPlant.Type.CHERRY_TREE),
BLUEBERRY(FruitPlant.Type.BLUEBERRY_BUSH),
MELON(FruitPlant.Type.MELON_VINE),
ORANGE(FruitPlant.Type.ORANGE_TREE),
APPLE(FruitPlant.Type.APPLE_TREE),
STRAWBERRY(FruitPlant.Type.STRAWBERRY_BUSH);


public FruitPlant.Type plant;

Type(FruitPlant.Type plant) {
System.out.println(plant);
this.plant = plant;
}
}

Type type;
public Fruit(Type type){
this.type = type;
System.out.println(type.plant); // Prints null
}
}

以及一些所需的测试代码:

public class Main {
public static void Main(String args[]){
System.out.println("Next line printed should be BLUEBERRY:");
FruitPlant blueberryBush = new FruitPlant(FruitPlant.Type.BLUEBERRY_BUSH);
System.out.println("Next line printed should be BLUEBERRY_BUSH:");
Fruit blueberry = new Fruit(Fruit.Type.BLUEBERRY);
System.out.println("End test");

}
}

每当我创建FruitPlant时对象并传递给它 FruitPlant.Type枚举 FruitPlant构造函数,fruit该枚举的字段返回给我 Fruit.Type枚举。然而,每当我制作 Fruit对象并给它一个 Fruit.Type枚举 Fruit构造函数,plant该枚举的字段的计算结果始终为 null .

这与系统的循环性有关吗?为什么 Fruit.Type.plant现场返回nullFruitPlant.Type.fruit返回 Fruit.Type enum

PS:这两个类还有更多内容,但我已经删除了大部分内容,因为它们不相关。我还使用了带有类型枚举的骨架类,因为大多数水果植物和水果的行为相似,并且创建 8 个基本相同的类感觉是错误的。

最佳答案

出现此问题的原因是您有两个枚举,Fruit.TypeFruitPlant.Type,它们都在常量声明中相互引用:

public class FruitPlant extends Plant {
public static enum Type {
// Dry-hot
PEACH_TREE("peach", 3, 80, Fruit.Type.PEACH),
// ...
}
// ...
}

// ...
public class Fruit implements Element, Holdable, Plantable {
public static enum Type {
// ...
PEACH(FruitPlant.Type.PEACH_TREE),
// ...
}
// ...
}

枚举常量 Fruit.Type.PEACHFruitPlant.Type.PEACH_TREE 必须在使用之前实例化。

假设首先实例化PEACH;那么 FruitPlant.Type.PEACH_TREE 尚未实例化,因此该名称的静态成员的默认初始值为 null。它要引用的对象尚不存在,因此无法引用它。另一方面,如果先实例化PEACH_TREE,则它无法获取对PEACH的引用,因为该对象尚不存在;当前引用它的枚举成员的默认初始值为 null

解决方案是延迟这些枚举之间的循环引用,直到所有枚举常量都已初始化;您可以编写方法 getPlantgetFruit 返回正确的值,而不是将它们存储为字段。调用这些方法时,所有枚举常量都将被实例化,因此不会因未初始化的成员而产生默认 null 值。

public class FruitPlant extends Plant {
public static enum Type {
PEACH_TREE("peach", 3, 80),
// ...

public Fruit.Type getFruit() {
switch(this) {
case PEACH_TREE: return Fruit.Type.PEACH;
// ...
default: throw new IllegalStateException();
}
}

// ...
}
// ...
}

关于java - Enum 的参数始终设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59277299/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com