44. μΈν°νμ΄μ€μ νλ‘νΌν°
μΈν°νμ΄μ€λ λ©μλ λΏλ§ μλλΌ νλ‘νΌν°μ μΈλ±μλ κ°μ§ μ μλ€.
νλ‘νΌν°λ μΈλ±μλ₯Ό κ°μ§ μΈν°νμ΄μ€λ₯Ό μμνλ ν΄λμ€κ° 'λ°λμ' ν΄λΉ νλ‘νΌν°μ μΈλ±μλ₯Ό ꡬνν΄μΌ νλ κ²μ λ¬Όλ‘ μ΄λ€.
(λΉμ°ν μ΄μΌκΈ°μ΄μ§λ§)μΈν°νμ΄μ€μ λ€μ΄κ°λ νλ‘νΌν°λ ꡬνμ κ°μ§ μλλ€.
μΈν°νμ΄μ€μ νλ‘νΌν° μ μΈμ ν΄λμ€μ μλ ꡬν νλ‘νΌν° μ μΈκ³Ό κ·Έ λͺ¨μ΅μ΄ λμΌνλ€.
λ€μμ μΈν°νμ΄μ€μ νλ‘νΌν° μ μΈ νμμ΄λ€.
interface μΈν°νμ΄μ€μ΄λ¦
{
public νμ νλ‘νΌν°μ΄λ¦1
{
get; set;
}
public νμ νλ‘νΌν°μ΄λ¦2
{
get; set;
}
//...
}
λ€μμ νλ‘νΌν°λ₯Ό κ°μ§ μΈν°νμ΄μ€μ μ΄λ₯Ό μμνλ νμ ν΄λμ€μ μμ΄λ€.
interface Iproduct
{
string ProductName
{
get;
set;
}
}
class Product : Iproduct
{
private string productName;
string ProductName
{
get;
set;
}
}
<μμ >
using System;
namespace PropertiesInterface
{
interface INamedValue
{
string Name { get; set; } //μλ ꡬν νλ‘νΌν°μ²λΌ ꡬνμ΄ μμ§λ§, C# μ»΄νμΌλ¬λ μΈν°νμ΄μ€μ νλ‘νΌν°μ λν΄μλ
//μλμΌλ‘ ꡬνν΄μ£Όμ§ μλλ€. μ΄μ λ... μΈν°νμ΄μ€λ μ΄λ€ ꡬνλ κ°μ§μ§ μκΈ° λλ¬Έμ΄λ€!
string Value { get; set; }
}
class NamedValue : INamedValue
{
//INamedValue μΈν°νμ΄μ€λ₯Ό μμνλ NamedValue ν΄λμ€λ λ°λμ
//Nameκ³Ό Valueλ₯Ό ꡬνν΄μΌνλ€.
//μ΄λ μλ ꡬν νλ‘νΌν°λ₯Ό μ΄μ©νλ κ²λ κ°λ₯νλ€.
public string Name { get; set; }
public string Value { get; set; }
}
class MainApp
{
static void Main(string[] args)
{
NamedValue name = new NamedValue() { Name="μ΄λ¦", Value="무μ§κ°μ½λ" };
NamedValue height = new NamedValue() { Name = "ν€", Value = "190cm" };
NamedValue weight = new NamedValue() { Name = "무κ²", Value = "88kg" };
Console.WriteLine($"{name.Name} : {name.Value}");
Console.WriteLine($"{height.Name} : {height.Value}");
Console.WriteLine($"{weight.Name} : {weight.Value}");
}
}
}
<μΆλ ₯ κ²°κ³Ό>
μ΄λ¦ : 무μ§κ°μ½λ
ν€ : 190cm
λ¬΄κ² : 88kg
+