实现使用成员为实现程序接口 [英] implements interface using member as implementor

查看:128
本文介绍了实现使用成员为实现程序接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了实现IA类的。结果
现在我需要创建B类应该也实现IA。 B类有A类为成员的实例。



有没有办法来定义A的实例,在B级实现了IA?



  interfase IA {
无效方法1();
无效方法2();




无效methodN();
}

A类:IA {
公共无效方法1(){}
公共无效方法2(){}




公共无效methodN(){}
}

B类:IA {
私人IA M_A;
公众B(IA一){
M_A =一个;
}

//而是这一切我期待的方式来定义M_A是落实乙
IA公共无效方法1(){使用的。方法1}
公共无效方法2(){使用a.method2的}




公共无效methodN(){使用a.methodN的}
}


解决方案

如果 B 是真的要落实 IA ,那么 B 必须重新定义每个接口方法一个接一个,即使每个实施是简单地封装 A <实施呼叫/ code>成员。



不过,有一个偷懒的办法,可以防止你这一切繁琐的东西,这几乎可以被视为是相同的,从的实际情况来看:

 类节目
{
静态无效的主要(字串[] args)
{
CatOwner鲍勃=新CatOwner();
Console.WriteLine(((CAT)BOB).Cry);
Console.ReadKey();
}
}

接口ICry
{
串惊魂{搞定; }
}

类猫:ICry
{
公共字符串惊魂{{返回!喵; }}
}

类CatOwner
{
私人猫_MyCat;

公共CatOwner()
{
_MyCat =新猫();
}

公共静态隐运营商卡特彼勒(CatOwner PO)
{
返回po._MyCat;
}
}



CatOwner 并没有真正落实惊魂由于猫主人不是谁叫声之一:他的猫一样。但作为一个近似,我们可以认为,通过要求给猫主人哭,我们当然意味着这种需求实际上是针对他的猫,不是所有者本身。然后,我们剧组的猫主人给他的猫,然后我们就可以让他惊魂



这是很有趣,不是吗? : - )



编辑:



这是说,Magnus的答案是高度值得考虑恕我直言。这似乎更符合逻辑,如果传递的成员是好的考虑到语义语境更干净。我的解决方案可能仍然有趣的,如果 B 只是一种增强的各种 A 的不能被继承(密封)或者在这种特定背景下...真的取决于上下文和语义约束...


I've got class A that implements IA.
Now I need to create class B that should implement also IA. Class B has instance of class A as a member.

Is there any way to define that A's instance implements the IA in class B?

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 }
}

解决方案

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.

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. 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.

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...

这篇关于实现使用成员为实现程序接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆