π» Programming Language/C#
19. μΈν°νμ΄μ€μμ μλ νλ‘νΌν° μ¬μ©λ²
S.Honey
2022. 4. 7. 09:28
namespace InterfacePropertyEx
{
// μΈν°νμ΄μ€ νλ‘νΌν°
interface IKeyValue
{
// μΈν°νμ΄μ€μμ μλ νλ‘νΌν°λ C# μ»΄νμΌλ¬κ° μλμΌλ‘ ꡬνν΄μ£Όμ§ μλλ€.
// λ°λΌμ, ν΄λΉ μΈν°νμ΄μ€λ₯Ό μμλ°λ ν΄λμ€μμ ꡬνν΄μ£Όμ΄μΌ νλ€.
string Key { get; set; }
string Value { get; set; }
}
class KeyValue : IKeyValue
{
public string Key { get; set; }
public string Value { get; set; }
// μ΄λ κ² μμλ°λ ν΄λμ€μμ μλνλ‘νΌν°λ₯Ό μ΄μ©ν΄μ ꡬνν μ μλ€.
// IKeyValueμ μΈν°νμ΄μ€λ₯Ό ꡬνν΄μ£Όκ³ μμ(μ¦, μ»΄νμΌλ¬κ° μλμΌλ‘ νλ‘νΌν°λ₯Ό ꡬνν΄μ€)
}
class Program
{
static void Main(string[] args)
{
KeyValue kv = new KeyValue()
{
Key = "μ΄λ±νκ΅",
Value = "μ°μ²"
};
Console.WriteLine($"{kv.Value} {kv.Key}");
}
}
}
Output
μ°μ² μ΄λ±νκ΅
- μΈν°νμ΄μ€(Interface)μλ ꡬνλΆκ° μ‘΄μ¬νμ§ μλλ€.
- μμ μ½λμ κ°μ΄ μΈν°νμ΄μ€ λ΄λΆμμ μλ νλ‘νΌν°λ₯Ό μ¬μ©νκ²λλ©΄ ꡬνλΆκ° μ‘΄μ¬νμ§ μλκ²μμ μκ³ , μμλ°λ ν΄λμ€μμ ꡬνλΆλ₯Ό μ μν΄μΌνλ€.
- μ½λμμ
IKeyValue
λ₯Ό μμνKeyValue
μμλ μλ νλ‘νΌν°λ₯Ό μ΄μ©νκ³ μμΌλ©°, ν΄λΉ ν΄λμ€ λ΄ μλνλ‘νΌν°λ₯Ό μ΄μ©ν΄IKeyValue
μΈν°νμ΄μ€μ μλ νλ‘νΌν°λ€μ ꡬνν΄μ£Όκ³ μλ κ²μ΄λ€.