简体   繁体   English

如何从界面实现公共方法?

[英]How to have public methods implemented from interface?

I have following interface 我有以下界面

interface ITest
{
  void TestVoid();
}

class A : ITest
{
   public void ITest.TestVoid() //will not work
   {
     Conole.WriteLine("Done");
   }

   public void TestVoid() //without name of interface, it works
   {
     Conole.WriteLine("Done");
   }
}

Second question: Is that correct that interface only contains signature of members but never the implementation? 第二个问题:接口是否仅包含成员签名但从未实现,这是否正确?

First method is explicit implementation. 第一种方法是显式实现。 This allows you to implement interface, without showing this method outside of your class. 这允许您实现接口,而不在您的类之外显示此方法。 Also, you cant have visibility modifier on explicit implementation. 此外,您无法在显式实现上使用可见性修饰符。

Second method is normal (implicit) implementation, where you implement interface AND create public method. 第二种方法是正常(隐式)实现,其中实现接口AND创建公共方法。

More : Implicit and Explicit Interface Implementations , C#: Interfaces - Implicit and Explicit implementation 更多: 隐式和显式接口实现C#:接口 - 隐式和显式实现

For your second question : This is exacly what interface is. 对于你的第二个问题:这是什么界面。 It only tells you, what method, properties or events are available on the object. 它只告诉您,对象上有哪些方法,属性或事件可用。 Not how they are implemented. 不是他们如何实施。

The first is called Explicit implementation and should not have any access specifier, Explicit interfaces can also be used to hide the details of an interface that the class developer considers private. 第一个叫做Explicit实现,不应该有任何访问说明符,Explicit接口也可以用来隐藏类开发人员认为私有的接口的细节。 Second one is implicit implementation. 第二个是隐式实现。 It must have public as access specifier. 它必须具有public作为访问说明符。

this link is really helpful to understand it. 这个链接非常有助于理解它。 http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx http://blogs.msdn.com/b/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx

I answer to your second question: yes, it's correct. 我回答你的第二个问题:是的,这是正确的。
Interface declare a sort of contract you must work with if you implement that interface. 如果您实现该接口,接口将声明您必须使用的一种合同
An interface simply (and only) declare what you MUST writein your classes if you implement those interfaces. 如果实现这些接口,接口只需(并且仅)声明您必须在类中编写的内容。

You need to implement the interface like. 你需要实现这样的界面。

class A : ITest {
}.   

You can implement ITests methods explicitly with ITest. 您可以使用ITest明确实现ITests方法。 Prefix 字首

Or implicitly as public methods 或者隐含地作为公共方法

If they are explicit the the way to access them is as such. 如果它们是明确的,那么访问它们的方式就是这样。

ITest a = new A();

Otherwise. 除此以外。
A a = new A(); A =新A();

An interface never has an implementation, 接口永远不会有实现,
A class method always does unless it's marked as abstract - then the derived class has the implementatio n 类方法总是会做,除非它被标记为抽象 - 然后派生类具有实现

The first is called explicit implementation, it will work like this: 第一个叫做显式实现,它会像这样工作:

interface ITest
{
  void TestVoid();
}
class A : ITest
{
   public static void Main()
   {
       A a1 = new A();
       ITest a2 = new A();
       a1.TestVoid(); // won't work
       a2.TestVoid(); // will work
   }
   public void ITest.TestVoid()
   {
     Conole.WriteLine("Done");
   }
}

Interface cannot ever contain implemenations, only signatures. 接口不能包含实现,只能包含签名。

第二个是好的,所以你可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM