Vergleich von Enums

PapaBär

Neues Mitglied
Hallo,

habe ein kleines Problem, vielleicht könnt ihr mir helfen.
Meine Klasse soll in Abhängigkeit von der Button-Stellung alle 2,5 Sek. etwas ausgeben.
Der Vergleich den ich dabei anstelle funktioniert aber nicht, habe leider keine Ahnung wieso.
Der Teil um den es geht, ist rot markiert.

Java:
//Aufgabe 2e
package WS07_08.Aufgabe2_Enum;

public class Playercore extends Thread {

	public enum Zustand {PLAYING, STOPPED, OFF};
	private Zustand zustand;
	static boolean isCreated = false;
	//static int count = 0;
	
	public void run() {
		while(!isInterrupted()) {
			try {
				[COLOR="Red"]if (Zustand.PLAYING == zustand) {
					System.out.println("Pling");
				}
				if (zustand==Zustand.STOPPED) {
					System.out.println(".");
				}[/COLOR]
				else {
					
				}
				
				Thread.sleep(2500);
				System.out.println("Ich laufe noch!");
			}
			catch (Exception e) {
				
			}
		}
		
		
		
	}
	
	Playercore() throws Exception {
		zustand = Zustand.OFF;
		//count++;
		//isCreatedCheck();
		isCreated = true;
	}
	
	public void play() {
		zustand = Zustand.PLAYING;
		System.out.println(zustand);
		
		
	}
	
	public void stopPlayer() {
		zustand = Zustand.STOPPED;
		System.out.println(zustand);

	}
	
	public void off() {
		zustand = Zustand.OFF;
		System.out.println(zustand);

	}
	
	public Zustand getPlayerState() {
		//System.out.println(count);
		return zustand;
	}
	
	private void isCreatedCheck() throws Exception {
		
		if (isCreated) {
			throw new Exception();				
		} 
		
	}
	
	public static void main(String[] args) {
	// TODO Auto-generated method stub
		try {
			Playercore thread1 = new Playercore();
			Playercore p1 = new Playercore();
			System.out.println(p1.getPlayerState());
			p1.play();
			thread1.start();	
		}	
		catch (Exception e) {
			System.out.println("Objekt bereits vorhanden");
		}
	}	
}
 
Naja für thread1 ist ja der Status auch immer noch OFF, du setzt PLAYING ja nur für die andere Instanz. Wieso hast du eig. überhaupt zwei? oO

Java:
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Playercore thread1 = new Playercore();
			System.out.println(thread1.getPlayerState());
			thread1.play();
			thread1.start();
		} catch (Exception e) {
			System.out.println("Objekt bereits vorhanden");
		}
	}

edit:

enums vergleicht man mit equals.
Ähm:
Enum.class:
Java:
    /**
     * Returns true if the specified object is equal to this
     * enum constant.
     *
     * @param other the object to be compared for equality with this object.
     * @return  true if the specified object is equal to this
     *          enum constant.
     */
    public final boolean equals(Object other) { 
        return this==other;
    }
 
Zuletzt bearbeitet:
Wow, danke für die schnellen Antworten.

@ Guardi:
Enums kann man mit == vergleichen

@eRaaa:
Super, Danke.
Hatte das mal irgendwo gelesen, am besten ich schlags mir wieder aus dem Kopf 🙂
 

Zurück
Oben