为什么C#不支持多重继承?用例子解释。 [英] Why C# does not support Multiple Inheritance? Explain with example.

查看:250
本文介绍了为什么C#不支持多重继承?用例子解释。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C#不支持多重继承?用例子解释。

解决方案

参考:



http://social.msdn.microsoft.com /论坛/ zh-CN / 616d520b-ffae-4b3f-9e0c-35c47ce6ccc9 / why-c-does-not-support-multiple-inheritance [ ^ ]


  public   class  Parent1 
{
public Parent1()
{

}

< span class =code-keyword> public void SayHello()
{

}
public voi d SayHi()
{

}

}

public class Parent2
{
public Parent2()
{

}

public void SayHello ()
{

}
public void SayHi()
{

}

}
public class Child:Parent1,Parent2-- 理解
{
public void SayHi123()
{

}
}
public partial class _Default:System .W eb.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
Child objChild = new Child();
objChild.SayHello();
objChild.SayHi();
}
}



在上述情况下,当您致电 SayHello SayHi 通过创建chhild类的实例的方法...那时候应该调用父mmethod(Parent1或Parent2)的歧义,所以他们不允许继承多个类......但我们可以通过多级或接口实现相同的


Why C# does not support Multiple Inheritance?? Explain with example.

解决方案

Refer to this:

http://social.msdn.microsoft.com/Forums/en-US/616d520b-ffae-4b3f-9e0c-35c47ce6ccc9/why-c-does-not-support-multiple-inheritance[^]


public class Parent1
{
    public Parent1()
    {

    }

    public void SayHello()
    {

    }
    public void SayHi()
    {

    }

}

public class Parent2
{
    public Parent2()
    {

    }

    public void SayHello()
    {

    }
    public void SayHi()
    {

    }

}
public class Child : Parent1,Parent2-- Its not possible just for understanding
{
    public void SayHi123()
    {

    }
}
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Child objChild = new Child();
        objChild.SayHello();
        objChild.SayHi();
    }
}


In the above scenario when you call the SayHello or SayHi methods by creating instance of chhild class... that time there is ambiguity from which parent mmethod should get called (Parent1 or Parent2) so they are not allowing to inherit multiple classes... but we can achive the same by multilevel or by interface .


这篇关于为什么C#不支持多重继承?用例子解释。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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