0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VB.netで純粋仮想関数

Last updated at Posted at 2019-04-20

1.経緯と目的など

基底クラスで定義した関数を、継承先で呼び出す。
その関数の実行順序はある程度枠組みを作りたい。
こういったケースを考えたときに、使えるかもしれません。
例)
途中まで共通で同じ処理を行って、
そのあとの処理は「処理1 → 処理2 → 処理3」の順番で実行してほしい。
ただし、実装内容は継承先にお任せします。
といったケース。(需要あるのかな?)

2.サンプル

Test.vb

Module Module1
# Region "基底クラス"
    MustInherit Class BaseClass

        Public MustOverride Function Proc1() As Boolean
        Public MustOverride Function Proc2() As Boolean
        Public MustOverride Function Proc3() As Boolean

        Private Sub CommonProc1()
            '継承するクラスが共通して実施する処理をここへ記載する。
            Console.Out.WriteLine("何かしらの共通処理1")
        End Sub
        Private Sub CommonProc2()
            Console.Out.WriteLine("何かしらの共通処理2")
        End Sub
        Public Function DoExec() As Boolean
            Me.CommonProc1() ' 基底クラスで定義した処理を実行する関数1
            Me.Proc1()       ' 継承先で実装した処理内容を実行する関数1
            Me.Proc2()       ' 継承先で実装した処理内容を実行する関数2
            Me.Proc3()       ' 継承先で実装した処理内容を実行する関数3
            Me.CommonProc2() ' 基底クラスで定義した処理を実行する関数2
            Return True
        End Function
    End Class
# End Region
# Region "継承クラス"
    Class InheritClass_A
        Inherits BaseClass
        Public Overrides Function Proc1() As Boolean
            Console.Out.WriteLine("Proc1 From [ A ]")
            Return True
        End Function
        Public Overrides Function Proc2() As Boolean
            Console.Out.WriteLine("Proc2 From [ A ]")
            Return True
        End Function
        Public Overrides Function Proc3() As Boolean
            Console.Out.WriteLine("Proc3 From [ A ]")
            Return True
        End Function
    End Class

    Class InheritClass_B
        Inherits BaseClass
        Public Overrides Function Proc1() As Boolean
            Console.Out.WriteLine("Proc1 From [ B ]")
            Return True
        End Function
        Public Overrides Function Proc2() As Boolean
            Console.Out.WriteLine("Proc2 From [ B ]")
            Return True
        End Function
        Public Overrides Function Proc3() As Boolean
            Console.Out.WriteLine("Proc3 From [ B ]")
            Return True
        End Function
    End Class

# End Region

    ' TestProc
    Sub Main()
        Dim _A As New InheritClass_A
        _A.DoExec()
        Dim _B As New InheritClass_B
        _B.DoExec()
    End Sub
End Module

3.実行結果


何かしらの共通処理1
Proc1 From [ A ]
Proc2 From [ A ]
Proc3 From [ A ]
何かしらの共通処理2
何かしらの共通処理1
Proc1 From [ B ]
Proc2 From [ B ]
Proc3 From [ B ]
何かしらの共通処理2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?