问题描述
有没有办法从方法继承?我不想在重载版本中一遍又一遍地重复我的方法中的代码。如果我需要
进行更改,我不想在我的方法的每个超载
版本中重新加载它们。
更好的方法吗?
谢谢,
Paul
Is there a way to Inherit from a Method? I don''t want to have to repeat
the code in my method over and over for the overloaded versions. If I need
to make changes I don''t want to have to reapeat them in each overloaded
version of my method.
Better way to do this?
Thank you,
Paul
推荐答案
通常的方法是进行一次重载,其中包含您可能需要的所有信息,并从其他方法调用,传递在
参数中。例如:
公共对象LookupItem(对象键,对象defaultValue)
{
...执行查找.. 。
}
//与LookupItem(key,defaultValue)相同,但默认值为
// null
公共对象LookupItem(对象键)
{
返回LookupItem(键,null);
}
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复群组,请不要邮寄我也是
The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:
public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}
// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
通常的方法是使一个重载具有你可能需要的所有信息
,并从其他方法调用它,传递在
参数中。例如:
公共对象LookupItem(对象键,对象defaultValue)
{
...执行查找.. 。
}
//与LookupItem(key,defaultValue)相同,但默认值为
// null
公共对象LookupItem(对象键)
{
返回LookupItem(键,null);
}
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复群组,请不要邮寄我也是
The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:
public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}
// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
通常的方法是进行一次重载,其中包含您可能需要的所有信息,并从其他方法调用,传递在
参数中。例如:
公共对象LookupItem(对象键,对象defaultValue)
{
...执行查找.. 。
}
//与LookupItem(key,defaultValue)相同,但默认值为
// null
公共对象LookupItem(对象键)
{
返回LookupItem(键,null);
}
-
Jon Skeet - < sk *** @ pobox.com>
博客:
如果回复群组,请不要邮寄我也是
The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:
public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}
// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
这篇关于过载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!