简体   繁体   English

使用成员作为实现者实现接口

[英]implements interface using member as implementor

I've got class A that implements IA.我有实现 IA 的 class A。
Now I need to create class B that should implement also IA.现在我需要创建应该实现 IA 的 class B。 Class B has instance of class A as a member. Class B 具有 class A 实例作为成员。

Is there any way to define that A's instance implements the IA in class B?有什么方法可以定义 A 的实例在 class B 中实现 IA?

interfase IA {
    void method1();
    void method2();
    .
    .
    .
    .
    void methodN();
}

class A:IA {
    public void method1(){}
    public void method2(){}
    .
    .
    .
    .
    public void methodN(){}
}

class B:IA {
    private IA m_a;
    public B(IA a) {
      m_a=a;
    }

    //Instead all of this I am looking of a way to define that m_a is the implement to IA of B
    public void method1(){ use of a.method1 }
    public void method2(){ use of a.method2 }
    .
    .
    .
    .
    public void methodN(){ use of a.methodN }
}

Not really, you're probably going to want to define some sort of interface that returns the IA member, like the Enumerable / Enumerator pattern.并非如此,您可能想要定义某种返回IA成员的接口,例如Enumerable / Enumerator模式。

public interface IB
{
   public IA Item { get; }
}

Then B could simply return the instance you're storing in it.然后 B 可以简单地返回您存储在其中的实例。

public class B : IB
{
    public IA Item { get; private set; }
}

A could even implement IB A甚至可以实现IB

public class A : IA, IB
{
    public void Method1();
    //...
    public void MethodN();

    IA IB.Item
    {
        get
        {
            return this;
        }
    }
}

Derive B from A is all what you need.从 A 导出 B 就是您所需要的。

class B:A {

    public B() {

    }
}

You basically have two options: inherit from A , or encapsulate A .您基本上有两个选择:从A继承或封装A This is basically the difference between two design patterns: encapsulation andinheritance .这基本上是两种设计模式之间的区别: 封装inheritance

If B is really supposed to implement IA , then B must redefine each of the interface methods one by one, even if each implementation is simply a call to the implementation of the encapsulated A member.如果B真的应该实现IA ,那么B必须一个一个地重新定义每个接口方法,即使每个实现只是对封装A成员的实现的调用。

Nevertheless, there is a lazy way which can prevent you from all this tedious stuff and which can be considered almost as the same, from a practical point of view:然而,有一种懒惰的方法可以让你远离所有这些乏味的东西,从实际的角度来看,它几乎可以被认为是相同的:

class Program
{
    static void Main(string[] args)
    {
        CatOwner Bob = new CatOwner();
        Console.WriteLine(((Cat)Bob).Cry);
        Console.ReadKey();
    }
}

interface ICry
{
    string Cry { get; }
}

class Cat : ICry
{
    public string Cry { get { return "Meow !"; } }
}

class CatOwner
{
    private Cat _MyCat;

    public CatOwner()
    {
        _MyCat = new Cat();
    }

    public static implicit operator Cat(CatOwner po)
    {
        return po._MyCat;
    }
}

CatOwner doesn't really implement Cry since the cat owner is not the one who meows: his cat does. CatOwner并没有真正实现Cry ,因为猫主人不是喵喵叫的人:他的猫会。 But as an approximation we could consider that by demanding to the cat owner to cry, we of course mean that this demand actually targets his cat, not the owner itself.但作为一个近似值,我们可以认为,通过要求猫主人哭泣,我们当然意味着这个要求实际上是针对他的猫,而不是主人本身。 Then we "cast the cat owner to his cat" and then we can make him Cry .然后我们“将猫主人投给他的猫”,然后我们可以让他Cry

That's pretty funny, no?这很有趣,不是吗? :-) :-)

Edit:编辑:

That said, Magnus' answer is highly worth considering IMHO.也就是说,马格努斯的回答非常值得考虑恕我直言。 It appears more logical and more clean if passing a member is fine considering the semantic context.如果考虑到语义上下文,传递一个成员是好的,它看起来更合乎逻辑和更干净。 My solution may be still interesting if B is just a kind of enhanced variety of A which cannot be inherited (sealed), or in such a particular context... Really depends on the context and the semantic constraints...如果B只是A的一种增强变体,不能被继承(密封),或者在这样的特定上下文中,我的解决方案可能仍然很有趣......真的取决于上下文和语义约束......

Not sure, but why are you inheritting class B from IA?不确定,但你为什么要从 IA 继承 class B? you already have an instance of object A in class B, you can use that...您已经在 class B 中有一个 object A 的实例,您可以使用它...

How about exposing the "implementer" in a property instead of B implementering the interface.如何在属性中公开“实现者”而不是 B 实现接口。

class B
{
  public IA Implementer {get; private set;}
  public B(IA a) 
  {
      Implementer = a;
  }
}

Looking at your question from design perspective, it is a perfectly valid question (in a more complicated real life situation) and it would be nice to save a lot of lines of code by just saying that member m_a is the one that implements all the IA interface in B.从设计的角度来看您的问题,这是一个完全有效的问题(在更复杂的现实生活中),只需说成员 m_a 是实现所有 IA 的那个就可以节省很多代码行B中的接口

I don't quite agree with the suggestions to inherit B from A: 1- in real life, B could inherit from another class unrelated, or say you are implementing IC, and ID, and have members m_c and m_d would be nice to be able to point the implementation of C to m_c and implementation fo ID to m_d..etc 2- replacing aggregation with inheritance is usually a bad design.我不太同意从 A 继承 B 的建议: 1- 在现实生活中,B 可以从另一个不相关的 class 继承,或者说你正在实现 IC 和 ID,并且有成员 m_c 和 m_d 会很高兴能够将 C 的实现指向 m_c 并将 fo ID 的实现指向 m_d..etc 2- 用 inheritance 替换聚合通常是一个糟糕的设计。

Nice idea though..不过好主意..

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

相关问题 C#在接口及其实现者类中使用泛型 - C# using Generics both in the Interface and its implementor class 如何指定接口的实现者抛出的异常? - How to specify exceptions to be thrown by an implementor of an interface? F#类将接口实现为私有成员,为什么呢? - F# class implements interface as private member, why? 具体类如何隐藏它实现的接口的成员 - How can a concrete class hide a member of the interface that it implements 从实现者调用接口扩展方法在C#中很奇怪 - Invoking interface extension methods from implementor is weird in C# 使用反射来设置接口成员 - Using reflection to set an interface member 您是否可以定义一个接口,以便实现它的类必须包含同样属于该类的成员? - Can you define an interface such that a class that implements it must contain a member that is also of that class? Roslyn 2.x CodeFix,实现缺少的接口,委派给成员VS 2017 - Roslyn 2.x CodeFix that implements a missing interface, delegated to a member, VS 2017 检查“ this”是否实现了接口,然后从通用基类内部为子类调用其成员? - Check if 'this' implements an interface then call its member from inside generic base class for subclass? 如何使用c#检查类是否实现并与泛型接口? - how to check if a class implements and interface with a generic using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM