Interface ステートメント (Visual Basic)Interface Statement (Visual Basic)
インターフェイスの名前を宣言し、インターフェイスを構成しているメンバーの定義を取り込みます。Declares the name of an interface and introduces the definitions of the members that the interface comprises.
構文Syntax
[ <attributelist> ] [ accessmodifier ] [ Shadows ] _
Interface name [ ( Of typelist ) ]
[ Inherits interfacenames ]
[ [ modifiers ] Property membername ]
[ [ modifiers ] Function membername ]
[ [ modifiers ] Sub membername ]
[ [ modifiers ] Event membername ]
[ [ modifiers ] Interface membername ]
[ [ modifiers ] Class membername ]
[ [ modifiers ] Structure membername ]
End Interface
指定項目Parts
用語Term | 定義Definition |
---|---|
attributelist |
任意。Optional. 「属性リスト」を参照してください。See Attribute List. |
accessmodifier |
任意。Optional. 次のいずれかの値を指定します。Can be one of the following: - Public- Public - Protected- Protected - Friend- Friend - Private- Private - Protected Friend- Protected Friend - Private Protected- Private Protected 「 Access levels in Visual Basic」を参照してください。See Access levels in Visual Basic. |
Shadows |
任意。Optional. 「Shadows」を参照してください。See Shadows. |
name |
必須です。Required. このインターフェイスの名前。Name of this interface. 「 Declared Element Names」を参照してください。See Declared Element Names. |
Of |
任意。Optional. これがジェネリック インターフェイスであることを指定します。Specifies that this is a generic interface. |
typelist |
Of キーワードを使用する場合は必須です。Required if you use the Of keyword. このインターフェイスの型パラメーターの一覧。List of type parameters for this interface. 必要に応じて、In および Out ジェネリック修飾子を使用して、各型パラメーターをバリアントとして宣言できます。Optionally, each type parameter can be declared variant by using In and Out generic modifiers. 「型リスト」を参照してください。See Type List. |
Inherits |
任意。Optional. このインターフェイスは、別のインターフェイス (複数のインターフェイスも含む) の属性とメンバーを継承することを示します。Indicates that this interface inherits the attributes and members of another interface or interfaces. 「Inherits ステートメント」を参照してください。See Inherits Statement. |
interfacenames |
Inherits ステートメントを使用する場合は必ず指定します。Required if you use the Inherits statement. このインターフェイスの派生元のインターフェイスの名前。The names of the interfaces from which this interface derives. |
modifiers |
任意。Optional. 定義されているインターフェイス メンバーの適切な修飾子。Appropriate modifiers for the interface member being defined. |
Property |
任意。Optional. インターフェイスのメンバーであるプロパティを定義します。Defines a property that is a member of the interface. |
Function |
任意。Optional. インターフェイスのメンバーである Function プロシージャを定義します。Defines a Function procedure that is a member of the interface. |
Sub |
任意。Optional. インターフェイスのメンバーである Sub プロシージャを定義します。Defines a Sub procedure that is a member of the interface. |
Event |
任意。Optional. インターフェイスのメンバーであるイベントを定義します。Defines an event that is a member of the interface. |
Interface |
任意。Optional. このインターフェイス内で入れ子になっているインターフェイスを定義します。Defines an interface that is a nested within this interface. 入れ子になったインターフェイスの定義は、End Interface ステートメントで終了する必要があります。The nested interface definition must terminate with an End Interface statement. |
Class |
任意。Optional. インターフェイスのメンバーであるクラスを定義します。Defines a class that is a member of the interface. メンバー クラスの定義は、End Class ステートメントで終了する必要があります。The member class definition must terminate with an End Class statement. |
Structure |
任意。Optional. インターフェイスのメンバーである構造体を定義します。Defines a structure that is a member of the interface. メンバー構造体の定義は、End Structure ステートメントで終了する必要があります。The member structure definition must terminate with an End Structure statement. |
membername |
インターフェイスのメンバーとして定義されているプロパティ、プロシージャ、イベント、インターフェイス、クラス、または構造体ごとに必須です。Required for each property, procedure, event, interface, class, or structure defined as a member of the interface. メンバーの名前。The name of the member. |
End Interface |
Interface の定義を終了します。Terminates the Interface definition. |
RemarksRemarks
インターフェイスでは、クラスおよび構造体で実装できる、プロパティやプロシージャなどの一連のメンバーを定義します。An interface defines a set of members, such as properties and procedures, that classes and structures can implement. インターフェイスでは、メンバーのシグネチャのみを定義し、それらの内部動作は定義しません。The interface defines only the signatures of the members and not their internal workings.
クラスまたは構造体では、インターフェイスで定義されているすべてのメンバーにコードを提供することによって、インターフェイスを実装します。A class or structure implements the interface by supplying code for every member defined by the interface. 最後に、アプリケーションによって、そのクラスまたは構造体からインスタンスが作成されると、メモリ内にオブジェクトが存在し、実行されます。Finally, when the application creates an instance from that class or structure, an object exists and runs in memory. 詳細については、「オブジェクトとクラス」と「インターフェイス」を参照してください。For more information, see Objects and Classes and Interfaces.
Interface
は、名前空間またはモジュール レベルでのみ使用できます。You can use Interface
only at namespace or module level. つまり、インターフェイスの宣言コンテキストは、ソース ファイル、名前空間、クラス、構造体、モジュール、またはインターフェイスである必要があり、プロシージャまたはブロックであってはいけません。This means the declaration context for an interface must be a source file, namespace, class, structure, module, or interface, and cannot be a procedure or block. 詳細については、「宣言コンテキストと既定のアクセス レベル」を参照してください。For more information, see Declaration Contexts and Default Access Levels.
インターフェイスは、既定で Friend アクセスに設定されます。Interfaces default to Friend access. アクセス修飾子を使用してこれらのアクセス レベルを調整できます。You can adjust their access levels with the access modifiers. 詳しくは、「Visual Basic でのアクセス レベル」を参照してください。For more information, see Access levels in Visual Basic.
ルールRules
インターフェイスの入れ子。Nesting Interfaces. インターフェイスの内部に別のインターフェイスを定義できます。You can define one interface within another. 外側のインターフェイスは、包含インターフェイスと呼ばれ、内側のインターフェイスは 入れ子になったインターフェイスと呼ばれます。The outer interface is called the containing interface, and the inner interface is called a nested interface.
メンバー宣言。Member Declaration. インターフェイスのメンバーとしてプロパティまたはプロシージャを宣言する場合は、そのプロパティまたはプロシージャのシグネチャのみを定義します。When you declare a property or procedure as a member of an interface, you are defining only the signature of that property or procedure. これには、要素の型 (プロパティまたはプロシージャ)、そのパラメーターとパラメーターの型、およびその戻り値の型が含まれます。This includes the element type (property or procedure), its parameters and parameter types, and its return type. このため、メンバー定義では、1 行だけのコードを使用し、インターフェイスでは
End Function
やEnd Property
などの終了ステートメントは、有効ではありません。Because of this, the member definition uses only one line of code, and terminating statements such asEnd Function
orEnd Property
are not valid in an interface.これに対し、列挙型、構造体、または入れ子になったクラスやインターフェイスを定義する場合は、それらのデータ メンバーを含める必要があります。In contrast, when you define an enumeration or structure, or a nested class or interface, it is necessary to include their data members.
メンバー修飾子。Member Modifiers. モジュール メンバーを定義するときにアクセス修飾子を使用することはできず、Shared または Overloads を除く任意のプロシージャ修飾子を指定することもできません。You cannot use any access modifiers when defining module members, nor can you specify Shared or any procedure modifier except Overloads. Shadows で任意のメンバーを宣言できます。また、プロパティを定義するときに Default のほか、ReadOnly や WriteOnly を使用することもできます。You can declare any member with Shadows, and you can use Default when defining a property, as well as ReadOnly or WriteOnly.
継承。Inheritance. インターフェイスで Inherits ステートメントを使用する場合は、1 つまたは複数の基底インターフェイスを指定できます。If the interface uses the Inherits Statement, you can specify one or more base interfaces. それぞれが同じ名前のメンバーを定義している場合でも、2 つのインターフェイスから継承できます。You can inherit from two interfaces even if they each define a member with the same name. その場合、実装するコードでは、実装するメンバーを指定するために名前の修飾を使用する必要があります。If you do so, the implementing code must use name qualification to specify which member it is implementing.
インターフェイスは、より制限の厳しいアクセス レベルの別のインターフェイスから継承することはできません。An interface cannot inherit from another interface with a more restrictive access level. たとえば、
Public
インターフェイスは、Friend
インターフェイスから継承することはできません。For example, aPublic
interface cannot inherit from aFriend
interface.インターフェイスは、その中に入れ子にされたインターフェイスから継承することはできません。An interface cannot inherit from an interface nested within it.
実装。Implementation. クラスで Implements ステートメントを使用してこのインターフェイスを実装する場合は、インターフェイス内に定義されているすべてのメンバーを実装する必要があります。When a class uses the Implements statement to implement this interface, it must implement every member defined within the interface. さらに、実装するコード内の各シグネチャが、このインターフェイスで定義されている対応するシグネチャと正確に一致する必要があります。Furthermore, each signature in the implementing code must exactly match the corresponding signature defined in this interface. ただし、実装するコード内のメンバーの名前が、インターフェイスで定義されているメンバー名と一致する必要はありません。However, the name of the member in the implementing code does not have to match the member name as defined in the interface.
クラスでプロシージャを実装している場合、プロシージャを
Shared
として指定することはできません。When a class is implementing a procedure, it cannot designate the procedure asShared
.既定のプロパティ。Default Property. インターフェイスでは、プロパティ名を使用せずに参照できる、既定のプロパティとして、最大で 1 つのプロパティを指定できます。An interface can specify at most one property as its default property, which can be referenced without using the property name. このようなプロパティを指定するには、Default 修飾子を使用して宣言します。You specify such a property by declaring it with the Default modifier.
つまり、インターフェイスで何も継承しない場合にのみ、既定のプロパティを定義できます。Notice that this means that an interface can define a default property only if it inherits none.
動作Behavior
アクセス レベル。Access Level. すべてのインターフェイス メンバーには、暗黙的にPublic アクセス権が与えられます。All interface members implicitly have Public access. メンバーを定義するときに、アクセス修飾子を使用することはできません。You cannot use any access modifier when defining a member. ただし、インターフェイスを実装するクラスでは、実装される各メンバーのアクセス レベルを宣言できます。However, a class implementing the interface can declare an access level for each implemented member.
クラス インスタンスを変数に割り当てる場合、そのメンバーのアクセス レベルは、変数のデータ型が、基になるインターフェイスであるか、または実装しているクラスであるかによって異なります。If you assign a class instance to a variable, the access level of its members can depend on whether the data type of the variable is the underlying interface or the implementing class. 次に例を示します。The following example illustrates this.
Public Interface IDemo Sub DoSomething() End Interface Public Class implementIDemo Implements IDemo Private Sub DoSomething() Implements IDemo.DoSomething End Sub End Class Dim varAsInterface As IDemo = New implementIDemo() Dim varAsClass As implementIDemo = New implementIDemo()
varAsInterface
によってクラス メンバーにアクセスする場合、それらすべてにパブリック アクセス権が与えられます。If you access class members throughvarAsInterface
, they all have public access. ただし、varAsClass
によってメンバーにアクセスする場合は、Sub
プロシージャdoSomething
にプライベート アクセス権が与えられます。However, if you access members throughvarAsClass
, theSub
proceduredoSomething
has private access.範囲。Scope. インターフェイスは、その名前空間、クラス、構造体、またはモジュール全体をスコープとします。An interface is in scope throughout its namespace, class, structure, or module.
すべてのインターフェイス メンバーのスコープは、インターフェイス全体になります。The scope of every interface member is the entire interface.
有効期間。Lifetime. インターフェイス自体には、有効期間やメンバーがありません。An interface does not itself have a lifetime, nor do its members. クラスでインターフェイスを実装し、そのクラスのインスタンスとしてオブジェクトが作成される場合、そのオブジェクトには、それが実行されているアプリケーション内での有効期間があります。When a class implements an interface and an object is created as an instance of that class, the object has a lifetime within the application in which it is running. 詳細については、「Class ステートメント」の「有効期間」を参照してください。For more information, see "Lifetime" in Class Statement.
例Example
次の例では、Interface
ステートメントを使用して、thisInterface
という名前のインターフェイスを定義しています。これは、Property
ステートメントと Function
ステートメントで実装する必要があります。The following example uses the Interface
statement to define an interface named thisInterface
, which must be implemented with a Property
statement and a Function
statement.
Public Interface thisInterface
Property ThisProp(ByVal thisStr As String) As Char
Function ThisFunc(ByVal thisInt As Integer) As Integer
End Interface
Property
ステートメントと Function
ステートメントでは、インターフェイス内に End Property
および End Function
で終わるブロックを取り込んでいないことに注意してください。Note that the Property
and Function
statements do not introduce blocks ending with End Property
and End Function
within the interface. このインターフェイスでは、そのメンバーのシグネチャのみを定義しています。The interface defines only the signatures of its members. 完全な Property
と Function
のブロックは、thisInterface
を実装するクラスに存在します。The full Property
and Function
blocks appear in a class that implements thisInterface
.
関連項目See also
- インターフェイスInterfaces
- Class ステートメントClass Statement
- Module ステートメントModule Statement
- Structure ステートメントStructure Statement
- Property ステートメントProperty Statement
- Function ステートメントFunction Statement
- Sub ステートメントSub Statement
- Generic Types in Visual BasicGeneric Types in Visual Basic
- ジェネリック インターフェイスの変性Variance in Generic Interfaces
- InIn
- OutOut