简体   繁体   English

从system.object派生的C#类型

[英]C# types derived from system.object

As I understand, C# support Multiple inheritance using Interfaces indeirectly. 据我所知,C#支持使用Interfaces进行多重继承。 When I was going through CLR via C# book, I got a query on this. 当我通过C#book通过CLR时,我得到了一个查询。 The book says 这本书说

The runtime requires every type to ultimately be derived from the System.Object type. 运行时要求每个类型最终都是从System.Object类型派生的。 This means that the following two type definitions are identical: 这意味着以下两种类型定义是相同的:

// Implicitly derived from Object
class Employee {
....
}

// Explicitly derived from Object
class Employee : System.Object {
...
}

If this is the correct statement, can the code mentioned below be true? 如果这是正确的陈述,下面提到的代码是真的吗?

// Implicitly derived from Object
class SoftwareEngineer : Employee 
{
....
}

// Explicitly derived from Object
class SoftwareEngineer : Employee, System.Object {
...
}

You cannot do this, because multiple inheritance from classes is not allowed: 你不能这样做,因为不允许从类中继承多个:

class SoftwareEngineer : Employee, System.Object

Line above will give you compilation error: 上面的行会给你编译错误:

'SoftwareEngineer' cannot have multiple base classes: 'Employee' and 'System.Object' 'SoftwareEngineer'不能有多个基类:'Employee'和'System.Object'

But thus Employee implicitly inherits from System.Object that means SoftwareEngineer also will be inherited from System.Object (you can think of it as 'vertical' inheritance): 但是因此Employee隐式继承自System.Object ,这意味着SoftwareEngineer也将继承自System.Object (您可以将其视为'垂直'继承):

class Employee : System.Object

class SoftwareEngineer : Employee

It's a frequent misconception, but interface implementation is completely different from inheritance. 这是一种常见的误解,但接口实现与继承完全不同 Other than they look similar they have nothing in common. 除了看起来相似之外,它们没有任何共同点。

Interface implementation happens at compile-time and basically says "The implementing class must contain certain member signatures" . 接口实现在编译时发生,基本上说“实现类必须包含某些成员签名” Inheritance on the contrary is dynamic runtime behavior which is realized using a Virtual Method Table . 相反,继承是使用虚拟方法表实现的动态运行时行为。

So implementing multiple interfaces is no problem, while multiple inheritance is forbidden. 因此,实现多个接口没有问题,而禁止多重继承。

You can't inherit from more than one class in c#. 您不能从c#中的多个类继承。

So second is false. 所以第二个是假的。

Employee inherits from Object SoftwareEngineer inherits from Employee Employee继承自Object SoftwareEngineer继承自Employee

But SoftwareEngineer can access visible methods from Engineer AND visible methods from Object (like ToString for example) 但是SoftwareEngineer可以从工程师和Object中的可见方法访问可见方法(比如ToString

The runtime requires every type to ultimately be derived from the System.Object type 运行时要求每个类型最终都是从System.Object类型派生的

In your second example, SoftwareEngineer inherits from Employee , which in turn, inherits from Object The second declaration is illegal because C# does not allow multiple inheritance (implementing multiple interfaces is a different concept), but it in effect its still inheriting from Object because it's base type does. 在你的第二个例子, SoftwareEngineer从继承Employee ,这反过来,从继承Object第二个声明是非法的,因为C# 不允许多重继承(实现多个接口是不同的概念),但实际上它仍然继承Object ,因为它是基类型。

No. 没有。

Employee class derives from System.Object. Employee类派生自System.Object。 So indirectly SoftwareEngineer derives from System.Object. 所以间接的SoftwareEngineer派生自System.Object。

// Explicitly derived from Object
class SoftwareEngineer : Employee, System.Object {
...
}

This will give you syntax error. 这会给你语法错误。 Even if there is some other class it is not possible. 即使有其他课程也不可能。 The hierarchy is one class will derive from father, and father will driver from grand father and so on. 层次结构是一个类派生自父亲,父亲将驱动程序来自祖父等等。

At first, C# *doesn't support multiple inheritanc*e, so 首先,C#*不支持多个inheritanc * e,所以

  class SoftwareEngineer : Employee, System.Object { ... } // <- Compile time error

The phrase that "every type to ultimately be derived from the System.Object type" means that every heritage chain should end at Object, eg “最终从System.Object类型派生的每种类型”这一短语意味着每个遗产链应该以Object结尾,例如

  SafeHandleMinusOneIsInvalid // <- inherited from 
  SafeHandle                  // <- which in turn inherited from
  CriticalFinalizerObject     // <- which finally inherited from
  Object                      // <- The ultimate base class

Interfaces are not classes ; 接口不是类 ; they are kind of contracts, eg 他们是一种合同,例如

  public class MyVersion: 
    IComparable<MyVersion>,  // <- I guarantee, there's "int CompareTo(MyVersion other)" method
    ICloneable,              // <- I guarantee, there's "Object Clone()" method
  ...

This, if permitted 如果允许的话

class SoftwareEngineer : Employee, System.Object { ... }

Would essentially be the same as: 基本上与以下相同:

class SoftwareEngineer : Employee { ... }

..since an Employee is implicitly, an Object anyway (but you cant use that form anyway). ..因为一个员工隐含地,一个对象无论如何(但你无论如何都不能使用那个形式)。

You could do something like the following with interfaces: 您可以通过接口执行以下操作:

// Implements IEmployee, and an IPerson (interfaces), 
// while inheriting from System.Object:
class SoftwareEngineer : System.Object, IEmployee, IPerson { ... }

..but the System.Object here would still be superfluous, since again - SoftwareEngineer would always implicitly be an Object anyway. ..但是System.Object在这里仍然是多余的,因为再次 - SoftwareEngineer总是隐式地成为一个Object

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

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