java:enum
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:enum [2018/02/09 13:28] – gthanos | java:enum [2022/02/24 12:44] (current) – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Enumerations ====== | ====== Enumerations ====== | ||
- | Τα // | + | Τα // |
Η πιο απλή δήλωση ενός enumerated τύπου είναι η παρακάτω. | Η πιο απλή δήλωση ενός enumerated τύπου είναι η παρακάτω. | ||
Line 8: | Line 8: | ||
enum Level { HIGH, MEDIUM, LOW }; | enum Level { HIGH, MEDIUM, LOW }; | ||
- | public | + | class LevelUsage { |
- | | + | static void main(String []args) { |
Level level = Level.HIGH; | Level level = Level.HIGH; | ||
System.out.println(" | System.out.println(" | ||
Line 27: | Line 27: | ||
enum Level { HIGH, MEDIUM, LOW }; | enum Level { HIGH, MEDIUM, LOW }; | ||
- | public | + | class LevelUsage { |
- | | + | static void main(String []args) { |
Level level = Level.HIGH; | Level level = Level.HIGH; | ||
if(level == Level.HIGH) | if(level == Level.HIGH) | ||
Line 45: | Line 45: | ||
enum Level { HIGH, MEDIUM, LOW }; | enum Level { HIGH, MEDIUM, LOW }; | ||
- | public | + | class LevelUsage { |
- | | + | static void main(String []args) { |
Level level = Level.HIGH; | Level level = Level.HIGH; | ||
switch(level) { | switch(level) { | ||
Line 68: | Line 68: | ||
enum Level { HIGH, MEDIUM, LOW }; | enum Level { HIGH, MEDIUM, LOW }; | ||
- | public | + | class LevelUsage { |
- | | + | static void main(String []args) { |
Level level = Level.HIGH; | Level level = Level.HIGH; | ||
for(Level l: Level.values()) | for(Level l: Level.values()) | ||
Line 77: | Line 77: | ||
</ | </ | ||
- | Για να εκτυπώσετε την τιμή ενός enum ως αλφαριθμητικό μπορείτε να χρησιμοποιήσετε τη μέθοδο //name//: | + | Για να εκτυπώσετε την τιμή ενός enum ως αλφαριθμητικό μπορείτε να χρησιμοποιήσετε τη μέθοδο |
<code java> | <code java> | ||
enum Level { HIGH, MEDIUM, LOW }; | enum Level { HIGH, MEDIUM, LOW }; | ||
- | public | + | class LevelUsage { |
- | | + | static void main(String []args) { |
Level level = Level.HIGH; | Level level = Level.HIGH; | ||
System.out.println(" | System.out.println(" | ||
Line 89: | Line 89: | ||
</ | </ | ||
- | /* | ||
- | ===== Πεδία και μέθοδοι σε Enum ===== | ||
- | Ένας τύπος enum μπορεί να περιέχει πεδία, κατασκευαστές και μεθόδους ως εξής: | + | |Προηγούμενο: |
- | + | ||
- | <code java LevelUsage.java> | + | |
- | enum Level { | + | |
- | HIGH (3), //calls constructor with value 3 | + | |
- | MEDIUM(2), | + | |
- | LOW | + | |
- | + | ||
- | private int levelCode; | + | |
- | + | ||
- | Level(int levelCode) { | + | |
- | this.levelCode = levelCode+1; | + | |
- | } | + | |
- | + | ||
- | public void setLevelCode(int lc) { | + | |
- | levelCode = lc; | + | |
- | } | + | |
- | + | ||
- | public int getLevelCode() { | + | |
- | return this.levelCode; | + | |
- | } | + | |
- | + | ||
- | public String toString() { | + | |
- | return "My Level is: " | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | public class LevelUsage { | + | |
- | public static void main(String [] args) { | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | */ | + | |
- | + | ||
- | |Προηγούμενο: | + | |
java/enum.1518182902.txt.gz · Last modified: 2018/02/09 13:28 by gthanos