-
-
Save orange-in-space/5142e97f1d045d00469bddcaa1915e65 to your computer and use it in GitHub Desktop.
1+hogeが出来るかできないかどうかと、静的型付けと動的型付けは関係ないよって説明するためのクラス><
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
namespace YurufuwaZikken | |
{ | |
internal class Yurufuwa | |
{ | |
//1+hogeが出来るかできないかどうかと、静的型付けと動的型付けは関係ないよって説明するためのクラス>< | |
//for C# 8.0 | |
// | |
//(C) orange_in_space | |
//License: CC0 | |
//中身 | |
public ValueType EntityV = 0; | |
public string EntityS = ""; | |
private readonly bool isValueType = true; | |
/// <summary> | |
/// 数値ならTrue | |
/// </summary> | |
public bool IsValueType { get => isValueType; } | |
public Yurufuwa() | |
{ | |
// | |
} | |
//数値型からのコンストラクタ | |
public Yurufuwa(System.ValueType entity) | |
{ | |
EntityV = entity; | |
isValueType = true; | |
} | |
//文字列型からのコンストラクタ | |
public Yurufuwa(string? entity) | |
{ | |
if (entity != null) | |
{ | |
EntityS = entity; | |
} | |
else | |
{ | |
EntityS = ""; | |
} | |
isValueType = false; | |
} | |
//数値を文字に変換するやつ(なんかC#8からnullable対策でめんどくさくなったコード><(?)) | |
private string GetValueTypeString() | |
{ | |
string? result = EntityV.ToString(); | |
if (result == null) | |
{ | |
return ""; | |
} | |
else | |
{ | |
return (string)result; | |
} | |
} | |
//文字列にする | |
public override string ToString() | |
{ | |
if (isValueType) | |
{ | |
return GetValueTypeString(); | |
} | |
else | |
{ | |
return EntityS; | |
} | |
} | |
//文字列からの暗黙の型変換 | |
public static implicit operator Yurufuwa(string str) | |
{ | |
return new Yurufuwa(str); | |
} | |
//値型からの暗黙の型変換 | |
public static implicit operator Yurufuwa(ValueType value) | |
{ | |
return new Yurufuwa(value); | |
} | |
//加算演算子の実装 | |
public static Yurufuwa operator +(Yurufuwa z, Yurufuwa w) | |
{ | |
if (z.IsValueType && w.IsValueType) | |
{ | |
return new Yurufuwa(Convert.ToDouble(z.EntityV) + Convert.ToDouble(w.EntityV)); | |
} | |
else | |
{ | |
return new Yurufuwa(z.ToString() + w.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment