简体   繁体   English

我如何在 inheritance 链中多次实现该接口的同时显式实现该接口?

[英]How can i explicitly implement an interface while having implemented that interface multiple times in inheritance chain?

In my Unity C# project I have these interfaces:在我的 Unity C# 项目中,我有这些接口:

interface IGameService
{
 void Initialize(params object[] args);
}

interface IADManager : IGameService
{
 void Show();
}

interface IIAPManager : IGameService
{
 void Purchase();
}

And a class:还有一个 class:

public class MonatizationManager : IAPManager, IADManager
{
 ... 
}

I have a list of IGameService classes, that I need to initialize (also i'm using Zenject).我有一个 IGameService 类列表,我需要对其进行初始化(我也在使用 Zenject)。 For example:例如:

// this basicly means that _iapSettingsProvider and _adSettingsProvider will contain link to a class that implements IADManager and IIAPManager interfaces. In my case this is the same class for these two  interfaces - MonatizationManager 
[Inject] private readonly ISomeSettingsProvider1 _someSettingsProvider1; // example
...
[Inject] private readonly IADManager _iapSettingsProvider;
[Inject] private readonly IIAPManager _adSettingsProvider;
...
[Inject] private readonly ISomeSettingsProvider2 _someSettingsProvider2; // example

// here i initialize my classes (some are managers, some are just providing game settings etc)
// all initQueue's elements must implement IGameService interface
var initQueue = new GameTaskQueue();
initQueue.Add(new GameTaskInitService(_someSettingsProvider1)); // example
...
initQueue.Add(new GameTaskInitService(_iapSettingsProvider));
initQueue.Add(new GameTaskInitService(_adSettingsProvider));
...
initQueue.Add(new GameTaskInitService(_someSettingsProvider2)); // example

// here my queue is starting to initialize my IGameService classes (basically just calling Initialize() method for each element in the queue)
initQueue.Start();

Here comes the problem: I have same class (MonatizationManager) for IADManager and IIAPManager while i also need to Initialize these interfaces separately.问题来了:我对 IADManager 和 IIAPManager 有相同的 class (MonatizationManager),同时我还需要分别初始化这些接口。 So, how do MonatizationManager class would look like?那么,MonatizationManager class 会是什么样子呢?

First, i tried:首先,我试过:

// it did not work
public class MonatizationManager : IAPManager, IADManager
{
 IGameService.IADManager.Initialize(params object[] args) {...}
 IGameService.IIAPManager.Initialize(params object[] args) {...}
}

Then i tried (didn't work either):然后我尝试了(也没有用):

interface IADManager : IGameService
{
 new void Initialize(params object[] args);
 void Show();
}

interface IIAPManager : IGameService
{
 new void Initialize(params object[] args);
 void Purchase();
}

public class MonatizationManager : IAPManager, IADManager
{
 void IIAPManager.Initialize(params object[] args)
 {
  ...
 }

 void IADManager.Initialize(params object[] args)
 {
  ...
 }
// compiler wants me to also write this method, but i think i don't need it
 public void Initialize(params object[] args)
 {
  ...
 }
}

Is it possible to somehow explicitly implement Initialize(params object[] args) method for these two interfaces (IIAPManager and IADManager) in my MonatizationManager class?是否有可能在我的 MonatizationManager class 中为这两个接口(IIAPManager 和 IADManager)显式实现 Initialize(params object[] args) 方法?

Your design is a bit overcomplicated.您的设计有点过于复杂。 There's only a single Intialize -method you should implement, not two.您应该只实现一个Intialize方法,而不是两个。 It would be pretty confusing for a client to use your class and having to need to call Initialize twice.客户端使用您的 class 并且必须调用Initialize两次,这将非常令人困惑。 However that single implementation can of course do whatever you want, for example call two private methods然而,单个实现当然可以做任何你想做的事,例如调用两个私有方法

Furthermore your manager-class already is an IADManager , so there's no reason to also have a member of that type and use that one by calling its members (unless you implement the decorator-pattern or similar).此外,您的经理类已经一个IADManager ,因此没有理由还拥有该类型的成员并通过调用其成员来使用该成员(除非您实现装饰器模式或类似模式)。

So I'd suggest to use this instead:所以我建议改用这个:

interface IGameService
{
    void Initialize(params object[] args);
}    
interface IADManager : IGameService
{
    void Show();
}    
interface IIAPManager : IGameService
{
    void Purchase();
}

public class MonatizationManager : IAPManager, IADManager
{
    void Initialize(params object[] args) 
    {
        // code for initializing the one interface
        // code for initializing the other interface
    }
    void Show() { ... }
    void Purchase() { ... }
}

As an aside you usuially initialize specific classes , not an interface .顺便说一句,您通常会初始化特定的,而不是接口 Although every class has some initialize-method doesn't neccessarily mean such initialization should be available on the public interface.虽然每个 class 都有一些初始化方法并不一定意味着这种初始化应该在公共接口上可用。 Having such a design would enable multiple calls to Initialize for instance, which is usually not intended.具有这样的设计将启用对Initialize的多次调用,例如,这通常不是预期的。 Instead you should provide a fully usable class within the constructor-code (unless there's some heavy magic happening, in which case you would implement some lazy-loading).相反,您应该在构造函数代码中提供一个完全可用的 class(除非发生了一些重大的魔法,在这种情况下您将实现一些延迟加载)。 Then your client-code does not need to know if or if not it already initialized the class, as it already gets a fully initialized one.然后你的客户端代码不需要知道它是否已经初始化了 class,因为它已经得到了一个完全初始化的。

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

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