Overloading ,vb.net vs C#


vb.net 的实现和C# 的实现有很多方面都不一样,今天就拿 Overload 来做个对比。

大家可能都对 C# 比较熟悉,我列出一下一段代码:

None.gif namespace  OverloaddingCS
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void  Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Derived d
=new Derived();
InBlock.gif                d.WriteLine(
10);
InBlock.gif                d.WriteLine(
"11");
InBlock.gif                d.WriteLine(
10.5);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class Base
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void WriteLine(String AnyString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(AnyString 
+ " called Base:String") ;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void WriteLine(int AnyInteger)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(AnyInteger.ToString() 
+ " called Base:Integer");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class Derived :  Base
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public void WriteLine(double AnyDouble)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(AnyDouble.ToString() 
+ " called Derived:double");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


答案很简单,如果你对c++ 比较熟悉的话

10 called Derived:double
11 called Base:String
10.5 called Derived:double

如果我把类似的代码翻译成 vb.net ,注意是原本的翻译

ExpandedBlockStart.gif ContractedBlock.gif Class Test Class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Sub Main()Sub Main()
InBlock.gif        
Dim objMyTest As New Derived
InBlock.gif        
With objMyTest
InBlock.gif            .
WriteLine(10)
InBlock.gif            .
WriteLine("11")
InBlock.gif            .
WriteLine(10.5)
InBlock.gif        
End With
InBlock.gif        Console.ReadLine()
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

ExpandedBlockStart.gifContractedBlock.gif
Class Base Class Base
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub WriteLine()Sub WriteLine(ByVal AnyString As String)
InBlock.gif        Console.
WriteLine(AnyString + " called Base:String")
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub WriteLine()Sub WriteLine(ByVal AnyInteger As Integer)
InBlock.gif        Console.
WriteLine(AnyInteger.ToString + " called Base:Integer")
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Class Derived Class Derived : Inherits Base
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Overloads Sub WriteLine()Sub WriteLine(ByVal AnyDouble As Double)
InBlock.gif        Console.
WriteLine(AnyDouble.ToString + " called Derived:Double")
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class


代码起始是一样的。我直接公布结果,起始跟 C# 的结果不一样

10 called Base:Integer
11 called Base:String
10.5 called Derived:Double

结果出乎意料,起始也是情理之中的事情。

我就不做解释了,关键是提醒各位有 C++ 专项 VB.NET 的时候,不要想当然的以为 VB.NET 的处理方式跟 C# 一样。


转载于:https://www.cnblogs.com/montaque/archive/2005/02/02/100894.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值