继承(2个基本类归为一个子类) [英] Inheritance (2 base classes in to one child class)

查看:115
本文介绍了继承(2个基本类归为一个子类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题

I have a small problem

 class a
{
   public void getmember();
    {
        ----
        -----
     }
}

class b
{
   public void  setmember();
    {
      ---
      ----
     }
}


class c:  a , b 
{
    public void display()
    {
---- 
 ---
    }
}




我的问题是

C级:a,b不起作用

即它允许我仅从一个类(a或b)派生,但是我想从两个类派生(我不想在类之间更改方法)

请帮助我




my problem is

class c: a , b not working

i.e it is allowing me to derive only from one class (a or b), but i want to derive from both classes (I don''t want to change methods between classes)

please help me

推荐答案

在C#中,因此在.net中,您只能从一个基类继承.您可以通过使用接口来实现一种多重继承:

C#的模拟多重继承模式 [
In C# and therefore in .net you can only inherit from one base class. You can achieve a kind of multiple inheritance by using interfaces:

Simulated Multiple Inheritance Pattern for C#[^]


同意吉姆.
.Net不支持多重继承,但是您可以在Interfaces的帮助下实现该继承,如果满足您的要求,则可以采用多层次继承.
多级继承限制了实现,并且它可能不符合许多复杂的案例方案,并且可以使用接口来决定在派生类中实现什么.
请参阅将您的代码修改为可能的解决方案.

A类
{
public void getmember();
{
...
}
}

B类:a
{
public void setmember();
{
...
}
}


C级:b
{
公共无效的display()
{
...
}
}
Agree with jim.
.Net does not support multiple inheritance, but you can achieve it in help of Interfaces, and other way to do is multilevel inheritance, if that is what matches your requirement.
Multilevel inheritance limits the implementation and it may not comply many complex case scenarios, and using interfaces you can decide what to implement in deriving class.
plz see your code modified as possible solution.

class a
{
public void getmember();
{
...
}
}

class b: a
{
public void setmember();
{
...
}
}


class c: b
{
public void display()
{
...
}
}


这篇关于继承(2个基本类归为一个子类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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