2011-07-11 7 views
0

は、次の例を考えてみましょう:基本メソッドがVBでオーバーライドされたメソッドを呼び出すとどうなりますか?

変数 Colは数字や文字の集合であることを行っている cls.GenerateReport()を呼び出し Main()
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 

clsExtendedClassのインスタンスであることを認識し、オーバーライドされたメソッドを呼び出して文字を返すことを期待しています。

答えて

1

これは、メソッドをオーバーライドしたときと同じように、文字の集まりになります。しかし、GetItemCollectionはどこで宣言しましたか?インスタンス変数が必要です。

+0

GetItemCollectionは関数の名前であり、コレクションオブジェクトとしてそれ自身で使用できるため、宣言されています。 – jwatts1980

+0

私はvb.netに慣れていません。とにかく、派生クラスのメソッドが呼び出されます。 – Femaref

関連する問題