简体   繁体   English

接口迫使抽象类实现其功能

[英]Interface is forceing abstract class to implement its functions

I have got a abstract class which is implementing 3 interfaces. 我有一个实现3个接口的抽象类。

 public abstract class ServiceBaseCore<Entity, EntityKey> : MarshalByRefObject, IComponentService, IEntityProvider<Entity, EntityKey>
    where Entity : IEntityId<EntityKey>, new()
    where EntityKey : IEntityKey, new()
{
 // Provided functionality/body to some methods of interface
}

Problem: i am getting error that my abstract class is not providing implementation (definition/body) to functions of interface, where as what i read is that "if a class is abstract than there is no need to provide body to all/any functions of interface its implementing". 问题:我得到一个错误,我的抽象类没有为接口的函数提供实现(定义/主体),而我读到的是“如果一个类是抽象的,则不需要为所有/任何函数提供主体”接口的实现”。

Note: the code was generated by codeSmith even though its showing error. 注意:即使显示错误,代码也是由codeSmith生成的。

please tell me where i am wrong and what i am missing. 请告诉我哪里错了,我想念什么。

Thanks 谢谢

Just create some abstract functions, and the compiler will stop complaining: 只要创建一些抽象函数,编译器就会停止抱怨:

public abstract void MyMethodDeclaredInTheInterface();

EDIT: To speed up the process, just move the caret on the interface name in your abstract class, then ctrl + . 编辑:要加快此过程,只需将插入符号移动到抽象类中的接口名称上,然后按ctrl +即可。 and select "Implement interface YourInterface". 并选择“实现接口YourInterface”。 Then a little search and replace over the NotImplementedException should do the trick. 然后稍微搜索并替换NotImplementedException就可以解决问题。

Create abstract methods for the interface. 为接口创建抽象方法。 Otherwise, the class doesn't actually necessarily implement those methods in any way, even though derived classes might (the derived versions wouldn't be available to the base via vtables and therefore could not fulfill the interface contract). 否则,该类实际上不一定以任何方式实现这些方法,即使派生类可以(派生版本将无法通过vtables用于基类,因此无法实现接口协定)。 That would violate the idea behind interfaces. 这将违反接口背后的想法。

Note: it's late and I'm tired, so I might be wrong about the rationale. 注意:太晚了,我很累,所以我可能在原理上是错误的。 But adding abstract methods for the methods required by the interfaces will take care of the problem. 但是,为接口所需的方法添加抽象方法将解决此问题。

You should be able to right click on the Interface name (near MyClass : IMyInterface ) to see the context menu, and then choose 'Implement Interface'. 您应该能够右键单击接口名称(在MyClass : IMyInterface附近)以查看上下文菜单,然后选择“实现接口”。 Visual Studio will create all the required methods and properties to satsify the interface. Visual Studio将创建所有必需的方法和属性来使界面满意。

You may try some IDE to save much of your time. 您可以尝试使用一些IDE来节省大量时间。 I know exactly, that Eclipse can do this automatically. 我完全知道,Eclipse可以自动执行此操作。

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

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