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

Farbton;Konstruktor ENUM-Typ zuweisen

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

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

Ist das in Ordnung? gibt immer noch Fehler, wenn ich das Objekt tatsächlich mit der Hauptmethode mache.

+0

Nein, hast du nicht. Das ist nicht wissen, enums Arbeit. Sie haben einen Typ deklariert, Sie haben keine Instanz definiert. – Ivan

+0

hey, wie kann ich das machen? –

Antwort

1

Diese Syntax: this.Color = shade; verweist auf ein Instanzfeld mit dem Namen Color in der Klasse Car. Aber Sie haben kein Color Feld in der Car Klasse.

Dieses:

enum Color {GREEN, BLUE, RED}; 

ist die Enum-Klassendeklaration.

vorstellen Nur ein Feld in Car der Lage sein, es zuweisen eines 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; 
    } 
} 
+0

Oh, hab's! Problem behoben Vielen Dank für die Hilfe :) –

+0

@DannyBorisOv Wenn diese Antwort Ihr Problem gelöst hat, könnten Sie in Betracht ziehen, es zu akzeptieren, um den Antworter zu belohnen und zukünftige Besucher wissen zu lassen, was die richtige Antwort ist. –

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); 
    } 
} 
Verwandte Themen