父类中的c#虚方法调用另一个在子类中重写的虚方法

时间:2018-01-03 03:58:37

标签: c#

假设我有一个c#类A,它继承自B类,后者继承自C类。

Class A : Class B
{}

Class B : Class C
{
    override OnIntialise()
    {
    do something else...
    }
}

Class C
{
    virtual Initialise()
    {
        OnIntialise();
    }
    virtual OnInitialise()
    {
        do something...
    }
}

现在当我调用a.Initialise()时,程序将调用Class C中定义的方法Initialise()。在Initialise()中,是否会调用在C类或B类中定义的OnIntialise()方法?

1 个答案:

答案 0 :(得分:0)

在这种情况下,将调用类Sub UniqueNumber() Dim vDB, vR(), a Dim i As Integer, j As Integer, n As Long, z As Integer Dim k As Integer, p As Integer Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") vDB = Range("a2", Range("a" & Rows.Count).End(3)) z = UBound(vDB, 1) ReDim Preserve vR(1 To 3) For i = 1 To z: For j = 1 To z: For k = 1 To z If i <> j And i <> k And j <> k Then vR(1) = vDB(i, 1) vR(2) = vDB(j, 1) vR(3) = vDB(k, 1) s = Join(vR, "") If dic.Exists(s) Then Else n = n + 1 dic.Add s, s End If End If Next k: Next j: Next i a = dic.Keys Range("d2").CurrentRegion.Offset(1).Clear Range("d2").Resize(n) = WorksheetFunction.Transpose(a) End Sub 的类Initialise方法,因为它在子类中被重写,即类B。如果在类B中重写了此方法,那么类A中的方法将被调用。