方法 : 派生クラスを作成する

更新 : 2007 年 11 月

Inherits ステートメントを使用すると、指定したクラスのプライベート メンバ以外のすべてのメンバが、クラスに継承されます。

他のクラスを継承するには

  • 基本クラスとして使用するクラスの名前を指定した Inherits ステートメントを、派生クラスの最初のステートメントとして追加します。Inherits ステートメントは、クラス ステートメントの後の最初の非コメント ステートメントであることが必要です。

使用例

次の例では、2 つのクラスを定義しています。最初のクラスは、2 つのメソッドを持つ基本クラスです。2 番目のクラスは、基本クラスの 2 つのメソッドを継承し、2 つ目のメソッドをオーバーライドして、Field という名前のフィールドを定義しています。

Class Class1
    Sub Method1()
        MsgBox("This is a method in the base class.")
    End Sub
    Overridable Sub Method2()
        MsgBox("This is another method in the base class.")
    End Sub
End Class

Class Class2
    Inherits Class1
    Public Field2 As Integer
    Overrides Sub Method2()
        MsgBox("This is a method in a derived class.")
    End Sub
End Class

Protected Sub TestInheritance()
    Dim C1 As New Class1
    Dim C2 As New Class2
    C1.Method1() ' Calls a method in the base class.
    C1.Method2() ' Calls another method from the base class.
    C2.Method1() ' Calls an inherited method from the base class.
    C2.Method2() ' Calls a method from the derived class.
End Sub

プロシージャ TestInheritance を実行すると、次のメッセージが表示されます。

This is a method in the base class.

This is another method in the base class.

This is a method in the base class.

This is a method in a derived class.

参照

概念

プロパティとメソッドのオーバーライド

オーバーライド修飾子

その他の技術情報

Visual Basic の継承

クラス、プロパティ、フィールド、およびメソッド