构造函数的继承。 [英] Inheritance of constructors.
问题描述
大家好。
我正面临以下建设:
class ClOld
{
public ClOld(string a){...}
}
class ClNew:ClOld
{
SomeData [] arr;
public ClNew(string a,SomeObject b):base(a)
{
arr = new SomeData [] {b.data1,b.data2,b.data3};
}
}
>
现在,我想知道如何将初始化的arr传递给基础构造函数。为什么
我不能编写如下代码:
class ClOld
{
public ClOld(string a,ref SomeData [] arr){...}
}
class ClNew:ClOld
{
SomeData [] arr;
public ClNew(string a,SomeObject b):
{
arr = new SomeData [] {b.data1,b.data2,b.data3};
base(a,ref arr);
}
}
提前致谢,
Pete
Hi all.
I''m facing the following construction:
class ClOld
{
public ClOld(string a) { ... }
}
class ClNew: ClOld
{
SomeData[] arr;
public ClNew(string a, SomeObject b): base(a)
{
arr = new SomeData[] {b.data1, b.data2, b.data3 };
}
}
Now, I wonder how to pass initialized arr to a base constructor. Why
can''t I code something like below:
class ClOld
{
public ClOld(string a, ref SomeData[] arr) { ... }
}
class ClNew: ClOld
{
SomeData[] arr;
public ClNew(string a, SomeObject b):
{
arr = new SomeData[] {b.data1, b.data2, b.data3 };
base(a, ref arr);
}
}
Thanks in advance,
Pete
推荐答案
6月30日,12日:17 * pm,A ngler< p | k | o | n | i | u | ... @ hotmail.c_o_m>
写道:
On Jun 30, 12:17*pm, A n g l e r <p|k|o|n|i|u|...@h-o-t-m-a-i-l.c_o_m>
wrote:
我正面临以下结构:
I''m facing the following construction:
< snip>
<snip>
现在,我想知道如何将初始化的arr传递给基础构造函数。为什么
我不能编码如下:
Now, I wonder how to pass initialized arr to a base constructor. Why
can''t I code something like below:
< snip>
基本上,在基础构造函数调用之前,你不能在构造函数中编写任何实际代码。你*可以*进行静态方法调用,所以你可以这样做
:
public ClNew(string a):base(a,CreateArray())
{
//其他东西
}
这对你没有帮助但是,参数是一个参数。你确定你在构造函数调用中需要一个ref参数
吗?这是非常不寻常的。
Jon
<snip>
Basically you can''t write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:
public ClNew(string a) : base(a, CreateArray())
{
// Other stuff
}
That won''t help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That''s pretty unusual.
Jon
基本上你可以在
基础构造函数调用之前,不要在构造函数中编写任何实际代码。你*可以*进行静态方法调用,所以你
可以做:
Basically you can''t write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:
很棒,我是这么认为的。垃圾收集器的弊端和
整个自动内存管理由ms,对吗? :/
Brilliant, I thought so. Blooming drawbacks of the garbage collector and
the whole automated memory management by ms, right? :/
public ClNew(string a):base(a,CreateArray())
{
//其他东西
}
public ClNew(string a) : base(a, CreateArray())
{
// Other stuff
}
这对现在有帮助:)
That will help for now :)
$ b但是,b不能帮助你获得ref参数。你确定你在构造函数调用中需要一个ref参数
吗?这很不寻常。
That won''t help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That''s pretty unusual.
不,我想我不是因为我只传递了一系列物品,无论如何
的意思是(我猜是这样的)它以类似的方式处理。不是吗?
DataGridView [] arr = new DataGridView [] {grid1,grid2,grid3};
干杯,
P.
No, I guess I don''t cos I pass just an array of objects which anyway
means (I guess so) it''s handled in a similar way to reference. Isn''t it?
DataGridView[] arr = new DataGridView[] { grid1, grid2, grid3 };
Cheers,
P.
6月30日,1:43 * pm,A ngler< p | k | o | n | i | u | ... @ hotmail.c_o_m>
写道:
On Jun 30, 1:43*pm, A n g l e r <p|k|o|n|i|u|...@h-o-t-m-a-i-l.c_o_m>
wrote:
基本上你可以'在
基础构造函数调用之前,不要在构造函数中编写任何实际代码。你*可以*进行静态方法调用,所以你可以这样做:
Basically you can''t write any actual code in the constructor before
the base constructor call. You *can* make a static method call, so you
can do:
很棒,我是这么认为的。垃圾收集器的弊端和
整个自动内存管理由ms,对吗? :/
Brilliant, I thought so. Blooming drawbacks of the garbage collector and
the whole automated memory management by ms, right? :/
嗯,更好的做法是在对象内部进行操作之前在父对象中初始化
有点可疑。
< snip>
Well, it''s more that doing things within an object before it''s been
initialized in the parent is somewhat questionable.
<snip>
那不能帮到你但是,对于ref参数。你确定你在构造函数调用中需要一个ref参数
吗?这很不寻常。
That won''t help you for a ref parameter, however. Are you sure you
need a ref parameter in the constructor call? That''s pretty unusual.
不,我想我不是因为我只传递一系列物品,无论如何
的意思是(我猜是这样的)它以类似的方式处理。不是吗?
DataGridView [] arr = new DataGridView [] {grid1,grid2,grid3};
No, I guess I don''t cos I pass just an array of objects which anyway
means (I guess so) it''s handled in a similar way to reference. Isn''t it?
DataGridView[] arr = new DataGridView[] { grid1, grid2, grid3 };
按参考传递和按参考传递之间存在很大差异并且按价值传递
参考。它让很多人感到困惑,并且很好地意识到了差异。它主要归结为令人困惑的是,b $ b命名术语,IMO。
见 http://pobox.com/~skeet/csharp/parameters.html
Jon
There''s a big difference between "pass by reference" and "pass
reference by value". It confuses quite a lot of people, and it''s well
worth being aware of the difference. It''s mostly down to confusingly
named terminology, IMO.
See http://pobox.com/~skeet/csharp/parameters.html
Jon
这篇关于构造函数的继承。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!