本文介绍了重载功能,全双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个奇怪的要求,我想重载一个函数.是的,是的,你们都会想,但这很容易.但是我不想以向函数提供两个值的常规方式对其进行重载,而是通过将值设置为double或integer或任何情况来完成重载...

我需要以下内容:

Hi All,

I have a bit of an odd request, I''d like to overload a function. Yes, yes, you all will think but that is easy. But I don''t want to overload it in the conventional way of feeding two values to the function and the overloading is done by having the values as double or integer or whatever the case might be...

I need the following:

public function circle (byval diameter as double, byval radius as double, byval area as double, byval cirumference as double) as double

'Depending on which byval I fill in, and which one I indicate with say a "0", the 0 unknown is returned

end function

推荐答案


Public Sub circle(ByRef diameter As Double, ByRef radius As Double, ByRef area As Double, ByRef cirumference As Double)

If diameter <> 0 Then
   'Calculate all parameters other than diameter
End If 

If radius <> 0 Then
   'Calculate all parameters other than radius
End If

'And so on...

End Sub



这样,在调用该方法时,您已经设置了所有变量以检索正确的值.



This way, when call the method, you already have all the variables setup to retrieve the proper values.

Dim diameter as Double = 10
Dim radius as Double = 0
Dim area as Double = 0
Dim circumference as Double = 0
circle(diameter, radius, area, circumference)
'Now you already know which "return" value is for which variable


public static double Circle(double? diameter = null, double? radius = null, double? area = null, double? circumference = null)
{
    double result = 0.0;

    if (diameter.HasValue)
    {
        // Do Something
    }

    if (radius.HasValue)
    {
        // Do Something
    }

    if (area.HasValue)
    {
        // Do Something
    }

    if (circumference.HasValue)
    {
        // Do Something
    }

    return result;
}



然后可以使用命名参数来调用它:



You can then call this with named parameters:

Circle(radius: 10.0);



抱歉,在C#中,但这就是我所知道的.



Sorry it''s in C#, but that''s what I know.


这篇关于重载功能,全双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:23