您的位置:首页 > 其它

class中对实例字段进行初始化调用构造方法的问题

2005-09-11 00:21 801 查看
1class SomeType {
2class SomeType {
2Int32 x;
3String s;
4Double d;
5Byte b;
6// This constructor must be called by all the other constructors.
7// This constructor contains the code to initialize the fields.
8public SomeType() {
9x = 5;
10s = "Hi There!";
11d = 3.14159;
12}
13// This constructor calls the default constructor first.
14public SomeType(Int32 x) : this() {
15this.x = x;
16}
17// This constructor calls the default constructor first.
18public SomeType(String s) : this() {
19this.s = s;
20}
21}

我的想法是:
这个class的定义中依然有三个sometype构造方法,只是一开始的时候,没有对x,s,d进行赋值初始化.而且后面两个sometype构造方法有对无参sometype构造方法的继承.
但是这样,在三个重载的构造方法中,在每个方法的开始处,编译器不是也要包括初始化x,s,d的一份代码吗?不是照样形成代码的臃肿了吗??

请问,按照后面一种class的定义,是不是也会同时包括一份对x,s,d的初始化代码呢??
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐