是否始终创建默认构造函数? [英] Is default constructor always created?

查看:67
本文介绍了是否始终创建默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个必须在实例化时接收参数的类。

换句话说,我不希望它有一个Public Sub New()。


1)如果我没有申报,VB.NET会创建一个默认的公共构造函数吗?


2)我读过我可以拥有aPrivate Sub New()这应该照顾我需要的b $ b。评论?


谢谢,

Andrew

I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?

2) I''ve read that I can have a "Private Sub New()" which should take care of
my needs. Comments?

Thanks,
Andrew

推荐答案

Andrew,
1)如果我没有声明
,VB.NET是否会创建一个默认的公共构造函数?

如果你没有声明ANY,VB.NET会创建一个默认的公共构造函数

构造函数,如果在类中定义构造函数,则不会创建默认的public

构造函数。记住构造函数不是继承的。

2)我读过我可以拥有一个Private Sub New()这应该照顾我需要的
。评论?
你只需要一个Private Sub New如果它是唯一的构造函数而你想要
阻止其他人创建该类。或者你有一个Form,Control或

组件,因为VS.NET设计师需要一个默认的构造函数,而你

不希望使用默认的构造函数。在这种情况下,我会考虑制作

它受保护。


希望这有帮助

Jay

Andrew J. Marshall <安************* @ ObjectVision.net>在消息中写道

news:e0 ************** @ TK2MSFTNGP11.phx.gbl ...我想创建一个必须在实例化时接收参数的类。
换句话说,我不希望它有一个Public Sub New()。

1)如果我不这样做,VB.NET是否会创建一个默认的公共构造函数声明
一个?
2)我读过我可以有一个Private Sub New()这应该照顾我需要的
。评论?

谢谢,
Andrew
1) Does VB.NET create a default public constructor if I do not declare one?
VB.NET creates a default public constructor if you do not declare ANY
constructors, if you define a constructor in the class, the default public
constructor is not created. Remember constructors are NOT inherited.
2) I''ve read that I can have a "Private Sub New()" which should take care of my needs. Comments? You only need a Private Sub New if its the only constructor and you want to
prevent others from creating the class. Or you have a Form, Control or
Component as the VS.NET Designers require a default constructor, and you
don''t want the default constructor from being used. I would consider making
it Protected in this case.

Hope this helps
Jay
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl... I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?
2) I''ve read that I can have a "Private Sub New()" which should take care of my needs. Comments?

Thanks,
Andrew



文章< e0 ***** *********@TK2MSFTNGP11.phx.gbl> ;, Andrew J. Marshall写道:
In article <e0**************@TK2MSFTNGP11.phx.gbl>, Andrew J. Marshall wrote:
我想创建一个必须在实例化时接收参数的类。
In换句话说,我不希望它有一个Public Sub New()。

1)如果我没有声明一个,VB.NET是否会创建一个默认的公共构造函数?


是的,除非你添加一个参数化的构造函数。在这种情况下,

编译器不会创建默认构造函数。所以基本上,所有你需要做的就是添加你想要的构造函数,并且没有默认值会创建



2)我'我读过我可以有一个Private Sub New()哪个应该照顾我的需要。评论?
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?

Yes, unless you add a parameterized constructor. In that case, the
compiler will not create a default constructor. So basically, all you
have to do is add the constructor you want, and no default will be
created.
2) I''ve read that I can have a "Private Sub New()" which should take care of
my needs. Comments?




好​​吧,除非你想在私有构造函数中做东西,否则没有

需要这样做。


-

Tom Shelton [MVP]

操作系统名称:Microsoft Windows XP Professional

操作系统版本:5.1 .2600 Service Pack 1 Build 2600

系统运行时间:0天,21小时,9分钟,45秒



Well, unless you want to do stuff in the private constructor there is no
need to do this.

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 21 Hours, 9 Minutes, 45 Seconds


AFAIK默认的空白构造函数只要您不提供任何其他构造函数,我们只会提供。如果你提供任何其他构造函数(public,

private,带参数等),那么它不会自动将默认值

一个 - 你仍然可以指定它如果你愿意,你自己。


这是VB试图向开发者隐藏的另一件事。作为一个

约定我总是手动在类中放置一个空白构造函数,如果它支持它 - 即使它只是对MyBase.New()的调用 - 只是为了清晰度。


HTH,


Trev。

" Andrew J. Marshall" <安************* @ ObjectVision.net>在留言中写道

news:e0 ************** @ TK2MSFTNGP11.phx.gbl ...
AFAIK The default blank constructor is only provided as long as you don''t
provide any other constructor. If you provide any other constructor (public,
private, with parameters etc.) then it won''t automatically put the default
one in - you can still specify it yourself if you want.

It''s yet another thing that VB tries to hide from the developer. As a
convention I always manually put a blank constructor in the class if it
supports it - even if it is only a call to MyBase.New() - just for clarity.

HTH,

Trev.
"Andrew J. Marshall" <An*************@ObjectVision.net> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
我想创建一个实例化时必须接收参数的类。
换句话说,我不希望它有一个Public Sub New()。

1)VB.NET是否创建一个默认的公共构造函数,如果我没有声明
一个?
2)我读过我可以有一个Private Sub New()这应该照顾我需要的
。评论?

谢谢,
Andrew
I want to create a class that must receive a parameter when instantiated.
In other words, I do not want it to have a "Public Sub New()".

1) Does VB.NET create a default public constructor if I do not declare one?
2) I''ve read that I can have a "Private Sub New()" which should take care of my needs. Comments?

Thanks,
Andrew



这篇关于是否始终创建默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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