“Overloads” keyword in VB.NET

后端 未结 6 654
予麋鹿
予麋鹿 2020-12-06 16:29

Do you really need this keyword to overload methods? What is the difference between using the overloads keyword vs. just having different method signatures?

6条回答
  •  爱一瞬间的悲伤
    2020-12-06 17:12

    This shows up high in the Google results, and I think it could be explained more clearly here.

    There is no reason to use the Overloads keyword when you're overloading various methods within the same class. The main reason that you would use Overloads is to allow a derived class to call a method from its base class that has the same name as the overloaded method, but a different signature.

    Suppose you have two classes, Foo and SonOfFoo, where SonOfFoo inherits from Foo. If Foo implements a method called DoSomething and SonOfFoo implements method with the same name, the SonOfFoo method will hide the parent class's implementation...even if the two methods take different parameters. Specifying the Overloads keyword will allow the derived class to call the parent class's overloads of the method.

    Here's some code to demonstrate the above, with the classes Foo and SonOfFoo implemented as described, and another pair of classes, Bar and SonOfBar that use the Overloads keyword:

    Class Foo
        Public Sub DoSomething(ByVal text As String)
            Console.WriteLine("Foo did: " + text)
        End Sub
    End Class
    
    Class SonOfFoo
        Inherits Foo
    
        Public Sub DoSomething(ByVal number As Integer)
            Console.WriteLine("SonOfFoo did: " + number.ToString())
        End Sub
    End Class
    
    Class Bar
        Public Sub DoSomething(ByVal text As String)
            Console.WriteLine("Bar did: " + text)
        End Sub
    End Class
    
    Class SonOfBar
        Inherits Bar
    
        Public Overloads Sub DoSomething(ByVal number As Integer)
            Console.WriteLine("SonOfBar did: " + number.ToString())
        End Sub
    End Class
    
    Sub Main()
        Dim fooInstance As Foo = New SonOfFoo()
        'works
        fooInstance.DoSomething("I'm really a SonOfFoo")
        'compiler error, Foo.DoSomething has no overload for an integer
        fooInstance.DoSomething(123)
    
        Dim barInstance As Bar = New SonOfBar()
        'works
        barInstance.DoSomething("I'm really a SonOfBar")
        'compiler error, Bar.DoSomething has no overload for an integer
        barInstance.DoSomething(123)
    
        Dim sonOfFooInstance As New SonOfFoo()
        'compiler error, the base implementation of DoSomething is hidden and cannot be called
        sonOfFooInstance.DoSomething("I'm really a SonOfFoo")
        'works
        sonOfFooInstance.DoSomething(123)
    
        Dim sonOfBarInstance As New SonOfBar()
        'works -- because we used the Overloads keyword
        sonOfBarInstance.DoSomething("I'm really a SonOfBar")
        'works
        sonOfBarInstance.DoSomething(123)
    End Sub
    

    Here's some information on how this compiles differently in the CLI.

提交回复
热议问题