Microsoft如何使所有的.NET类隐式地从Object类继承?我可以做同样的事情? [英] How does Microsoft make all .NET classes implicitly inherit from the Object class? Can I do the same kind of thing?

查看:184
本文介绍了Microsoft如何使所有的.NET类隐式地从Object类继承?我可以做同样的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们创建从Object类继承,而无需显式声明继承的类中的所有类。

All classes that we create inherit from the Object class without needing to explicitly declare inheritance to that class.

  • Microsoft如何可以使所有的.NET类隐式地从继承 Object类?
  • 在该编译器的后面注入的继承 场景?
  • 我可以做同样的事情:让我所有的课 从一个类的接口,例如继承含蓄?
  • How does Microsoft can make all .NET class implicitly inherit from the Object class?
  • Does the compiler inject that inheritance behind the scenes?
  • Can I do the same kind of thing: make all my classes inherit implicitly from a class interface for example?

我的问题可能是模糊的,所以我会澄清:

My question may be ambiguous so I will clarify:

我不要求有关从另一个类比其他对象继承,因为我知道,多重继承的类是被禁止的。我说的是继承的,而通过类接口,我的问题是有关的隐式做它不是由其他类我自己的替代对象,或做多重继承的对象和其他类我自己的。我想所有的类继承的形式Interface1OfMyOwn和Interface2OfMyOwn。

I'm not asking about inheriting from another class other than Object since I know that multiple inheritance for classes is forbidden. I'm talking about inheritance rather through class Interfaces, my question is about IMPLICITLY DOING IT not about substituting an object by another class of my own, or do multiple inheritance with Object and another class of my own. I want to get all classes inherit form Interface1OfMyOwn and Interface2OfMyOwn.

推荐答案

所有的类都隐含地扩展对象因为C#标准规定的语言,所以。

All classes implicitly extend Object because the C# standard specifies the language so.

问他们是如何实现的就是喜欢问他们如何实现这样一个事实:无效方法不返回数据,或者他们是如何实现的一个事实,即类具有的属性:它只是在某处编译器(这这里可能是C#编译器或JIT编译器)的code。我们不能肯定知道,但它是合理地说,每当编译器不会检测到显式继承一个类,它只是为 System.Object的,而不是简单作为。所以,你必须接受对象是一个简单的特殊类型作为编译器。

Asking how they implemented that is like asking how they implemented the fact that a void method doesn't return data, or how they implemented the fact that classes have properties: It's simply somewhere in the code of the compiler (which here may be the C# compiler or the JIT compiler). We can't know for sure, but it's plausible to say that whenever the compiler doesn't detect explicit inheritance in a class, it just goes for System.Object instead, simple as that. So you'll have to accept that Object is simply a special type for the compiler.

为什么你不能做到这一点与自定义类似乎很明显:这将彻底混淆编译过的类没有明确继承是否应该延长对象或您的自定义类。如果选择后者,每类没有明确的继承,这将引入一个编译错误,在大多数情况下,或者,如果你特别不走运,令人惊讶的行为只体现在运行时。如果选择前者,你必须明确禁止类中的这一点,有什么意义?

Why you can't do that with a custom class seems obvious: It would thoroughly confuse the compiler over whether a class with no explicit inheritance should extend Object or your custom class. If it chose the latter for every class with no explicit inheritance, it would introduce compilation errors in most cases or, if you're particularly unlucky, surprising behavior that only manifests at runtime. If it chose the former and you'd have to explicitly opt-in your classes for this, what's the point?

(同样的,当然,如果你想隐含实现一个接口会发生:所有的类,并没有真正实现该接口将打破,并导致编译错误或者,如果你运气不好,该接口将匹配一类无关的方法,碰巧有一个匹配的签名,并导致奇怪的行为,你会了解被测试。)

(The same, of course, would happen if you wanted to implicitly implement an interface: All classes that don't really implement that interface would break and cause compilation errors. Or, if you're unlucky, the interface would match unrelated methods in a class that happen to have a matching signature and cause strange behavior that you'd find out about by testing.)

这篇关于Microsoft如何使所有的.NET类隐式地从Object类继承?我可以做同样的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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