2013-02-01 8 views
-1

두 개의 인터페이스 A, B (다른 구성 요소에 있음)가 있습니다. 둘 다 동일한 서명 (MyMethod)을 가진 메소드를 선언합니다. 두 인터페이스는 세 번째 인터페이스 (C)에 상속됩니다.new-keyword를 사용하여 두 인터페이스의 메소드를 결합하십시오.

첫 번째 두 인터페이스 (A, B)에서 선언 된 메서드는 항상 동일한 값 (A와 B)을 반환하기위한 것이므로 C에서 파생 될 때 인터페이스를 명시 적으로 구현하고 싶지 않습니다.
new 키워드를 사용하면서 세 번째 인터페이스에서도이 메서드를 선언하면이 작업을 수행 할 수 있습니다.

public interface A { 
    MyType MyMethod(); 
} 
public interface B { 
    MyType MyMethod(); 
} 

public interface C : A,B{ 
    new MyType MyMethod(); 
} 

public class ImplementingClass : C{ 
    public MyType MyMethod(){ 
     // do somethin 
     // return something 
    } 
} 

예상치 못한 문제가 있습니까? 아니면이 나쁜 스타일입니까?

업데이트
죄송합니다. 초기 질문에 전체 내용이 표시되지 않았습니다. 문제는 C의 인터페이스 참조에서 MyMethod를 호출하려고 할 때 발생합니다. 컴파일러는 컴파일하지 않습니다.

C aReferenceToC=new CImplementingClass(); 
aReferenceToC.MyMethod(); // <<< Here the compiler will throw an exception 

전체 예를

C myCImplementationAsAnInterfaceReference = new MyCImplementation(); 
myCImplementationAsAnInterfaceReference.MyMethod(); // This does not compile without declaring MyMethod in C with the new-Keyword 

MyCImplementation myCImplementationReference= new MyCImplementation(); 
myCImplementationReference.MyMethod(); // This however will always compile and run 

public interface A { 
     int MyMethod(); 
} 
public interface B { 
     int MyMethod(); 
} 

public interface C : A, B { 

} 

public class MyCImplementation : C { 

     public int MyMethod() { 
      return 1; 
     } 
} 
+0

-1 이것은 programmers.stackexchange (농담)에 속해 있습니다. 좋은 질문입니다. –

+0

방금 ​​시도한이 클래스를 구현하는 클래스는 하나의 메서드 만 구현하므로 호출되는 메서드는 무작위로 나타납니다. –

+0

@HCL 전체 예제를 사용하면 정확합니다. 새 MyType MyMethod()를 연결하지 않으면 C 유형을 사용할 수 없습니다. 인터페이스 C에서 두 개의 기본 인터페이스가 동일한 메소드 서명을 갖는 이유에 대해 궁금합니다. –

답변

1

당신이 무엇 이렇게, 어떤 방법으로 A.MyMethod, B.MyMethodC.MyMethod의 다른 구현을주는 사람들을 멈추지 않습니다.

class TestABC : C 
    { 
    MyType C.MyMethod() 
    { 
     // 1 
     return null; 
    } 

    MyType A.MyMethod() 
    { 
     // 2 
     return null; 
    } 

    MyType B.MyMethod() 
    { 
     // 3 
     return null; 
    } 
    } 

new 키워드는 어쨌든 "숨겨진"방법을 제거하지 않습니다에. 컴파일러에서 유형에 동일한 서명이있는 두 개의 동일한 메소드 (기본 유형에서 상속 된 메소드 및 현재 유형으로 선언 된 메소드)가 있음을 허용하도록 알려줍니다.

편집 : 질문의 개발을 주어, 여기 괜찮아요 나는 당신의 문제는 정말 (이 처음에 나에게 명확하지 않았다)이었다 생각 :

public interface A { 
    MyType MyMethod(); 
} 
public interface B { 
    MyType MyMethod(); 
} 

public interface C : A,B{ 
} 
: 당신은이 디자인을했다

C myInstance = CreateAnInstanceOfSomeClassImplementingC(); 
myInstance.MyMethod(); // does not compile, ambiguous 

귀하의 질문은 그것이 컴파일러 전자를 제거하기 위해 OK 솔루션의 경우 :

귀하의 문제는이 코드는 컴파일되지 않았다이었다 오류 오류 CS0121 : Cnew 구성원을 도입하여 [...] 메서드 또는 속성 사이에 호출이 모호합니다.

나에게는보기 흉한 것처럼 보입니다. 그러나 나는 다른 해결책을 생각해 낼 수 없다. (당신이 그 코멘트에 나를 알릴 때 Cclass이 될 수 없다). 상속 된 두 개의 메소드가 연합해야한다는 지시를 인터페이스가 가질 방법이 없습니다.

+0

사람들이 MyMethod를 명시 적으로 구현하지 못하게하고 싶습니다. C를 구현하는 클래스에서 MyMethod를 호출하려면 사람들이 명시 적으로 A 또는 B로 캐스팅해야하는 것을 피하기를 원합니다. – HCL

+0

@HCL 그러면 명시 적으로 인터페이스를 구현하지 마십시오 ... – Servy

+0

내 업데이트 된 질문을 참조하십시오. – HCL

-1

정말 여기에 아무것도 변경되지 않습니다 new 키워드를 사용하는지 여부. 그 행동은 똑같습니다.

Testing testing = new Testing(); 
testing.MyMethod(); // calls Testing.MyMethod 

AA testingA = new Testing(); 
testingA.MyMethod(); // calls AA.MyMethod 


public interface A 
{ 
    int MyMethod(); 
} 

public class AA : A 
{ 

    public int MyMethod() 
    { 
     return 11; 
    } 
} 

public interface B 
{ 
    int MyMethod(); 
} 

public interface C : A, B 
{ 
    int MyMethod(); 
} 

public class Testing : AA,C 
{ 
    public int MyMethod() 
    { 
     return 10; 
    } 
}