简体   繁体   English

如何处理实现接口并具有基类的类?

[英]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.

相关问题 实现接口的通用基类 - generic base class that implements interface 具有通用类型的基类,该基类实现具有通用类型的接口 - Base class with a generic type which implements an interface with a generic type 这种设计模式是否有名称,其中具体类实现了一个特定接口,该接口实现了 CRUD 操作的基本接口? - Is there a name for this design pattern where a concrete class implements a specific interface which implements a base interface for CRUD operations? 取实现接口的类 - Take class which implements interface Moq具有内部属性并实现接口的类 - Moq a class that has an internal property and which implements an interface 如何创建从实现接口的基类派生的实例列表? - How to create list of instances deriving from a base class that implements interface? 派生自实现接口的基础 class - Derived from a base class that implements an interface 如何从实现接口的类中调用新方法 - How to call new methods from a class which implements interface 从它实现的接口创建 class 的实例 - Create Instance of a class from interface which it implements 当基类实现abtract时,在派生类上指定实现接口 - Specify implement interface on derived class when base class implements it abtract
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM