Skip to content

STATIC(成员)


声明静态成员过程或变量

语法

vb
Type typename

Static variablename As DataType [, ...]
Declare Static Sub|Function procedurename ...
...

End Type

Dim typename.variablename As DataType [= initializer] [, ...]

[Static] Sub|Function typename.procedurename ...

...

End Sub|Function

说明

  • 静态成员过程
  • 静态成员变量

示例

start GeSHi

vb
'' Example showing how the actual procedure invoked by a member can be set at runtime.
'' using static member procedures.
Type _Object

  Enum handlertype
    ht_default
    ht_A
    ht_B
  End Enum

  Declare Constructor( ByVal ht As handlertype = ht_default)

  Declare Sub handler()

Private:
  Declare Static Sub handler_default( ByRef obj As _Object )
  Declare Static Sub handler_A( ByRef obj As _Object )
  Declare Static Sub handler_B( ByRef obj As _Object )
  handler_func As Sub( ByRef obj As _Object )

End Type

Constructor _Object( ByVal ht As handlertype )
  Select Case ht
  Case ht_A
    handler_func = @_Object.handler_A
  Case ht_B
    handler_func = @_Object.handler_B
  Case Else
    handler_func = @_Object.handler_default
  End Select
End Constructor

Sub _Object.handler()
  handler_func(This)
End Sub

Sub _Object.handler_default( ByRef obj As _Object )
  Print "Handling using default method"
End Sub

Sub _Object.handler_A( ByRef obj As _Object )
  Print "Handling using method A"
End Sub

Sub _Object.handler_B( ByRef obj As _Object )
  Print "Handling using method B"
End Sub

Dim objects(1 To 4) As _Object => _
  { _
    _Object.handlertype.ht_B, _
    _Object.handlertype.ht_default, _
    _Object.handlertype.ht_A _
  }
  '' 4th array item will be _Object.handlertype.ht_default

For i As Integer = 1 To 4
  Print i,
  objects(i).handler()
Next i

end GeSHi

start GeSHi

vb
'' Assign an unique ID to every instance of a Type (ID incremented in order of creation)

Type UDT
  Public:
    Declare Property getID () As Integer
    Declare Constructor ()
  Private:
    Dim As Integer ID
    Static As Integer countID
End Type
Dim As Integer UDT.countID = 0

Property UDT.getID () As Integer
  Property = This.ID
End Property

Constructor UDT ()
  This.ID = UDT.countID
  UDT.countID += 1
End Constructor

Dim As UDT uFirst
Dim As UDT uSecond
Dim As UDT uThird

Print uFirst.getID
Print uSecond.getID
Print uThird.getID

end GeSHi

与 QB 的区别

  • FreeBASIC 新增

另请参阅

  • Class
  • Declare
  • Type
  • Static

返回 目录

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