2017-11-25 11 views
0
String model; 
int year; 
enum Color {GREEN, BLUE, RED}; 
double price;  

색조;ENUM 타입을 생성자에 할당

public Car(String model, int year, Color shade, double price) { 

    this.model = model; 
    this.year = year; 
    this.shade= shade; 
    this.price = price; 
} 

괜찮습니까? main 메서드로 객체를 만들 때 여전히 오류가 발생합니다.

+0

아니, 당신은하지 않았다. Thats는 enums가 작동하는 것을 모릅니다. 유형을 선언했지만 인스턴스를 정의하지 않았습니다. – Ivan

+0

이봐, 어떻게 내가 그걸 할 수 있니? –

답변

1

이 구문은 다음과 같습니다. this.Color = shade; Car 클래스의 Color이라는 인스턴스 필드를 참조합니다. 그러나 클래스에는 Color 필드가 없습니다.

이 :

enum Color {GREEN, BLUE, RED}; 

는 열거 클래스 선언이다.

그냥 그것을 Color에 할당 할 수 Car에서 필드를 소개 :

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; 
    } 
} 
+0

아! 문제는 고마워요. 도와 줘서 고맙습니다 :) –

+0

@DannyBorisOv이 답이 문제를 해결하면 답변자에게 보상을주고 미래의 방문객에게 정답을 알려주십시오. –

0
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); 
    } 
}