不幸的是,FreeBASIC 有一个不优雅的类管理,并且有一些限制,当你创建一个类时,你不能把函数或子类放在里面,但是你可以声明它们并稍后在类型之外构建它们,下面我给你写了一个源代码,它显示了它是如何实现的是结构化的,当你创建一个在类中声明的函数时,它必须以类名开头,用点分隔
'This one define "IsEven(value)"
'that give true or 1 if the vaule n are Even number
#define IsEven(n) ((n AND 1) = 0)
'I make a class type where inside are 2 vars and 2 functions
type Useless
' this is the vars as integer
dim StartNum as integer
dim CurrentNum as integer
' this are the functions declared we ave to declare the function who work inside the class
declare function EvenValue(byval value as integer)as integer
declare function OddValue(byval value as integer)as integer
declare function FinalValue()as string
end type
' After declare the function inside the type (class) we ave to use the
' name of type followed with the name of the function separated by dot.
'
' inside type "declare myfunction()as integer"
' the function can be string too or other type
'
' when close the type with "End Type" you can make the function
' function nameclass.myfunction()as integer
function Useless.EvenValue(byval value as integer)as integer
dim Result as integer
Result = (value / 2)
return Result
end function
function Useless.OddValue(byval value as integer)as integer
dim Result as integer
Result = ((value * 3) + 1 )
return Result
end function
function Useless.FinalValue()as string
'with this you can use every function, var or sub
'inside the class(type)
'watever name you used for the object
dim OutMess as String
OutMess = "Start value =" + str(this.StartNum) + " ---- End value =" + str(this.CurrentNum)
return OutMess
end function
'this are global var outside of class(type)
dim thenexit as integer = 0
'this is the object created as class
dim Calculate as Useless
'message text and store imput inside the public integer var inside the object
print "type a number"
input Calculate.StartNum
'you can change value taked from other var from the object
Calculate.CurrentNum = Calculate.StartNum
'We use the do until cicle because we know at last we ave the one value
do until(thenexit > 0)
if IsEven(Calculate.CurrentNum) then
'we use the object.function for even value
Calculate.CurrentNum = Calculate.EvenValue(Calculate.CurrentNum)
else
'we use the object.function for odd value
Calculate.CurrentNum = Calculate.OddValue(Calculate.CurrentNum)
end if
print Calculate.CurrentNum
if Calculate.CurrentNum = 1 then
'and at the end we use the function with shared vars
print Calculate.FinalValue
thenexit = 1
end if
loop