是否有可能重写在C#构造函数? [英] Is it possible to override a constructor in C#?

查看:287
本文介绍了是否有可能重写在C#构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能重写基类的构造函数在派生类中?

Is it possible to override the constructor of the base class in the derived class?

如果是这样,怎么能完成以及在什么情况下使用这将是可行?如果不是,为什么不呢?

If so, the how can it be accomplished and in what use case would this be practical? If not, why not?

推荐答案

没有,你不能覆盖的构造。这个概念使得在C#中没有任何意义,因为根本构造并不多态调用。你总是说出你想要哪一个类来构建,并给构造函数的参数。

No, you can't override constructors. The concept makes no sense in C#, because constructors simply aren't invoked polymorphically. You always state which class you're trying to construct, and the arguments to the constructor.

构造函数不能被继承在所有 - 但派生类必须链要么在同一个类的另一个构造函数,或在基类构造函数中的一个所有构造函数。如果你不这样做明确,则编译器隐链的基类的参数的构造函数(如果该构造函数不存在或无法访问出现错误)。

Constructors aren't inherited at all - but all constructors from a derived class must chain either to another constructor in the same class, or to one of the constructors in the base class. If you don't do this explicitly, the compiler implicitly chains to the parameterless constructor of the base class (and an error occurs if that constructor doesn't exist or is inaccessible).

这篇关于是否有可能重写在C#构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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