-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
141 lines (113 loc) · 4.14 KB
/
Copy pathMain.java
File metadata and controls
141 lines (113 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// main.java
// Java Enum Örnekleri ve Açıklamaları
// 1. Basit Enum Tanımı
enum Level {
HIGH, // Yüksek seviye
MEDIUM, // Orta seviye
LOW // Düşük seviye
}
public class Main {
public static void main(String[] args) throws Exception {
// Enum kullanımı
Level level = Level.HIGH;
// Enum if-else kontrolü
if(level == Level.HIGH) {
System.out.println("Seviye: HIGH");
} else if(level == Level.MEDIUM) {
System.out.println("Seviye: MEDIUM");
} else {
System.out.println("Seviye: LOW");
}
// Enum switch-case kullanımı
switch(level) {
case HIGH:
System.out.println("Switch: HIGH");
break;
case MEDIUM:
System.out.println("Switch: MEDIUM");
break;
case LOW:
System.out.println("Switch: LOW");
break;
}
// Enum tüm değerleri üzerinde döngü
for(Level l : Level.values()) {
System.out.println("Enum değeri: " + l);
}
// toString() otomatik çağrılır, ekrana enum ismini basar
System.out.println("toString(): " + level.toString());
// valueOf() ile String'den Enum'a dönüşüm
Level levelFromString = Level.valueOf("HIGH");
System.out.println("valueOf(): " + levelFromString);
// -----------------------------------------
// 2. Enum alanları ve constructor kullanımı
enum LevelWithCode {
HIGH(3), MEDIUM(2), LOW(1);
private final int levelCode;
// Enum constructor'u private olmalıdır
LevelWithCode(int code) {
this.levelCode = code;
}
// Getter method
public int getLevelCode() {
return this.levelCode;
}
}
LevelWithCode l2 = LevelWithCode.HIGH;
System.out.println("Level code: " + l2.getLevelCode());
// -----------------------------------------
// 3. Enum abstract method örneği
enum LevelAbstract {
HIGH {
@Override
public String asLowerCase() {
return "high";
}
},
MEDIUM {
@Override
public String asLowerCase() {
return "medium";
}
},
LOW {
@Override
public String asLowerCase() {
return "low";
}
};
// Abstract method tanımı
public abstract String asLowerCase();
}
System.out.println("Abstract method: " + LevelAbstract.MEDIUM.asLowerCase());
// -----------------------------------------
// 4. Enum bir interface'i implemente edebilir
interface Describable {
String getDescription();
}
enum EnumWithInterface implements Describable {
FIRST("Birinci Değer"),
SECOND("İkinci Değer");
private final String description;
EnumWithInterface(String desc) {
this.description = desc;
}
@Override
public String getDescription() {
return description;
}
}
System.out.println(EnumWithInterface.FIRST.getDescription());
// -----------------------------------------
// 5. EnumSet - Enum'ları verimli tutmak için özel Set
java.util.EnumSet<Level> enumSet = java.util.EnumSet.of(Level.HIGH, Level.MEDIUM);
System.out.println("EnumSet: " + enumSet);
// -----------------------------------------
// 6. EnumMap - Enum'ları anahtar olarak kullanabilen Map
java.util.EnumMap<Level, String> enumMap = new java.util.EnumMap<>(Level.class);
enumMap.put(Level.HIGH, "Yüksek seviye");
enumMap.put(Level.MEDIUM, "Orta seviye");
enumMap.put(Level.LOW, "Düşük seviye");
System.out.println("EnumMap'ten değer: " + enumMap.get(Level.HIGH));
}
}