MustInherit (Visual Basic)MustInherit (Visual Basic)

指定一个类可以仅用作基类,则不能直接从其中创建对象。Specifies that a class can be used only as a base class and that you cannot create an object directly from it.

备注Remarks

目的基类(也称为抽象类) 是定义普遍适用于从其派生的所有类的功能。The purpose of a base class (also known as an abstract class) is to define functionality that is common to all the classes derived from it. 这使派生的类无需重新定义的常用元素。This saves the derived classes from having to redefine the common elements. 在某些情况下,此通用功能不是足够完整,从而使可用对象,并每个派生的类定义缺少的功能。In some cases, this common functionality is not complete enough to make a usable object, and each derived class defines the missing functionality. 在这种情况下,您希望使用的代码只能从派生类中创建对象。In such a case, you want the consuming code to create objects only from the derived classes. 您使用MustInherit上强制实施此的基类。You use MustInherit on the base class to enforce this.

另一个用途MustInherit类是限制对一组相关的类的变量。Another use of a MustInherit class is to restrict a variable to a set of related classes. 您可以定义一个基类,并从其派生所有这些相关的类。You can define a base class and derive all these related classes from it. 基类不需要提供任何功能普遍适用于所有派生的类,但它可用作筛选器将值分配到变量。The base class does not need to provide any functionality common to all the derived classes, but it can serve as a filter for assigning values to variables. 如果你使用的代码声明一个变量,作为类的基类,Visual Basic,可从派生类之一仅对象分配给该变量。If your consuming code declares a variable as the base class, Visual Basic allows you to assign only an object from one of the derived classes to that variable.

.NET Framework 定义了多个MustInherit类,在它们之间ArrayEnum,和ValueTypeThe .NET Framework defines several MustInherit classes, among them Array, Enum, and ValueType. ValueType 是一个基类,它将限制变量的示例。ValueType is an example of a base class that restricts a variable. 所有值类型都派生ValueTypeAll value types derive from ValueType. 如果你声明一个变量,作为ValueType,可以将只有值类型分配给该变量。If you declare a variable as ValueType, you can assign only value types to that variable.

规则Rules

  • 声明上下文。Declaration Context. 可以使用MustInherit仅在Class语句。You can use MustInherit only in a Class statement.

  • 组合的修饰符。Combined Modifiers. 不能指定MustInherit一起使用NotInheritable同一声明中。You cannot specify MustInherit together with NotInheritable in the same declaration.

示例Example

下面的示例演示强制的继承和强制重写。The following example illustrates both forced inheritance and forced overriding. 类的基类shape定义一个变量, acrossLineThe base class shape defines a variable acrossLine. circlesquare派生自shapeThe classes circle and square derive from shape. 它们继承的定义acrossLine,但它们必须定义该函数area因为该计算不同的形状的每一类。They inherit the definition of acrossLine, but they must define the function area because that calculation is different for each kind of shape.

Public MustInherit Class shape
    Public acrossLine As Double
    Public MustOverride Function area() As Double
End Class
Public Class circle : Inherits shape
    Public Overrides Function area() As Double
        Return Math.PI * acrossLine
    End Function
End Class
Public Class square : Inherits shape
    Public Overrides Function area() As Double
        Return acrossLine * acrossLine
    End Function
End Class
Public Class consumeShapes
    Public Sub makeShapes()
        Dim shape1, shape2 As shape
        shape1 = New circle
        shape2 = New square
    End Sub
End Class

您可以声明shape1shape2属于类型shapeYou can declare shape1 and shape2 to be of type shape. 但是,不能创建一个对象从shape因为它缺少函数的功能area将标记为MustInheritHowever, you cannot create an object from shape because it lacks the functionality of the function area and is marked MustInherit.

因为它们被声明为shape,变量shape1shape2仅限于从派生类的对象circlesquareBecause they are declared as shape, the variables shape1 and shape2 are restricted to objects from the derived classes circle and square. Visual Basic 不允许您将任何其他对象分配给这些变量,它为您提供了类型安全的高级别。Visual Basic does not allow you to assign any other object to these variables, which gives you a high level of type safety.

用法Usage

MustInherit修饰符可用于在此上下文中:The MustInherit modifier can be used in this context:

Class 语句Class Statement

请参阅See also