String model;
int year;
enum Color {GREEN, BLUE, RED};
double price; 色调;
public Car(String model, int year, Color shade, double price) {
this.model = model;
this.year = year;
this.shade= shade;
this.price = price;
}这样可以吗?当我实际使用main方法创建对象时,仍然会给出错误。
发布于 2017-11-25 20:51:02
此语法:this.Color = shade;在Car类中引用一个名为Color的实例字段。但是在Car类中没有任何Color字段。
这一点:
enum Color {GREEN, BLUE, RED};是枚举类声明。
只需在Car中引入一个字段,即可为其分配一个Color:
public class Car {
String model;
int year;
Color color;
...
public Car(String model, int year, Color shade, double price) {
this.model = model;
this.year = year;
this.color = shade;
this.price = price;
}
}发布于 2017-11-25 21:15:08
enum Color {GREEN, BLUE, RED} ;
public class Car{
String m_model;
int m_year;
Color m_color;
double m_price;
public Car(String model, int year, Color shade, double price) {
this.m_model = model;
this.m_year = year;
this.m_color = shade;
this.m_price = price;
System.out.println("A new Car has been created!");
}
static public void main(String[] args)
{
Car car = new Car("Ferrari", 2017, Color.RED, 350000);
}
}https://stackoverflow.com/questions/47486100
复制相似问题