ν΄λΉ κ²μκΈμ 보며, κ°μκΈ° κΆκΈνκ² μ겨μ μ£Όλμ΄ κ°λ°μμΈ μ μκ²λ μΆν μ€νμ μλͺ»μ΄ν΄ν κ² κ°μμ μ΄κ²μ κ² μ€νν΄λ³΄κ³ 곡μ λλ¦¬λ €ν©λλ€.
μ κ° μ²μ ν΄λΉ κΈ°λ₯μ μ²μ μ νκ³ λ μκ°μ Diamond Problemμ λ§κΈ°μν΄μ class λ€μ€ μμμ λ§μκ² μλμλ?
Interface λ₯Ό λ€μ€ μμ λ°κ³ default ꡬνμ΄ κ°λ€λ©΄? 무μμ νΈμΆνμ§? λΌλ μκ°μ΄λ€μ΄μ
μλμ κ°μ μ½λλ₯Ό μμ±ν΄λ³΄μμ΅λλ€.
public interface IFooA
{
void Print()
{
Console.WriteLine($"{nameof(IFooA)} κΈ°λ³Έ ꡬν");
}
}
public interface IFooB
{
void Print()
{
Console.WriteLine($"{nameof(IFooB)} κΈ°λ³Έ ꡬν");
}
}
public class FooA : IFooA
{
}
public class Foo : IFooA, IFooB
{
}
var fooA = new FooA();
fooA.Print(); // μ»΄νμΌ μλ¬
var a = fooA as IFooA;
a?.Print(); // μΆλ ₯: IFooA κΈ°λ³Έ ꡬν
var foo = new Foo();
foo.Print(); // μ»΄νμΌ μλ¬
Safely update interfaces using default interface methods - C# | Microsoft Learn
However, the
SampleCustomerclass doesnβt inherit members from its interfaces.
μ λ§ν¬μ²λΌ interfaceμμ λ©€λ²λ₯Ό μμνμ§ μλ λ€λ κΈ°λ³Έ κ·μΉμ μ μ§νλ€κ³ ν©λλ€.
μμ μ 보λ₯Ό μ°Ύλ€λ³΄λ Redditμμ λͺ»λ³Όκ±Έ λ λ°κ²¬νκ³ λ§μμ΅λλ€.
In java, scala, and kotlin, they behave as expected, as the implementing class inherit the default methods, but in C#, βA class does not inherit members from its interfacesβ (from the doc itself).
public interface IFooA { default void print() { System.out.println("FooA"); } }public class Foo implements IFooA { }public static void main(String[] args) { var foo = new Foo(); foo.print(); // μΆλ ₯: "FooA"; }
(μμ μ½λλ JAVA μ
λλ€)
μ! κ·Έλ¬λ©΄ IFooBλ₯Ό μΆκ°νλ©΄ μ΄λ»κ²λ κΉμ?
κ·Έλλ λ©μλλ₯Ό ꡬννλΌκ³ μ»΄νμΌ μλ¬λ₯Ό λ°μμν€κ³ μλ€μ.
https://www.reddit.com/r/csharp/comments/120kr4p/default_interface_methods_and_inheritance/
redditμ ν΄λΉ λ΅λ³μ μμΈν λ¬μμ£Όμ λΆμ΄ μμ΄μ λ체ν©λλ€!
νΉμ μ½κΈ° μ«μΌμ λΆμ λ°μ μμ½λ³Έμ λ΄μ£Όμλ©΄ λ κ² κ°μ΅λλ€!
Redditμ λ¬λ¦° κΈ μμ½λ³Έμ λ²μν΄μ λ¬μ λκ² μ΅λλ€.
μΈν°νμ΄μ€λ λ³Έμ§μ μΌλ‘ ꡬνμ κ°μ§μ§ μλ ꡬ쑰μ λλ€.
μΈν°νμ΄μ€λ₯Ό ν λ² κ³΅κ°ν μ΄νμ λ³κ²½νλ κ²μ νΈνμ±μ κΉ¨λ¨λ¦¬λ λ³ν μ λλ€.
μ΄λ° νΈνμ± κΉ¨μ§μ, μ무리 κ³ ν΅μ μ€μ΄λ € ν΄λ νμ κ³ ν΅μ€λ½μ΅λλ€.μΌλΆ μ¬λλ€μ μΈν°νμ΄μ€μ λ©μλλ₯Ό μΆκ°νλ κ²μ΄ κ³΅κ° μ΄νμλ νΈνμ±μ κΉ¨λ¨λ¦¬λ λ³νλΌλ μ μ μ’μνμ§ μμμ΅λλ€.
C# νμ μ΄λ₯Ό μ΅λν μμ νκ² λ§λ€κΈ° μν΄ λ Έλ ₯νμ΅λλ€.
κ·ΈλΌμλ λΆκ΅¬νκ³ νΈνμ±μ κΉ¨λ¨λ¦¬λ λ³νλ μ¬μ ν κ³ ν΅μ€λ½μ΅λλ€.C#μ μΌκ΄λκ², ν΄λμ€ κΈ°λ°μ virtualκ³Ό abstract λ©μλλ§ λ€νμ±μ κ°μ§λ©°, κ·Έ μΈμ κΈ°λ²λ€μ κ·Έλ μ§ μμ΅λλ€.

