C#中对象的继承 [英] Inheritance from object in C#

查看:65
本文介绍了C#中对象的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,

我在C#中有疑问,我们都知道在C#中,一切都是从System.Object继承的。

即Object是每个类的母亲。但是假设我们有这样的代码。

Hi Experts,
I have question in C#,we all know that in C# everything is inherited from System.Object.
i.e Object is mother of every class.But Suppose we have code like this.

public class A:Object
{
    //by default Object is inherited in class A or it explicitly 
    //inherited from object
}

public class B :Object, A
{

}





C#如何在A类的B类驱动程序中处理?C#也继承自B类驱动类中的Object吗?但是C#不支持多重继承。



谢谢

Dinesh Sharma < br $> b $ b

我尝试了什么:



嗨专家,

我在C#中有疑问,我们都知道在C#中,一切都是从System.Object继承的。

即Object是每个类的母亲。但是假设我们有这样的代码。 />



How C# handle in class B driver class from A ?Is C# also inherited from Object in class B drive class?But C# does not support multiple inheritance.

Thanks
Dinesh Sharma

What I have tried:

Hi Experts,
I have question in C#,we all know that in C# everything is inherited from System.Object.
i.e Object is mother of every class.But Suppose we have code like this.

public class A:Object
{
    //by default Object is inherited in class A or it explicitly 
    //inherited from object
}

public class B :Object, A
{

}





C#如何处理在A类的B类驱动程序类中,C#也继承自B类驱动类中的Object吗?但C#不支持多个继承。



谢谢

Dinesh Sharma



How C# handle in class B driver class from A ?Is C# also inherited from Object in class B drive class?But C# does not support multiple inheritance.

Thanks
Dinesh Sharma

推荐答案

你这样做错误。



You are doing it wrong.

  1. .NET中的类没有多重继承。只有弱多重继承形式:它表示接口的多重继承和类的混合继承:每个类不能有多个基类,但接口数量不限。
  2. As System.Object 是所有类型的顶级基类,您不需要将它放在继承列表中;所有类型都是从这个类隐式地派生出来的。如果是单一继承,你可以这样做,但你不需要这样做。
  3. 你正在展示正常链继承的情况: A 派生自 System.Object B 派生自A,间接派生,来自 System.Object 。即使继承链的顶部不是隐式基类,也不需要在继承列表中列出它,并且由于缺少多重继承,因此不允许这样做。



    她就是一个简单的例子:

  1. There is no multiple inheritance for classes in .NET. There is only the weak form of multiple inheritance: it mean multiple inheritance for interfaces and the "mixed" inheritance for classes: each class cannot have more than one base class, but unlimited number of interfaces.
  2. As System.Object is the top-level base class for all types, you don't need to put it in the inheritance list; all types are derived from this class implicitly. In case of singular inheritance, you can do it, but you don't need to do so.
  3. You are showing the case of normal chain inheritance: A derived from System.Object, B is derived from A, and, indirectly, from System.Object. Even if the top of the inheritance chain wasn't the implicit base class, you would not need to list it in the inheritance list, and, due to lack of multiple inheritance, you are not allowed to do so.

    Her is the simple example:
class A { ... } // base class is System.Object
class B: A { ... } // base classes are System.Object and A
class C: B { ... } // no need to list A, it is already an indirect base







-SA


这篇关于C#中对象的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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