关于继承和构造函数的问题。 [英] Question about inheritance and constructors.

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

问题描述

我搜索了新闻组但找不到这个问题的答案,所以

我会继续发布它。


让我说我有一个A级,有几个不同的构造者......没什么''

特别。


现在,让我们说我有B级继承自A. B自动

获得A的所有属性和方法,但我无法利用任何

A'的构造函数而无需重新定义他们在B和委托。我确定

有一个很好的理由,但我发现它有点令人沮丧。


是否有一些语法我不熟悉的镜头

利用A构造函数而不必在B级镜像它们?


非常感谢。


-Kevin Buchan

I searched the news group and could not find an answer to this question, so
I''ll go ahead and post it.

Let''s say I have a class A with a couple different constructors... nothin''
special.

Now, let''s say that I have class B that inherits from A. B automagically
gets all of the properties and methods of A, but I cannot leverage any of
A''s contructors without redefining them on B and delegating. I''m sure that
there is a very good reason for this, but I find it a bit frustrating.

Is there, perhaps, some syntax shotcut that I''m not familiar with for
leveraging the A constructors without having to mirror them on class B?

Thanks so much.

-Kevin Buchan

推荐答案

IIRC,构造函数不是继承的。你的派生类必须使用MyBase关键字调用

基类的构造函数:


Public Sub New()

' '调用基类构造函数

MyBase.New()

结束子

IIRC, Constructors are not inherited. Your derived classes must call
the constructors of the base class using the MyBase keyword:

Public Sub New()
''Call base class constructor
MyBase.New()
End Sub


4月20日2005 06:32:59 -0700,Chris Dunaway < du ****** @ gmail.com>

写道:
On 20 Apr 2005 06:32:59 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
IIRC,构造函数不是继承的。派生类必须使用MyBase关键字调用基类的构造函数:

Public Sub New()
''调用基类构造函数
MyBase.New ()
End Sub
IIRC, Constructors are not inherited. Your derived classes must call
the constructors of the base class using the MyBase keyword:

Public Sub New()
''Call base class constructor
MyBase.New()
End Sub



是的,这就是我一直在做的事情,但当你的基类有三个

构造函数时在

多个子类中复制和委托是一件令人讨厌的事。


Yup, that''s what I''ve been doing, but when your base class has three
constructors, it''s a bit of an annoyance to replicate and delegate in
multiple subclasses.




Jason Kendall写道:

Jason Kendall wrote:
2005年4月20日06:32:59 -0700,Chris Dunaway < du ****** @ gmail.com>
写道:
On 20 Apr 2005 06:32:59 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
IIRC,构造函数不是继承的。你的派生类必须
使用MyBase关键字调用基类的构造函数:

Public Sub New()
''调用基类构造函数
MyBase.New( )
End Sub
IIRC, Constructors are not inherited. Your derived classes must callthe constructors of the base class using the MyBase keyword:

Public Sub New()
''Call base class constructor
MyBase.New()
End Sub



是的,这就是我一直在做的事情,但是当你的基类有三个
构造函数时,它就是在多个子类中复制和委托有点烦恼。


Yup, that''s what I''ve been doing, but when your base class has three
constructors, it''s a bit of an annoyance to replicate and delegate in
multiple subclasses.




我想如果有某种向导那就好了

自动引入给定基类方法的源,其中

可用。关于VS加载项的想法也许。


但我相信你可以看到为什么构造函数不会自动*

继承 - 派生类有-something-比基类更多,

因此几乎总是希望构造函数比基本的

类构造函数更多。在派生类的所有行为中,与其父类相比,实例化在

行为列表中肯定会有所不同。

-

Larry Lard

回复团体请



I suppose it would be nice if there were some kind of Wizard that
automatically brought in the source of a given base class method, where
available. An idea for a VS add-in maybe.

But I''m sure you can see why constructors aren''t *automatically*
inherited - a derived class has -something- more than the base class,
so will almost always want constructors that do more than the base
class constructors. Of all the behaviours of a derived class as
compared to its parent, instantiation is surely high on the list of
behaviours that will differ.

--
Larry Lard
Replies to group please


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

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