重载功能,全双 [英] Overload a function, all double

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

问题描述

大家好,

我有一个奇怪的要求,我想重载一个函数.是的,是的,你们都会想,但这很容易.但是我不想以向函数提供两个值的常规方式对其进行重载,而是通过将值设置为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

推荐答案

也许您不知道超载是什么.难怪,这个术语很混乱,因为实际上没有什么是超载"的,那么什么可能是超载"的呢?其实什么都没有.

这实际上意味着两个或多个方法具有相同的名称(在相同的类或结构中,即,具有相同的完全限定名称),但是具有不同的签名,并且通过调用可以将每个特定的签名识别为编译器.也就是说,根据实际调用代码中的特定调用参数,这种识别可能或不可能,并且歧义会导致编译错误.

此外,您应该了解,尽管VB.NET通常具有隐式转换,但它仍然是一种具有强类型化的语言.请参阅 http://en.wikipedia.org/wiki/Strong_typing [
Maybe you don''t know what is overloading. No wonder, the term is quite confusing, because nothing is actually "loaded", so what could be "overloaded"? Actually, nothing.

It really means that two or more methods have the same name (in the same class or structure, that is, the same fully-qualified name), but different signatures, and each particular signature could be recognized be the compiler by the call. That said, depending on the particular call parameters in the code of the actual call, such recognition may or may not be possible, and the ambiguity will cause compilation error.

Additionally, you should understand that VB.NET is still a language with strong typing, despite of implicit conversion typical for this language. Please see http://en.wikipedia.org/wiki/Strong_typing[^].

That said: if your idea is a dynamic parameter, it''s a wrong idea. If you formal parameter is double, it will always be double, even if the actual parameter is int — it is merely converted to double, so, from the standpoint of the method implementation, it is always double and nothing else.

Actually, your question is not clear and/or incorrectly formulated and/or makes no sense. Not to worry — I hope my explanations are enough for you to sort things out.

—SA


您想做的事是可能的,但在我看来,您正在使之变得比原来更难.为什么不做一个简单的方法而不是做一个函数呢?在ByRef(按引用)中传递变量,以便您可以直接更新它们:
Want you want to do is possible, but in my opinion you are making it harder than it needs to be. Instead of making a function, why not make a simple method? Pass your variables in ByRef (By Reference) so you can update them directly:
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


类似的事情就是我想要的:

Something like this is what I guess you want:

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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆