函数重载未按预期工作 [英] Function overloading is not working as expected

查看:96
本文介绍了函数重载未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有两个具有相同名称和不同类型参数的函数,如下所述:



I have a class having two functions with same name and different type of arguments as described below:

  Public Class functionOverload
'<--- first function starts here
        Public Function Test(ByVal x As Int16, ByVal y As Int16) As Double
            Return x * y
        End Function
'<--- first function Ends here

'<--- Second function starts here
        Public Function Test(ByVal x As Int32, ByVal y As Int32) As Integer
            Return x + y
        End Function
'<--- Second function ends here
    End Class



在我的主模块中,我创建了一个对象这个类并使用不同的参数调用函数:




In my main module i had created an object of this class and call the function using different arguments:

Dim obj As New functionOverload
 MsgBox(obj.Test(10, 20)) '<--- will call second function as both the parameters are of type int32
 Dim i, j As Int16
 i = 10
 j = 20
 MsgBox(obj.Test(i, j)) '<--- will call First function as both the parameters are of type int16





到目前为止工作正常。我尝试了更多如下:





Up to this works fine as excepted. and i had tried even more as follows:

MsgBox(obj.Test(i, 10)) '<--- will call second function even though their is no matching function
MsgBox(obj.Test(20, j)) '<--- will call second function even though their is no matching function





问题:为什么会这样?函数超载的规则是不同的vb.net



Question : why this is happening? Is the rules for function over loading is different in vb.net

推荐答案

在这些情况下;

In these cases;
MsgBox(obj.Test(i, 10)) '<--- will call second function even though their is no matching function
MsgBox(obj.Test(20, j)) '<--- will call second function even though their is no matching function



它的工作原理是因为将 Int16 上传到 Int32 是安全的,所以编译器这样做,然后你有一个调用,你传递两个 Int32 s(这是第二个函数)为 10 20 将被解释为 Int32 s,除非另有说明。



希望这有帮助,

Fredrik


It works because upcasting an Int16 to an Int32 is safe, so the compiler that do that, and then you have a call where you pass two Int32s (which is the second function) as the 10 and 20 will be interpreted as Int32s unless specified.

Hope this helps,
Fredrik


这真的有更多使用VB.NET进行隐式转换而不是重载。



问题是VB.NET会隐式地将值转换为下一个更宽的类型,但是除非明确告知,否则不要将任何东西转换为缩小类型。这是因为扩展转换没有机会丢失数据,缩小转换可能会丢失数据。



这意味着,除非另有说明,VB.NET将选择如果所有值类型已经匹配,或者某些参数可以上转换为更宽的类型,则方法评估过程将使用新扩展的类型重新开始。这种情况一直持续到找不到匹配的方法或没有其他可能匹配的方法。



在其他语言中,您可能必须明确上转换您的值要获得匹配类型,并非所有语言都会为您进行隐式转换。这是大括号语言人们对VB.NET感到讨厌的问题之一。
This really has more to do with VB.NET doing implicit conversions than it does with overloading.

The thing is that VB.NET will implicitly upconvert a value to the next wider type, but not convert anything down to a narrowing type unless explicitly told to do so. This is because a widening conversion has no chance of loosing data where a narrowing conversion can lose data.

This means that, unless told otherwise, VB.NET will chose a method where all of the value type either already match, or some of the parameters can be upconverted to wider types, then the method evaluation process starts all over again with the newly widened types. This continues until either a matching method is found or there are no other methods that can possibly match.

In other languages, you'd probably have to explicitly upconvert your values to get a matching type as not all languages will do implicit conversions for you. It's one of the gotchas that curly brace language people find annoying with VB.NET.


这篇关于函数重载未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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