gpt4 book ai didi

java - 如何从枚举调用?

转载 作者:行者123 更新时间:2023-12-01 18:10:21 27 4
gpt4 key购买 nike

我有一个用于枚举的java代码。

public enum TruckType{
LIGHT, MEDIUM, HEAVY, SUPER_HEAVY, TRAILER;
}

现在我正在尝试从枚举中获取 MEDIUM。

public static void main (String[] args) {
Truck truck1 = new Truck("John Brown","9764780493","GJ67745676","Blue", 2400000.0,2019,18,TruckType.MEDIUM);
}

这就是我所做的,但它不起作用。我该怎么做才能从枚举中获得 MEDIUM ?我得到一个空值返回。

这是我的卡车类与 TruckTypes

public class Truck extends Vehicle{
private int wheelNo;
public TruckType classification;

public Truck(String ownername, String telephonenumber, String chassisNum, String color, double estimatedVal, int year,
int wheelNo,TruckType Classification)
{
super(ownername, telephonenumber,chassisNum, color, estimatedVal, year);
this.wheelNo = wheelNo;
this.classification = classification;

最佳答案

您的问题在于此处的构造函数参数:TruckType Classification

public Truck(String ownername, String telephonenumber, String chassisNum, String color, double estimatedVal, int year,
int wheelNo, TruckType Classification) {
super(ownername, telephonenumber,chassisNum, color, estimatedVal, year);
this.wheelNo = wheelNo;
this.classification = classification;

由于参数的大写,Java 会看到:

    this.classification = classification;

实际上是这样的:

    // remains null
this.classification = this.classification;

解决方案是检查拼写/大写并将 TruckType Classification 更改为 TruckType Classification

关于java - 如何从枚举调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60478813/

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