Last active
November 17, 2017 08:22
-
-
Save smyykb/5d8b41a0e6a8281973cda06677cfcf43 to your computer and use it in GitHub Desktop.
Enumsal Bişiler (Note : Just For Self-reference)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Tanımlama: | |
public enum UserStatus { | |
PENDING, | |
ACTIVE, | |
INACTIVE, | |
DELETED; | |
} | |
- Kullanım: | |
UserStatus.PENDING |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Tanımlama: | |
public enum UserStatus { | |
PENDING(0,"User is pending"), | |
ACTIVE(1,"User is active"), | |
INACTIVE(2,"User is inactive"), | |
DELETED(3,"User is deleted"); | |
private String message; | |
priavte int index; | |
UserStatus(String message, int index){ | |
this.message = message; | |
this.index = index; | |
} | |
public String message(){ | |
return this.message; | |
} | |
public int index(){ | |
return this.index; | |
} | |
} | |
- Kullanım: | |
UserStatus.PENDING.message() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Operation { | |
PLUS, | |
MINUS, | |
TIMES, | |
DIVIDE; | |
double calculate(double x, double y) { | |
switch (this) { | |
case PLUS: | |
return x + y; | |
case MINUS: | |
return x - y; | |
case TIMES: | |
return x * y; | |
case DIVIDE: | |
return x / y; | |
default: | |
throw new AssertionError("Unknown operations " + this); | |
} | |
} | |
} | |
- Kullanımı: | |
double result = Operation.PLUS.calculate(1, 2); | |
System.out.println(result); //3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum UserStatus { | |
PENDING, | |
ACTIVE, | |
INACTIVE, | |
DELETED; | |
} | |
for (UserStatus status : UserStatus.values()) { | |
System.out.println(status); | |
} | |
- OUTPUT: | |
PENDING | |
ACTIVE | |
INACTIVE | |
DELETED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UserStatus status = UserStatus.PENDING; | |
if(status == UserStatus.PENDING) { | |
System.out.println("Now : " + status.message()); | |
} | |
------ | |
switch(status){ | |
case PENDING: | |
//... | |
break; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- String To Enum | |
Operation op = Operation.valueOf("times".toUpperCase()); | |
System.out.println(op.calculate(10, 3)); | |
- Enum To String | |
String operationStr = Operation.TIMES.name(); //TIMES | |
int order = Operation.TIMES.ordinal(); //2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment