FreeBASIC VisProtected
目次→言語リファレンス→変数とデータ型→ユーザ定義型→PROTECTED:(Access Control)←オリジナル・サイト
Type か Class で、保護された要素アクセス制御を、指定します。
構文:
Type typename
Protected:
member declarations
End Type
パラメタ:
typename
member declarations
項目、関数、または列挙のための、宣言
記述:
Protected: は、それに続く
member declarations が、アクセス保護されていることを、示します。
保護されたメンバーは、その
Type か
Class のメンバー手続き内と、この
Type か
Class から派生したクラスからのみアクセスできます。
このようなメンバー手続きの内部から見ると、保護されたメンバーが実際にはパブリックであり、アクセス演算子が適用されるオブジェクトに関係なく、パブリックであるかのように見えます。
Protected: に続く
member declarations
は、異なったアクセス制御指定子(
Private: か
Public:)が与えられるまで、保護されます。
Type 宣言におけるメンバーは、メンバーアクセス制御指定子を与えない場合、デフォルトで、
Public: です。
例:
Type animal
Dim As String animalName
Protected:
Dim As Integer serialNumber
End Type
Type dog Extends animal
Dim As String masterName
Declare Sub setSerialNumber ( ByVal number As Integer )
End Type
Sub dog.setSerialNumber ( ByVal number As Integer )
'' これは OK。 派生型のメンバー関数の中にあります。
This.serialNumber = number
End Sub
Dim As dog d
'' これは OK。animalName は public です。
d.animalName = "Buddy"
'' これはコンパイル・エラーが発生するでしょう:
'' - serialNumber is protected and we're trying to access it outside its type and the derived type
'' - serialNumber は保護されています。その型と派生型の外部でアクセスしようとしています
'' d.serialNumber = 123456789
' アクセス制御「Protected」を示す例:ユーザー権利としての管理者権限が提供するトークンを使う:
' - The 'admin_right' type extends the 'user_right' type.
' - Create directly an 'user_right' object is forbidden.
' ('default user_right.constructor' access and 'copy user_right.constructor' access are 'Protected')
' - The 'user_right' type has only the access right to get the token.
' ('user_right.token' get-property access is 'Public' and 'user_right.token' set-property access is 'protected')
' - The 'admin_right' type has the access rights to set and to get the token.
' ('admin_right.token' get-property access and 'admin_right.token' set-property access are 'Public')
'
' 'admin_right' オブジェクトが作成され、型'user_right' のこのオブジェクトへの参照が、定義されます。
' (create directly an 'user_right' object is forbidden)
Type user_right
Public:
Declare Property token () As String '' 'Public' to authorize user_right token get
Protected:
Declare Constructor () '' 'Protected' to forbid user_right object default-construction
Declare Constructor (ByRef u As user_right) '' 'Protected' to forbid user_right object copy-construction
Declare Property token (ByRef s As String) '' 'Protected' to forbid user_right token set
Private:
Dim As String user_right_token '' 'Private' to forbid access from outside user_right
End Type
Constructor user_right () '' Default-constructor
End Constructor
Constructor user_right (ByRef u As user_right) '' Protected copy-constructor
This.user_right_token = u.user_right_token
End Constructor
Property user_right.token () As String '' Public property user_right token get
Return This.user_right_token
End Property
Property user_right.token (ByRef s As String) '' Protected property user_right token set
This.user_right_token = s
End Property
Type admin_right Extends user_right
Public:
Declare Property token () As String '' 'Public' to authorize admin_right token get
Declare Property token (ByRef s As String) '' 'Public' to authorize admin_right token set
End Type
Property admin_right.token () As String '' Public property admin_right token get
Return Base.token '' 'Base.' to access to the base type property shadowed by this property name
End Property
Property admin_right.token (ByRef s As String) '' Public property admin_right token set
Base.token = s '' 'Base.' to access to the base type property shadowed by this property name
End Property
Dim As admin_right ar '' Create an admin_right type object 'ar'
ar.token = "fxm123456789" '' admin_right set the token for user_right
Print "'" & ar.token & "'" '' admin_right get the user_right token
Print
Dim ByRef As user_right ur = ar '' Create a user_right type reference 'ur' to the 'ar' instance of admin_right type
Print "'" & ur.token & "'" '' user_right get its token
'ur.token = "fxm0" '' Error: Illegal member access, USER_RIGHT.TOKEN.property.set (user_right cannot set its token)
'Dim As user_right ur1 '' Error: The default constructor has no public access
'Dim As user_right ur2 = ar '' Error: Constructor has no public access
Sleep
方言差:
QBからの違い:
参照:
ページ歴史:2022-04-23 02:18:13
日本語翻訳:WATANABE Makoto、原文著作者:ChaOs