强制实施构造函数 [英] Force Implementation of a Constructor

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

问题描述

我知道可以要求继承类来实现特定的

函数或属性,但是是否可以要求继承类来实现a / b
它的构造函数是什么?


谢谢,

史蒂夫

解决方案

< blockquote> Steve - DND< ng@digitalnothing.com>写道:

我知道可以要求继承类来实现特定的
函数或属性,但是可以要求继承类来实现构造函数这是自己的吗?




不,我不敢。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


< blockquote>史蒂夫,

不,没有办法吗?

除非你只声明基类的受保护构造函数。


class Foo

{

protected Foo()

{

}

}


现在,如果我想继承这个类并且能够实例化对象,那么
必须至少声明一个公共构造函数。但是我不能强迫

继承者用特定的原型声明构造函数(因为我可以使用

抽象方法)。


class Bar:Foo

{

公共酒吧()

{

}

}


您需要什么?我不能想到任何有用的应用。


HTH

B \ rgds

100

" Steve - DND" < ng@digitalnothing.com>在消息中写道

新闻:uL ************** @ TK2MSFTNGP09.phx.gbl ...

我知道''可能需要继承类来实现
特定的函数或属性,但是是否可以要求继承类来实现它自己的构造函数?

谢谢,
史蒂夫



100< 10*@100.com>写道:


< snip>

你需要什么?我无法想到任何有用的应用程序。




我可以,非常容易:插件系统。基本上,在任何

插件框架中,你需要在编译时实例化一个你不知道的类型。无论如何,你将不得不用反射实例化它,但是如果插件作者出现错误,那将会有所帮助。

写入类型如果他们没有''提供一个合适的构造函数。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


I know it''s possible to require inheriting classes to implement a particular
function or property, but is it possible to require a inheriting class to
implement a constructor of it''s own?

Thanks,
Steve

解决方案

Steve - DND <ng@digitalnothing.com> wrote:

I know it''s possible to require inheriting classes to implement a particular
function or property, but is it possible to require a inheriting class to
implement a constructor of it''s own?



No, I''m afraid not.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi Steve,
No, there is no way?
Unless if you declare only protected constructors for the base class.

class Foo
{
protected Foo()
{
}
}

Now if I want to inherit from this class and be able to instatiate objects I
have to declare at least one public constructor. But I cannot force the
inheritors to declare constructor with specific prototype (as I can do with
abstract methods).

class Bar: Foo
{
public Bar()
{
}
}

What do you need that for? I can''t think of any useful application of this.

HTH
B\rgds
100

"Steve - DND" <ng@digitalnothing.com> wrote in message
news:uL**************@TK2MSFTNGP09.phx.gbl...

I know it''s possible to require inheriting classes to implement a particular function or property, but is it possible to require a inheriting class to
implement a constructor of it''s own?

Thanks,
Steve



100 <10*@100.com> wrote:

<snip>

What do you need that for? I can''t think of any useful application of this.



I can, very easily: plug-in systems. Basically, sooner or later in any
plug-in framework you need to instantiate a type which you don''t know
about at compile-time. You''ll have to instantiate it with reflection
anyway, but it would help if the plug-in author got an error when
writing the type if they didn''t provide an appropriate constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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