简体   繁体   English

C#中的单一继承 - 对象类?

[英]Single Inheritance in C# - object class?

I have been asking myself this question for a long time now. 我很久以来一直在问自己这个问题。 Thought of posting it. 想发布它。 C# doesn't support Multiple Inheritance(this is the fact). C#不支持多重继承(这是事实)。 All classes created in C# derive out of 'Object' class(again a fact). 在C#中创建的所有类都派生出'Object'类(同样是一个事实)。

So if C# does not support Multiple inheritance, then how are we able to extend a class even though it already extends Object class? 因此,如果C#不支持多继承,那么即使它已经扩展了Object类,我们如何才能扩展一个类?

Illustating with an example: 用一个例子说明:

  1. class A : object - Class A created. A类:对象 - 创建的A类。
  2. class B : object - Class B created. B类:对象 - 创建B类。
  3. class A : B - this again is supported. A类:B - 再次支持。 What happens to the earlier association to object. 早期与对象的关联会发生什么。

We are able to use object class methods in A after step 3. So is the turned to multi level inheritance. 我们能够在步骤3之后在A中使用对象类方法。因此转向多级继承。 If that is the case, then 如果是这样的话,那么

  1. class A : B A类:B
  2. class C : B C级:B
  3. class A : C - I must be able to access class B's methods in A. Which is not the case? A类:C - 我必须能够在A中访问B类的方法。事实并非如此?

Can anyone please explain? 有人可以解释一下吗?

You're confusing mutliple inheritance with an inheritance tree. 你将多重继承与继承树混淆了。 You can inherit from something other than Object. 您可以继承Object之外的其他内容。 It's just that Object is sitting way up there at the top of your tree. 只是Object坐在你树顶的那里。 And someone can inherit your class, but because Object is still up there at the top that class will also inherit from object. 有人可以继承你的类,但是因为Object仍然位于顶层,所以该类也将继承自object。 Your "Multi-level" inheritance is not multiple inheritance. 您的“多级”继承不是多重继承。

Multiple inheritance is when you inherit from two different trees, and .Net actually does support this after a fashion via interfaces. 多重继承是从两个不同的树继承而来的.Net实际上通过接口来支持这种。

Joel's answer is correct. 乔尔的回答是正确的。 There is a difference between multiple inheritance and an inhertance tree (or derivation chain). 多继承和inhertance树(或派生链)之间存在差异。 In your example, you actually show an inhertance tree: One object inherits (derives) from another object higher in the tree. 在您的示例中,您实际显示了一个inhertance树:一个对象从树中较高的另一个对象继承(派生)。 Multiple inheritance allows one object to inherit from multiple base classes. 多重继承允许一个对象从多个基类继承。

Take, for example, the following tree: 举例来说,以下树:

public class BaseClass { }

public class SpecialBaseClass : BaseClass {}

public class SpecialtyDerivedClass : SpecialBaseClass {}

This is perfectly valid and says that SpecialtyDerivedClass inherits from SpecialBaseClass (SpecialtyDerivedClass' parent) which, in turn, derives from BaseClass (SpecialtyDerivedClass' grandparent). 这是完全有效的,并且说SpecialtyDerivedClass继承自SpecialBaseClass(SpecialtyDerivedClass'父级),而SpecialBaseClass又来自BaseClass(SpecialtyDerivedClass'祖父母)。

Under the idea of multiple inheritance, the example would look like this: 在多重继承的想法下,示例如下所示:

public class BaseClass { }

public class SpecialBaseClass {}

public class SpecialtyDerivedClass : BaseClass, SpecialBaseClass {}

This is not allowed in .NET, but it says that SpecialityDerivedClass inherits from both BaseClass and SpecialBaseClass (which are both parents). 这在.NET中是不允许的,但它表示SpecialityDerivedClass继承自BaseClass和SpecialBaseClass(它们都是父类)。

.NET does allow a form of multiple inheritance by allowing you to inherit from more than one interface. .NET允许您从多个接口继承,从而允许一种多重继承形式。 Changing the example above slightly: 稍微改变上面的例子:

public class BaseClass { }

public interface ISpecialBase {}

public interface ISpecialDerived {}

public class SpecialtyDerivedClass : BaseClass, ISpecialBase, ISpecialDerived {}

This says that SpecialtyDerivedClass inherits from BaseClass (it's parent) and also ISpecialBase and ISpecialDerived (also parent's but more like step-parents as interfaces can't specify functionality). 这表示SpecialtyDerivedClass继承自BaseClass(它的父级)以及ISpecialBase和ISpecialDerived(也是父级的,但更像是步骤父级,因为接口无法指定功能)。

All classes ultimately derive from Object. 所有类最终都来自Object。

public class A

is implicitly equivalent to 隐含地等价于

public class A : System.Object

When you derive from another class 当你从另一个类派生时

public class A : B

where 哪里

public class B : System.Object

B becomes the parent class, and Object becomes the grandparent class. B成为父类,Object成为祖父类。

And so on. 等等。

So it is the parent, grandparent, great-grandparent (etc) class of all other classes. 因此它是所有其他类的父,祖父母,曾祖父母(等)类。

一种看待它的方法是:C#有一个继承 ,而C ++(或其他多重继承语言)有一个继承

Given below. 如下所示。

public class A : B
{

}

public class B : C
{
    public int BProperty { get; set; }
}

public class C
{
    public int CProperty { get; set; }
}

public class Test
{
    public void TestStuff()
    {
        A a = new A();

        // These are valid.
        a.CProperty = 1;
        a.BProperty = 2;
    }

}

This is valid. 这是有效的。 Object is a base for C in this case. 在这种情况下,Object是C的基础。

In the example, the reason that B can extend A is because A extends Object. 在该示例中,B可以扩展A的原因是因为A扩展了Object。 A class can only specify one parent class, but that class must either be object or have object as one of its ancestors. 类只能指定一个父类,但该类必须是对象或具有对象作为其祖先之一。

A class inherits from object if you do not specify a base class . 如果未指定基类,则类继承自object。 Thus: 从而:

class C {}

is the same as 是相同的

class C : Object {}

However, if you specify a base class, it will inherit from that class instead of Object . 但是, 如果指定基类,它将从该类而不是Object继承 Thus, 从而,

class B : C {}

B directly inherits from C instead of Object. B直接继承自C而不是Object。 Another example, 另一个例子,

class A : B {}

In this case, A inherits from B instead of Object. 在这种情况下,A继承自B而不是Object。 To summarize, in this hierarchy: 总结一下,在这个层次结构中:

class C {}
class B : C {}
class A : B {}

Class A derives from B, which derives from C. So Class A is indirectly derived from C because B is derived from C. C also derived from Object which in not explicitly specified but it is there by default. A类派生自B,它派生自C.因此,A类间接地从C派生,因为B派生自C. C也派生自Object,未明确指定但默认存在。 So A is indirectly derived from Object too. 所以A也间接地来自Object。

A class in C# can only have one parent, but it can have multiple ancestors. C#中的一个类只能有一个父类,但它可以有多个祖先。 You can implement multiple interfaces, but that only means that your class agrees to implement the signatures defined by those interfaces. 您可以实现多个接口,但这仅表示您的类同意实现这些接口定义的签名。 You don't actually inherit any functionality from those interfaces. 您实际上并未从这些接口继承任何功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM