Enum
package main;
public enum Transport {
PLANE,
TRAIN,
AUTOMOBILE
}
package main;
public class Main {
public static void main(String[] args) {
Transport tp = Transport.PLANE;
if(tp == Transport.PLANE){
System.out.println("plane...");
}
}
}package main;
public class Main {
public static void main(String[] args) {
Transport tr = Transport.TRAIN;
foo(tr);
}
public static void foo(Transport transport) {
if(transport == Transport.AUTOMOBILE) {
System.out.println("automobile");
} else if(transport == Transport.TRAIN) {
System.out.println("train");
} else {
System.out.println("plane");
}
}
}---------------------------------------------------------------------------------------------
Arxa planda ne baw verir?
package main;
public enum Emeliyyat {
TOPLA('+'),
CIX('-'),
VUR('*'),
BOL('/');
private char c;
Emeliyyat(char c) {
this.c = c;
}
public char getOperationSymbol() {
return this.c;
}
public double hesabla(double a, double b) {
if (c == '+') {
return a + b;
}
return -1;
}
}package main;
public final class Emeliyyat2 {
public static final Emeliyyat2 TOPLA = new Emeliyyat2('+');
public static final Emeliyyat2 CIX = new Emeliyyat2('-');
public static final Emeliyyat2 VUR = new Emeliyyat2('*');
public static final Emeliyyat2 BOL = new Emeliyyat2('/');
private char c;
Emeliyyat2(char c) {
this.c = c;
}
public char getOperationSymbol() {
return this.c;
}
public double hesabla(double a, double b) {
if (c == '+') {
return a + b;
}
return -1;
}
}* Emumlar-dan extend etmek olmur! Cunki finaldir.
--------------------------------------------------------------------------------------------
enum-lar by default public, static ve final-dir.
enum-larin constructor-lari private-dir.
enum-lar implicitly final-dir, yeni extend oluna bilmezler
Комментарии
Отправить комментарий