如果基本方法在VB中调用重写的方法会怎样? [英] What happens if a base method calls an overridden method in VB?

查看:70
本文介绍了如果基本方法在VB中调用重写的方法会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

Public Class ParentClass
    Public Sub GenerateReport
        Dim Col As Collection
        Col = GetItemCollection()
    End Sub

    Public Overridable Function GetItemCollection() As Collection
        GetItemCollection = New Collection
        GetItemCollection.Add("1")
        GetItemCollection.Add("2")
        GetItemCollection.Add("3")
    End Function
End Class

Public Class ExtendedClass
    Inherits ParentClass

    Public Overrides Function GetItemCollection() As Collection
        GetItemCollection = New Collection
        GetItemCollection.Add("A")
        GetItemCollection.Add("B")
        GetItemCollection.Add("C")
    End Function
End Class

Public Sub Main()
    Dim cls As New ExtendedClass
    cls.GenerateReport()
End Sub

Main()调用cls.GenerateReport()时,变量Col是数字还是字母的集合?我希望它能够识别clsExtendedClass的实例,并调用重写的方法并返回字母.

When Main() calls cls.GenerateReport(), is the variable Col going to be a collection of numbers or letters? I'm hoping that it will recognize that cls is an instance of ExtendedClass and call the overridden method and return the letters.

推荐答案

它将覆盖字母,因为您确实覆盖了该方法.但是,您在哪里声明了GetItemCollection?您仍然需要一个实例变量.

It will be a collection of letters as you did override the method. However, where did you declare the GetItemCollection? You still need an instance variable.

这篇关于如果基本方法在VB中调用重写的方法会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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