반응형
동일한 메서드 이름을 가진 여러 인터페이스에서 상속
여러 인터페이스에서 상속 된 클래스가 있고 인터페이스에 동일한 이름의 메서드가있는 경우 클래스에서 이러한 메서드를 어떻게 구현할 수 있습니까? 어떤 인터페이스가 구현되는지 어떻게 지정할 수 있습니까?
다음과 같이 인터페이스를 명시 적으로 구현합니다.
public interface ITest {
void Test();
}
public interface ITest2 {
void Test();
}
public class Dual : ITest, ITest2
{
void ITest.Test() {
Console.WriteLine("ITest.Test");
}
void ITest2.Test() {
Console.WriteLine("ITest2.Test");
}
}
명시 적 인터페이스 구현을 사용하는 경우 함수는 클래스에서 공개되지 않습니다. 따라서 이러한 함수에 액세스하려면 먼저 개체를 인터페이스 유형으로 캐스트하거나 인터페이스 유형으로 선언 된 변수에 할당해야합니다.
var dual = new Dual();
// Call the ITest.Test() function by first assigning to an explicitly typed variable
ITest test = dual;
test.Test();
// Call the ITest2.Test() function by using a type cast.
((ITest2)dual).Test();
명시 적 인터페이스 구현을 사용해야합니다.
이러한 인터페이스 중 하나 또는 둘 다를 명시 적으로 구현할 수 있습니다 .
다음 인터페이스가 있다고 가정합니다.
public interface IFoo1
{
void DoStuff();
}
public interface IFoo2
{
void DoStuff();
}
다음과 같이 둘 다 구현할 수 있습니다.
public class Foo : IFoo1, IFoo2
{
void IFoo1.DoStuff() { }
void IFoo2.DoStuff() { }
}
때로는 다음을 수행해야 할 수도 있습니다.
public class Foo : IFoo1, IFoo2
{
public void IFoo1.DoStuff() { }
public void IFoo2.DoStuff()
{
((IFoo1)this).DoStuff();
}
}
하나의 인터페이스를 명시 적으로 구현 하고 다른 인터페이스를 암시 적으로 구현할 수 있습니다 .
public interface ITest {
void Test();
}
public interface ITest2 {
void Test();
}
public class Dual : ITest, ITest2
{
public void Test() {
Console.WriteLine("ITest.Test");
}
void ITest2.Test() {
Console.WriteLine("ITest2.Test");
}
}
ITest.Test될 것입니다 기본 구현입니다.
Dual dual = new Dual();
dual.Test();
((ITest2)dual).Test();
산출:
Console.WriteLine("ITest.Test");
Console.WriteLine("ITest2.Test");
public class ImplementingClass : AClass1, IClass1, IClass2
{
public override string Method()
{
return "AClass1";
}
string IClass1.Method()
{
return "IClass1";
}
string IClass2.Method()
{
return "IClass2";
}
}
따라서 다른 클래스에서 호출 할 때 개체를 필요한 인터페이스 또는 추상 클래스로 형변환해야합니다.
ImplementingClass implementingClass = new ImplementingClass();
((AClass1)implementingClass).Method();
public interface IDemo1
{
void Test();
}
public interface IDemo2
{
void Test();
}
public class clsDerived:IDemo1,IDemo2
{
void IDemo1.Test()
{
Console.WriteLine("IDemo1 Test is fine");
}
void IDemo2.Test()
{
Console.WriteLine("IDemo2 Test is fine");
}
}
public void get_methodes()
{
IDemo1 obj1 = new clsDerived();
IDemo2 obj2 = new clsDerived();
obj1.Test();//Methode of 1st Interface
obj2.Test();//Methode of 2st Interface
}
대답은 " 명시 적 인터페이스 구현을 사용하여 "입니다.
한 가지 예를 들어 보겠습니다.
using System;
interface A
{
void Hello();
}
interface B
{
void Hello();
}
class Test : A, B
{
void A.Hello()
{
Console.WriteLine("Hello to all-A");
}
void B.Hello()
{
Console.WriteLine("Hello to all-B");
}
}
public class interfacetest
{
public static void Main()
{
A Obj1 = new Test();
Obj1.Hello();
B Obj2 = new Test();
Obj2.Hello();
}
}
출력 : Hello to all-A Hello to all-B
반응형
'Program Club' 카테고리의 다른 글
| std :: strings를 반환해야합니까? (0) | 2020.11.17 |
|---|---|
| ASP.NET MVC, Url 라우팅 : 최대 경로 (URL) 길이 (0) | 2020.11.17 |
| Scala에서 어떻게 배열을 패턴 화합니까? (0) | 2020.11.17 |
| MySql은 성능을 봅니다. (0) | 2020.11.17 |
| 백 스택에서 조각을 꺼내는 방법 (0) | 2020.11.17 |