FreeBASIC Property
目次→言語リファレンス→変数とデータ型→ユーザ定義型→PROPERTY←オリジナル・サイト
型かクラスで、特性を宣言するか、または定義します。
構文:
パラメタ:
typename
fieldname
特性の名前
new_value
代入するために特性に渡される値
index
特性のインデックス値
記述:
Property の項目は、
Type や
Class の値を取得・設定するために使われます。他のデータ項目との違いは、項目や値に単純に代入するのではなく、項目から検索して、手続きが実行されることです。
typename は、
Property メソッド(操作命令) が宣言され、定義される、型の名前です。
typename の名前解決は、
Namespaceで使われる手続きと、同じ規則に従います。
Property には、任意で、1つの
index パラメタを設定できます。
索引をつけると、特性には、
fieldname(Index) = Value で、アクセスできます。
index パラメータのない
Property は、
fieldname の後に空の括弧を付けずに常に使用する必要があります。
get-
Property は、
byref as return_type を指定することによって、参照を返すこともできます。
typename と同じ型を持った、隠された
this パラメータは、特性の手続きに渡されます。
this は、
Type か
Class の項目にアクセスするために、使われます。
注意:
標準の
Property (get & set) は、組み合わせ演算子("+=" として)では動作しません。
しかし、結果 byref get-
Property (より一般的に、任意の ByRef 関数の結果として)は、組み合わせ演算子で動作します。
注意:
get-
Property が 1つの
index パラメータで定義されている場合、
fieldname= 構文を使って値を返すことはできません。
このような get-
Property の場合、(
return 構文に加えて)
property= 構文だけが、許可されています。
例:
Type Vector2D
As Single x, y
Declare Operator Cast() As String
Declare Property Length() As Single
Declare Property Length( ByVal new_length As Single )
End Type
Operator Vector2D.cast () As String
Return "(" + Str(x) + ", " + Str(y) + ")"
End Operator
Property Vector2D.Length() As Single
Length = Sqr( x * x + y * y )
End Property
Property Vector2D.Length( ByVal new_length As Single )
Dim m As Single = Length
If m <> 0 Then
'' new vector = old / length * new_length
x *= new_length / m
y *= new_length / m
End If
End Property
Dim a As Vector2D = ( 3, 4 )
Print "a(ベクトル) = "; a
Print "a.length(ベクトルの長さ) = "; a.length
Print
a.length = 10
Print "a(ベクトル) = "; a
Print "a.length(ベクトルの長さ) = "; a.length
Sleep
出力:
a(ベクトル) = (3, 4)
a.length(ベクトルの長さ) = 5
a(ベクトル) = (6, 8)
a.length(ベクトルの長さ) = 10
特性のインデックス:
'' True/False
Namespace BOOL
Const FALSE = 0
Const TRUE = Not FALSE
End Namespace
Type BitNum
Num As UInteger
'' Index でそれぞれ、Properties を取得、設定します
Declare Property NumBit( ByVal Index As Integer ) As Integer
Declare Property NumBit( ByVal Index As Integer, ByVal Value As Byte )
End Type
'' その索引で、ビットを取得します
Property BitNum.NumBit( ByVal Index As Integer ) As Integer
Return Bit( This.Num, Index )
End Property
'' その索引で、ビットを設定します
Property BitNum.NumBit( ByVal Index As Integer, ByVal Value As Byte )
'' インデックスが、Integer の範囲にあるのを確認します
If Index >= ( SizeOf(This.Num) * 8 ) Then
Print "uInteger の範囲外です!"
Exit Property
Else
If Index < 0 Then Exit Property
End If
If Value = BOOL.FALSE Then
This.Num = BitReset( This.Num, Index )
End If
If Value = BOOL.TRUE Then
This.Num = BitSet( This.Num, Index )
End If
End Property
Dim As BitNum Foo
Print "データ型で、特性の索引を付けるテスト:"
Print "FOO 番号の値: " & Foo.Num
'' true として、数にビットを設定します
Foo.NumBit(31) = BOOL.TRUE
Print "FOO の 31 番目の bit を設定します"
'' ビットが変化したかかどうかを見るために、表示します
Print "FOO 番号の値: " & Foo.Num
Print "FOO の 31 番目の Bit の設定は? " & Foo.NumBit(31)
Sleep
Print ""
出力:
データ型で、特性の索引を付けるテスト:
FOO 番号の値: 0
FOO の 31 番目の bit を設定します
FOO 番号の値: 2147483648
FOO の 31 番目の Bit の設定は? -1
参照:
ページ歴史:2021-12-07 02:40:23
日本語翻訳:WATANABE Makoto、原文著作者:JeffMarshall