以下で説明するコンテキスト/シナリオに基づいて、Implement Interface Explicitlyが C# で何を伴うのかを理解したいと思います。次のインターフェースがあるとしましょう:
public interface ITestService
{
void Operation1();
void Operation2();
}
以下に示すようにインターフェイスを明示的に実装するとしましょう。何らかの手段を使用して Operation1() から Operation2() を呼び出すことは可能ですか?
public sealed class TestService : ITestService
{
public TestService(){}
void ITestService.Operation1()
{
// HOW WOULD ONE SUCCESSFULLY INVOKE Operation2() FROM HERE. Like:
Operation2();
}
void ITestService.Operation2()
{
}
}
以下で異なるように宣言された testService1 と testService2 が異なる動作をするようにするために、(ラップの下で) 何が異なるのでしょうか?
static class Program
{
static void Main(string[] args)
{
ITestService testService1 = new TestService();
testService1.Operation1(); // => Works: WHY IS THIS POSSIBLE, ESPECIALLY SINCE Operation1() AND Operation2() WOULD BE *DEEMED* private?
// *DEEMED* since access modifiers on functions are not permitted in an interface
// while ...
TestService testService2 = new TestService();
testService2.Operation1(); //! Fails: As expected
}
}