π» Programming Language/C#
15. μΈν°νμ΄μ€(Interface) μ΄ν΄
S.Honey
2022. 4. 6. 09:27
namespace InterfaceEx
{
interface IMyInterfaceA
{
void output();
}
interface IMyInterfaceB
{
void output();
}
class MyClass : IMyInterfaceA, IMyInterfaceB // λ€μ€ μμ
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
IMyInterfaceA ia = new MyClass();
ia.output();
IMyInterfaceB ib = new MyClass();
ib.output();
}
void IMyInterfaceA.output()
// λ€μ€μμμ ν΅ν΄ κ°μ Έμ¨ ν΄λμ€λ€μ λ©μλλͺ
μ΄ κ°μκ²½μ°μλ `ν΄λμ€.λ©μλ()` ννλ₯Ό μ΄μ©νλ€.
{
Console.WriteLine("InterfaceA");
}
void IMyInterfaceB.output()
{
Console.WriteLine("InterfaceB");
}
}
}
μΈν°νμ΄μ€(Interface) μ μΈ
interface ν€μλλ₯Ό μ¬μ©
interface μΈν°νμ΄μ€λͺ
{
λ°ννμ λ©μλλͺ
(λ§€κ°λ³μ);
...
}
λ€μ€μμ
μ ν΅ν΄ κ°μ Έμ¨ ν΄λμ€λ€μ λ©μλλͺ μ΄ κ°μκ²½μ°μλν΄λμ€.λ©μλ()
ννλ₯Ό μ΄μ©νλ€.
μΈν°νμ΄μ€
μμλ νλλ₯Ό ν¬ν¨νμ§ μλλ€λ νΉμ§μ΄ μλ€.- (μ΄λ²€νΈ, λ©μλ, νλ‘νΌν°λ§μ λ©€λ²λ‘ κ°μ§λ€.)
μΈν°νμ΄μ€
μ λ©€λ²λ λͺ¨λpublic μ κ·ΌκΆνμΌλ‘ μ€μ
λλ€.- λ°λΌμ μ κ·Ό μ νμ(private, protected λ±)λ₯Ό μ¬μ©ν μ μλ€. =>
publicλ μμ¨μ£Όλκ² μ’μ
(public μ¬μ©μ κ²½κ³ λΈ ^^;)
- λ°λΌμ μ κ·Ό μ νμ(private, protected λ±)λ₯Ό μ¬μ©ν μ μλ€. =>
μΈν°νμ΄μ€
λ μΌμ’ μλͺ μΈμ
μ΄λ€.- λͺ μΈμλ₯Ό λ³΄κ³ ν¨μλ₯Ό μμ±νλ μΌμ’ μ μ€λͺ μ
μΈν°νμ΄μ€
λ ꡬνλΆ(λͺΈν΅)κ° μλμΆμλ©€λ²(μ΄λ²€νΈ, λ©μλ, νλ‘νΌν° λ±)
λ₯Ό κ°μ§λ€.
μΈν°νμ΄μ€
λλ€μ€ μμ
μ΄ κ°λ₯νλ€.- κ°νΉ λ¬Έμ μ μ΄ λ°κ²¬λ λκ° μμΌλ μ‘°μ¬νμ.
μΈν°νμ΄μ€
λ λ€λ₯ΈμΈν°νμ΄μ€
λ₯Ό μμ λ°μ μ μλ€.(=μΈν°νμ΄μ€
κ°μ μμλ κ°λ₯νλ€.)ν΄λμ€
μμλμΈν°νμ΄μ€
λ₯Ό μμλ°μ μ μκ³ ,ꡬ쑰체
μμλμΈν°νμ΄μ€
λ₯Ό μμ λ°μ μ μλ€.
- κΈ°λ³Έμ μΌλ‘
μΈν°νμ΄μ€
λμΈμ€ν΄μ€
λ₯Ό μμ±ν μ μλ€.
namespace MultiInheritanceEx
{
class A
{
public void Output()
{
Console.WriteLine("Aμ μΆλ ₯");
}
}
class B
{
public void Output()
{
Console.WriteLine("Bμ μΆλ ₯");
}
}
class Program : A, B
// C#μ μ΄μκ°μ ν΄λμ€ λ€μ€μμμ΄ λΆκ°λ₯νλ€. => Aμ Bλ₯Ό classλ‘ μ μΈ
// λ¨ Aμ Bκ° μΈν°νμ΄μ€μΈ κ²½μ°μ λ€μ€μμμ΄ κ°λ₯νλ€.
//
{
static void Main(string[] args)
{
}
}
}
- μμ κ°μ΄ λ€μ€ ν΄λμ€λ₯Ό μμλ°λ κ²μ C#μμλ λΆκ°λ₯νλ€.
namespace MultiInheritanceEx
{
interface AA
{
void Output();
}
interface BB
{
void Output();
}
class Program : AA, BB
// C#μ μ΄μκ°μ ν΄λμ€ λ€μ€μμμ΄ λΆκ°λ₯νλ€. => Aμ Bλ₯Ό classλ‘ μ μΈ
// λ¨ Aμ Bκ° μΈν°νμ΄μ€μΈ κ²½μ°μ λ€μ€μμμ΄ κ°λ₯νλ€.
//
{
static void Main(string[] args)
{
}
}
}
- μμ κ°μ΄ μΈν°νμ΄μ€λ€μ λν λ€μ€μμμ κ°λ₯νλ€.
μΈν°νμ΄μ€μμ μΈν°νμ΄μ€λ₯Ό μμ(interface inherits interface)
μΈν°νμ΄μ€ μμ νμ
interface μμ μΈν°νμ΄μ€ λͺ
: λΆλͺ¨ μΈν°νμ΄μ€ λͺ
{
...
}
namespace InterfaceEx2
{
interface ParentInterface
{
void myMethod(string str);
}
interface ChildInterface : ParentInterface
{
void myMethod(string str, int i);
}
class MyClass : ChildInterface
{
public void myMethod(string str)
{
Console.WriteLine(str + "λΆλͺ¨ μΈν°νμ΄μ€ νΈμΆ");
}
public void myMethod(string str, int i)
{
for (int j = 0; j < i; j++)
{
Console.WriteLine(str + j + "μμ μΈν°νμ΄μ€ νΈμΆ");
}
}
// μμμΈν°νμ΄μ€κ° μ μ΄μ λΆλͺ¨μΈν°νμ΄μ€λ₯Ό μμλ°μκΈ°μ
// μμμΈν°νμ΄μ€λ₯Ό μμν ν΄λμ€μμ λΆλͺ¨ μΈν°νμ΄μ€ λ΄μ λ©μλ λν μ μλ₯Ό ν΄μ€μΌ νλ€.
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.myMethod("λΆλͺ¨");
mc.myMethod("μμ", 3);
ParentInterface parent = new MyClass();
parent.myMethod("parent");
}
}
}
Output
λΆλͺ¨λΆλͺ¨ μΈν°νμ΄μ€ νΈμΆ
μμ0μμ μΈν°νμ΄μ€ νΈμΆ
μμ1μμ μΈν°νμ΄μ€ νΈμΆ
μμ2μμ μΈν°νμ΄μ€ νΈμΆ
parentλΆλͺ¨ μΈν°νμ΄μ€ νΈμΆ
- μ΅μ’
μ μΌλ‘
μμμΈν°νμ΄μ€λ₯Ό μμλ°λ ν΄λμ€
μμλμμμΈν°νμ΄μ€
κ° μ μ΄μλΆλͺ¨μΈν°νμ΄μ€
λ₯Όμμ
λ°μκΈ° λλ¬ΈμμμμΈν°νμ΄μ€λ₯Ό μμν ν΄λμ€
μμλΆλͺ¨ μΈν°νμ΄μ€
λ΄μ λ©μλ λν μ μλ₯Ό ν΄μ€μΌ νλ€.