我有一个枚举的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;发布于 2020-03-02 03:29:11
您的问题在于这里的构造函数参数: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
发布于 2020-03-02 03:38:28
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;
---
---
}在这里,this.classification = classification;是错的,应该是this.classification = Classification;
你能帮我查一下吗。
https://stackoverflow.com/questions/60478813
复制相似问题