C# 当您使用相同的方法实现两个接口时,您如何知道调用了哪一个接口?

C# 当您使用相同的方法实现两个接口时,您如何知道调用了哪一个接口?,c#,interface,C#,Interface,如果您在接口I1和I2中有method(),以及以下类 class TheClass : I1, I2 { void TheMethod() } 如果某个对象实例化了类,它如何知道它使用的是哪个接口?只要方法签名相同,一个方法实现两个或多个接口的方法是完全合法的 没有办法知道“通过哪个接口”调用一个方法——没有这样的概念(这也没关系)。只要方法签名相同,一个方法实现两个或多个接口的方法是完全合法的 没有办法知道“通过哪个接口”调用一个方法——没有这样的概念(这无关紧要)。你不使用调用它

如果您在接口I1和I2中有
method()
,以及以下类

class TheClass : I1, I2
{
    void TheMethod()
}

如果某个对象实例化了类,它如何知道它使用的是哪个接口?

只要方法签名相同,一个方法实现两个或多个接口的方法是完全合法的


没有办法知道“通过哪个接口”调用一个方法——没有这样的概念(这也没关系)。

只要方法签名相同,一个方法实现两个或多个接口的方法是完全合法的


没有办法知道“通过哪个接口”调用一个方法——没有这样的概念(这无关紧要)。

你不使用调用它的接口。接口只是一个契约,定义了可以调用哪些方法。您总是调用实现。

在调用接口的意义上,您不使用接口。接口只是一个契约,定义了可以调用哪些方法。您总是调用实现。

真正的问题是,“谁在乎它使用的是哪个接口?”实际上,它根本不是“使用”接口。它使用实现来实现接口。

真正的问题是,“谁在乎它使用的是哪个接口?”实际上,它根本不是“使用”接口。它使用实现来实现接口。

如果两个方法都是公共的,那么无论调用哪个接口,都将调用相同的方法。如果需要更大的粒度,则需要使用显式接口实现:

class TheClass : I1, I2
{
    void I1.TheMethod() {}
    void I2.TheMethod() {}
}
用法可能如下所示:

TheClass theClass = new TheClass();
I1 i1 = theClass;
I2 i2 = theClass;

i1.TheMethod();  // (calls the first one)
i2.TheMethod();  // (calls the other one)
需要记住的一点是,如果您将两个实现都显式化,您将无法再对声明为类的变量调用
方法

theClass.TheMethod();   // This would fail since the method can only be called on the interface

当然,如果您愿意,您可以只使其中一个实现显式,并使另一个实现保持公共,在这种情况下,对类的调用将调用公共版本。

如果两个方法都是公共的,那么无论调用了哪个接口,都将调用同一个方法。如果需要更大的粒度,则需要使用显式接口实现:

class TheClass : I1, I2
{
    void I1.TheMethod() {}
    void I2.TheMethod() {}
}
用法可能如下所示:

TheClass theClass = new TheClass();
I1 i1 = theClass;
I2 i2 = theClass;

i1.TheMethod();  // (calls the first one)
i2.TheMethod();  // (calls the other one)
需要记住的一点是,如果您将两个实现都显式化,您将无法再对声明为类的变量调用
方法

theClass.TheMethod();   // This would fail since the method can only be called on the interface

当然,如果您愿意,您可以只使其中一个实现显式,并使另一个实现保持公共,在这种情况下,对
的调用类将调用公共版本。

这是不相关的。强制转换到任何接口都会导致调用该方法,但由于接口不能包含代码,因此无法从中继承行为。

这与此无关。强制转换到任何接口都会导致调用该方法,但由于接口不能包含代码,因此无法从中继承行为。

如果客户机代码就是这样使用该类的,那么这并不重要。如果它需要做一些特定于接口的事情,它应该声明它需要的接口,并将类分配给它

I1 i = new TheClass()
i.TheMethod();
当然,使用类的当前实现
i
,将
i
声明为
I1
I2
并不重要,因为您只有一个实现

如果您希望每个接口都有一个单独的实现,那么您需要创建显式实现

    void I1.TheMethod()
    {
        Console.WriteLine("I1");
    }

    void I2.TheMethod()
    {
        Console.WriteLine("I2");
    }
但请记住,显式实现不能是公开的。您可以显式地实现其中一个,并将另一个保留为默认值(可以是公共的)

    void I1.TheMethod()
    {
        Console.WriteLine("I1");
    }

    public void TheMethod()
    {
        Console.WriteLine("Default");
    }

查看更多详细信息。

如果客户机代码就是这样使用该类的,那么这并不重要。如果它需要做一些特定于接口的事情,它应该声明它需要的接口,并将类分配给它

I1 i = new TheClass()
i.TheMethod();
当然,使用类的当前实现
i
,将
i
声明为
I1
I2
并不重要,因为您只有一个实现

如果您希望每个接口都有一个单独的实现,那么您需要创建显式实现

    void I1.TheMethod()
    {
        Console.WriteLine("I1");
    }

    void I2.TheMethod()
    {
        Console.WriteLine("I2");
    }
但请记住,显式实现不能是公开的。您可以显式地实现其中一个,并将另一个保留为默认值(可以是公共的)

    void I1.TheMethod()
    {
        Console.WriteLine("I1");
    }

    public void TheMethod()
    {
        Console.WriteLine("Default");
    }

查看更多详细信息。

如果实例化该类,则不使用任何接口。如果将引用强制转换到任一接口,则您正在使用该接口。例如:

TheClass c = new TheClass();
c.TheMethod(); // using the class

I1 i = new TheClass();
i.TheMethod(); // using the I1 interface
声明类时,两个接口将使用相同的方法。还可以为类和单独的接口指定方法:

class TheClass : I1, I2
{
  void TheMethod() {} // used by the class
  void I1.TheMethod() {} // used by the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}
如果仅为接口指定方法,则除非先将引用转换为接口,否则无法访问该方法:

class TheClass : I1, I2
{
  void I1.TheMethod() {} // used by the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}
如果仅为某些接口指定单独的方法,则其他接口将使用与类相同的实现:

class TheClass : I1, I2
{
  void TheMethod() {} // used by the class and the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}

如果实例化该类,则不使用任何接口。如果将引用强制转换到任一接口,则您正在使用该接口。例如:

TheClass c = new TheClass();
c.TheMethod(); // using the class

I1 i = new TheClass();
i.TheMethod(); // using the I1 interface
声明类时,两个接口将使用相同的方法。还可以为类和单独的接口指定方法:

class TheClass : I1, I2
{
  void TheMethod() {} // used by the class
  void I1.TheMethod() {} // used by the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}
如果仅为接口指定方法,则除非先将引用转换为接口,否则无法访问该方法:

class TheClass : I1, I2
{
  void I1.TheMethod() {} // used by the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}
如果仅为某些接口指定单独的方法,则其他接口将使用与类相同的实现:

class TheClass : I1, I2
{
  void TheMethod() {} // used by the class and the I1 interface
  void I2.TheMethod() {} // used by the I2 interface
}

如果接口中只有
method()
,那么我猜您希望实现是公共的。如果接口中只有
method()
,那么我猜您希望实现是公共的。我看