构造函数和继承 [英] Constructors and Inheritance

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

问题描述

让我们在C#中的例子

public class Foo
{
    public Foo() { }
    public Foo(int j) { }
}

public class Bar : Foo
{

}

现在,所有foo的公众成员,除了构造函数中栏访问。
我不能做这样的事情。

Now, All the public members of Foo is accessible in Bar except the constructor. I cannot do something like

Bar bb = new Bar(1);



为什么构造函数是不可继承?

Why the constructors are not inheritable?

更新

UPDATE

我不明白,我们可以链的构造,但我想知道为什么上面的结构是无效。我相信应该有一个有效的原因。

I do understand we can chain constructors, but I would like to know why the above construct is not valid. I am sure there should be a valid reason for it.

推荐答案

构造函数是不可继承,因为它可能会导致奇怪的和非预期的行为。更具体地说,如果您添加一个新的构造基类,所有派生类得到构造的一个实例。这是在某些情况下是一件坏事,因为也许你的基类参数指定不以适合您的派生类。

Constructors are not inheritable because it might cause weird and unintended behavior. More specifically, if you added a new constructor to a base class, all derived classes get an instance of that constructor. That's a bad thing in some cases, because maybe your base class specifies parameters that don't make sense for your derived classes.

一个普遍给出的这个例子是,在许多语言中,所有对象的基类(俗称对象)具有无参数的构造函数。如果构造函数被继承,这将意味着,所有的对象都具有一个参数的构造函数,而且也没有办法说我希望人们谁使这个类的一个实例提供参数X,Y和Z,否则他们的代码不应该编译。 对于很多类,但重要的是某些参数为他们应有的功能定义,而将构造非遗传是那类作家可以保证一些参数总是被定义方式的一部分。

A commonly given example for this is that in many languages, the base class for all objects (commonly called "Object") has a constructor with no parameters. If constructors were inherited, this would mean that all objects have a parameterless constructor, and there's no way to say "I want people who make an instance of this class to provide parameters X, Y and Z, otherwise their code shouldn't compile." For many classes, it's important that certain parameters be defined for their proper function, and making constructors non-heritable is part of the way that class authors can guarantee that some parameters are always defined.

编辑回应评论:拉梅什指出,如果构造函数被继承,因为他想他们是,他总是可以覆盖每个派生类使用私有声明构造函数的基类构造函数。这是千真万确的,但也有它的后勤问题,这一策略。它要求派生类的作家必须密切关注基类,并添加一个私有构造函数,如果他们想阻止基类构造函数的继承。这不仅是很多人写派生类的工作,这种隐含的依赖跨类正是诸如此类的事情,可能会导致怪异的行为。

Edit to respond to comments: Ramesh points out that if constructors were inherited as he would like them to be, he could always override base class constructors using privately declared constructors in each derived class. That is certainly true, but there it a logistical problem with this strategy. It requires that writers of derived classes have to watch base classes closely and add a private constructor if they want block inheritance of the base class constructor. Not only is this a lot of work for people writing derived classes, this kind of implicit dependency across classes is exactly the sort of thing that can cause weird behavior.

拉梅什 - 这并不是说你描述是无法加入到语言的内容。一般来说它没有这样做,因为那种行为可能会让人混淆,导致很多额外的调试和代码编写的。

Ramesh - it's not that what you describe would be impossible to add to a language. In general it's not done because that sort of behavior could confuse people and lead to a lot of extra debugging and code writing.

昆廷·罗宾逊提供了在绝对值得一读的评论这个问题,一些非常值得回应。

Quintin Robinson provides some very worthwhile responses to this question in the comments that are definitely worth reading.

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

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