且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

过载方法

更新时间:2023-02-16 21:27:37

Soli3d< So **** @ discussion.microsoft.comwrote:
Soli3d <So****@discussions.microsoft.comwrote:

有没有办法从方法继承?我不想在重载版本中一遍又一遍地重复我的方法中的代码。如果我需要

进行更改,我不想在我的方法的每个超载

版本中重新加载它们。


更好的方法吗?
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?



通常的方法是进行一次重载,其中包含您可能需要的所有信息,并从其他方法调用,传递在

参数中。例如:


公共对象LookupItem(对象键,对象defaultValue)

{

...执行查找.. 。

}


//与LookupItem(key,defaultValue)相同,但默认值为

// null

公共对象LookupItem(对象键)

{

返回LookupItem(键,null);

}


-

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

如果回复群组,请不要邮寄我也是

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


Jon,


非常感谢。如果我想

将不同的类型传递给main方法,这将如何工作? Int16 vs

双倍?我是否必须将主方法设为对象

并根据需要进行投射?


干杯,

Paul


" Jon Skeet [C#MVP]"写道:
Jon,

Thank you much. How would this work if I wanted
to pass different types to the main method? Int16 vs
Double? Do I have to make the main method an Object
and cast as needed?

Cheers,
Paul

"Jon Skeet [C# MVP]" wrote:

Soli3d< So **** @ discussion.microsoft.comwrote:
Soli3d <So****@discussions.microsoft.comwrote:

Is有没有办法从方法继承?我不想在重载版本中一遍又一遍地重复我的方法中的代码。如果我需要

进行更改,我不想在我的方法的每个超载

版本中重新加载它们。


更好的方法吗?
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?



通常的方法是使一个重载具有你可能需要的所有信息

,并从其他方法调用它,传递在

参数中。例如:


公共对象LookupItem(对象键,对象defaultValue)

{

...执行查找.. 。

}


//与LookupItem(key,defaultValue)相同,但默认值为

// null

公共对象LookupItem(对象键)

{

返回LookupItem(键,null);

}


-

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

如果回复群组,请不要邮寄我也是


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


只要类型不同,你可以拥有与你想要的

相同方法的重载次数,即使参数数量相同,每个



彼得


-

联合创始人,Eggheadcafe.com开发者门户网站:
http:/ /www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Soli3d"写道:
As long as the types are different, you can have as many overloads of the
same method you want, even though the number of parameters are the same for
each.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Soli3d" wrote:

Jon,


非常感谢。如果我想

将不同的类型传递给main方法,这将如何工作? Int16 vs

双倍?我是否必须将主方法设为对象

并根据需要进行投射?


干杯,

Paul


" Jon Skeet [C#MVP]"写道:
Jon,

Thank you much. How would this work if I wanted
to pass different types to the main method? Int16 vs
Double? Do I have to make the main method an Object
and cast as needed?

Cheers,
Paul

"Jon Skeet [C# MVP]" wrote:

Soli3d< So **** @ discussion.microsoft.comwrote:
Soli3d <So****@discussions.microsoft.comwrote:

Is有没有办法从方法继承?我不想在重载版本中一遍又一遍地重复我的方法中的代码。如果我需要

进行更改,我不想在我的方法的每个超载

版本中重新加载它们。

&gt ;

更好的方法吗?
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?



通常的方法是进行一次重载,其中包含您可能需要的所有信息,并从其他方法调用,传递在

参数中。例如:


公共对象LookupItem(对象键,对象defaultValue)

{

...执行查找.. 。

}


//与LookupItem(key,defaultValue)相同,但默认值为

// null

公共对象LookupItem(对象键)

{

返回LookupItem(键,null);

}


-

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

如果回复群组,请不要邮寄我也是

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