[英]How to approach a class which implements an interface and has a base class?
Imagine the following code: 想象以下代码:
MyService myService = new MyService();
myService.Start();
myService.DoStuff();
The MyService
class looks like this: MyService
类如下所示:
public class MyService : ClientBase, IMyService
{
...
}
Now I wanted to change the existing code to call this class by using its interface: 现在,我想更改现有代码以使用其接口来调用此类:
IMyService myService = GetMyService();
myService.Start();
myService.DoStuff();
Looks better, but now I get an error on the line containing myService.Start();
看起来更好,但现在在包含
myService.Start();
的行上出现错误myService.Start();
, since this is an implementation inherited from the ClientBase
base class. ,因为这是从
ClientBase
基类继承的实现。
What should I do now? 我现在应该怎么办? Extend the
IMyService
interface and include the ClientBase
interface? 扩展
IMyService
接口并包括ClientBase
接口? That's the only solution I can think of now, but I don't think it's very elegant... 这是我现在能想到的唯一解决方案,但我认为这不是很优雅。
EDIT: The ClientBase class is the one from System.ServiceModel so I can't change anything about this. 编辑:ClientBase类是System.ServiceModel中的类,因此我无法对此进行任何更改。
If the Start
method is specific to the MyService
class, and is not defined in the IMyService
interface, you shouldn't be calling Start
on a IMyService
reference... 如果
Start
方法特定于MyService
类,并且未在IMyService
接口中定义,则不应在IMyService
参考上调用Start
。
On the other hand, if all IMyService
implementations should support the Start
method, then it should be defined in the interface 另一方面,如果所有
IMyService
实现都应支持Start
方法,则应在接口中对其进行定义。
You could have something like that : 你可能会有类似的东西:
interface IMyService
{
void DoStuff();
void Start();
}
abstract class ClientBase
{
public void Start() { ... }
}
class MyService : ClientBase, IMyService
{
public void DoStuff() { ... }
}
There is nothing wrong with interface inheritance... 接口继承没有错...
public class ClientBase: IClientBase
{
public ClientBase()
{
}
public void Start()
{
}
}
public interface IClientBase
{
void Start();
}
...
public class MyService: ClientBase, IClientBase, IMyService
{
public MyService()
{
}
public void DoStuff()
{
}
}
public interface IMyService: IClientBase
{
void DoStuff();
}
Then you should be allowed to use your existing code: 然后,应该允许您使用现有代码:
IMyService service = new MyService();
service.Start();
service.DoStuff();
An alternative solution would be to make your MyService class implement IClientBase and then just do a cast: 一种替代解决方案是使您的MyService类实现IClientBase,然后进行强制转换:
MyService service = new MyService();
(service as IClientBase).Start();
(service as IMyService).DoStuff();
However, I think youl agree the first solution is much better. 但是,我认为您同意第一个解决方案要好得多。
IF it needs a start method to be used as an IMyService object, then putting that in the interface makes sense to me. 如果需要将start方法用作IMyService对象,则将其放在接口中对我来说很有意义。 Otherwise a general reconsideration of your design might be in order.
否则,可能需要对设计进行总体重新考虑。
What is the error you are getting? 您遇到什么错误?
You should not extend IMyService to include the ClientBase interface. 您不应将IMyService扩展为包括ClientBase接口。 I have had base classes that implemented methods of an interface.
我有实现接口方法的基类。
I would verify the Start method on the ClientBase class is public which is required by all interfeaces. 我将验证ClientBase类上的Start方法是否为public,这是所有接口所必需的。
How about aggregating the base class and interface implementation in an abstract class from which MyService will inherit. 如何在MyService将继承的抽象类中聚合基类和接口实现。 Instantiate MyService but then let the abstract class be the point of entry.
实例化MyService,然后让抽象类成为入口点。 You'd be coding towards a class rather than an interface but i don't know how you can just use an interface since you have concrete implementation to bring with you.
您可能会针对类而不是接口进行编码,但是我不知道如何使用接口,因为您有具体的实现方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.