방법: 파생 클래스 만들기

업데이트: 2007년 11월

Inherits 문을 사용하면 클래스에서는 지정된 클래스의 전용이 아닌 멤버를 모두 상속합니다.

다른 클래스에서 상속하려면

  • 기본 클래스로 사용하려는 클래스의 이름을 포함하는 Inherits 문을 파생 클래스에 첫째 문으로 추가합니다. Inherits 문은 주석을 제외하고 클래스 문 바로 뒤에 나오는 첫째 문이어야 합니다.

예제

다음 예제에서는 두 개의 클래스를 정의합니다. 첫째 클래스는 두 개의 메서드를 갖는 기본 클래스입니다. 둘째 클래스는 기본 클래스의 두 메서드를 모두 상속하고, 둘째 메서드를 재정의하고, 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의 상속

Class 속성, 필드, 메서드