Skip to content

PROPERTY


在类型或类中声明或定义属性

语法

vb
{ Type | Class } typename

declare Property fieldname () [ byref ] as datatype
declare Property fieldname ( [ byref | byval ] new_value as datatype )
declare Property fieldname ( [ byref | byval ] index as datatype ) [ byref ] as datatype
declare Property fieldname ( [ byref | byval ] index as datatype, [ byref | byval ] new_value as datatype )

End { Type | Class }

Property typename.fieldname () [ byref ] as datatype [ Export ]

statements

End Property

Property typename.fieldname ( [ byref | byval ] new_value as datatype ) [ Export ]

statements

End Property

Property typename.fieldname (  [ byref | byval ] index as datatype ) [ byref ] as datatype [ Export ]

statements

End Property

Property typename.fieldname (  [ byref | byval ] index as datatype, [ byref | byval ] new_value as datatype ) [ Export ]

statements

End Property

参数

typename

TypeClass 的名称

fieldname

属性的名称

new_value

传递给属性的待赋值

index

属性索引值

说明

Property 字段用于获取和设置 TypeClass 的值,方式与其他数据字段相同,区别在于不是简单地对字段赋值或从字段检索值,而是执行一个过程。

typename 是声明和定义 Property 方法的类型名称。在 Namespace 中使用时,typename 的名称解析遵循与过程相同的规则。

属性可以有一个可选的 index 参数。当带索引时,属性的访问方式为 fieldname(Index) [ = value ]

没有 index 参数的属性必须始终在 fieldname 后不使用空括号。

get 属性也可以通过指定 byref as return_type 来返回引用。

一个隐藏的 this 参数(与 typename 类型相同)会被传递给属性过程。this 用于访问 TypeClass 的字段。

注意:标准属性(get 和 set)不支持组合运算符(如 "+=")。但 byref 返回结果的 get 属性(更一般地说,任何 byref 返回结果的函数)支持组合运算符。

注意:当 get 属性定义了一个 index 参数时,不能使用 fieldname= 语法来返回值。对于这样的 get 属性,property= 语法(除了 return 语法之外)是唯一允许的语法。

示例

start GeSHi

vb
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

end GeSHi

输出:

a = (3, 4)
a.length =  5

a = (6, 8)
a.length =  10

属性索引:

start GeSHi

vb
  '' True/False
Namespace BOOL
  Const FALSE = 0
  Const TRUE = Not FALSE
End Namespace

Type BitNum
  Num As UInteger
 
    '' Get/Set Properties each with an Index.
  Declare Property NumBit( ByVal Index As Integer ) As Integer
  Declare Property NumBit( ByVal Index As Integer, ByVal Value As Byte )
End Type

  '' Get a bit by it's index.
Property BitNum.NumBit( ByVal Index As Integer ) As Integer
  Return Bit( This.Num, Index )
End Property

  '' Set a bit by it's index.
Property BitNum.NumBit( ByVal Index As Integer, ByVal Value As Byte )

    '' Make sure index is in Integer range.
  If Index >= ( SizeOf(This.Num) * 8 ) Then
    Print "Out of uInteger Range!"
    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 "Testing property indexing with data types:"
Print "FOO Number's Value: " & Foo.Num

  '' Set the bit in the number as true.
Foo.NumBit(31) = BOOL.TRUE
Print "Set the 31st bit of FOO"

  '' Print to see if our bit has been changed.
Print "FOO Number's Value: " & Foo.Num
Print "FOO 31st Bit Set? " & Foo.NumBit(31)
Sleep
Print ""

end GeSHi

输出:

Testing property indexing with data types:
FOO Number's Value: 0
Set the 31st bit of FOO
FOO Number's Value: 2147483648
FOO 31st Bit Set? -1

另请参阅

  • Class
  • Type

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系