Last active
August 31, 2018 17:25
-
-
Save lilacs2039/da4be5d1a596a1c5fb5722cdfc593fdb to your computer and use it in GitHub Desktop.
C#のenumにフィールド・メソッドを定義したいとき【JavaからC#へ】 ref: https://qiita.com/lilacs/items/167a73fbbfedf83eb51a
This file contains hidden or 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 AppState | |
{ | |
Active, Paused, Exit | |
} | |
public static class AppStateExt | |
{ | |
private static Dictionary<AppState, string> viewStrDictionary = new Dictionary<AppState, string>() | |
{ | |
{AppState.Active,"アクティブ" }, | |
{AppState.Paused,"ポーズ" }, | |
{AppState.Exit,"イグジット" } | |
}; | |
public static string viewStr(this AppState state) //<- 拡張メソッド | |
{ | |
return viewStrDictionary[state]; | |
} | |
} | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine(AppState.Active.viewStr()); // <- インスタンスメソッドのように呼び出せる | |
} | |
} |
This file contains hidden or 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
enum AppState | |
{ | |
Active("アクティブ"), Paused("ポーズ"), Exit("イグジット"); | |
public final String viewStr; | |
AppState(String viewStr){ | |
this.viewStr = viewStr; | |
} | |
} | |
class Main | |
{ | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
System.out.println(AppState.Active.viewStr); // <- enumのフィールド呼び出し | |
} | |
} |
This file contains hidden or 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
private static Dictionary<AppState, string> viewStrDictionary = new Dictionary<AppState, string>() | |
{ | |
{AppState.Active,"アクティブ" }, | |
{AppState.Paused,"ポーズ" }, | |
{AppState.Exit,"イグジット" } | |
}; | |
private static Dictionary<AppState, int> waitTimeDictionary = new Dictionary<AppState, int>() //<- 追加したフィールド | |
{ | |
{AppState.Active,10 }, | |
{AppState.Paused,20 }, | |
{AppState.Exit,30 } | |
}; | |
//(以下、フィールドの数だけ続く。。。) | |
This file contains hidden or 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 class AppState | |
{ | |
//フィールド一覧 | |
public readonly string viewStr; | |
public readonly int waitTime; | |
//コンストラクタ | |
private AppState(string viewStr, int waitTime) | |
{ | |
this.viewStr = viewStr; | |
this.waitTime = waitTime; | |
} | |
//メソッド | |
public string getViewStr() | |
{ | |
return this.viewStr; | |
} | |
// 参照用インスタンス | |
public static readonly AppState Active = new AppState("アクティブ", 10); | |
public static readonly AppState Paused = new AppState("ポーズ", 20); | |
public static readonly AppState Exit = new AppState("イグジット", 30); | |
} | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine(AppState.Active.viewStr); //<- フィールド呼び出し | |
Console.WriteLine(AppState.Active.getViewStr()); //<- メソッド呼び出し | |
} | |
} |
This file contains hidden or 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 class AppState | |
{ | |
//フィールド一覧 | |
public readonly string viewStr; | |
public readonly int waitTime; | |
//コンストラクタ | |
private AppState(string viewStr, int waitTime) | |
{ | |
this.viewStr = viewStr; | |
this.waitTime = waitTime; | |
} | |
//メソッド | |
public string getViewStr() | |
{ | |
return this.viewStr; | |
} | |
// 参照用インスタンス | |
public static readonly AppState Active = new AppState("アクティブ", 10); | |
public static readonly AppState Paused = new AppState("ポーズ", 20); | |
public static readonly AppState Exit = new AppState("イグジット", 30); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment