環/課程/反射和超程式設計
由於 Ring 程式語言是一種動態語言,我們可以在執行時獲取有關程式程式碼的資訊,並修改程式碼。
在本章中,我們將學習有關反射和超程式設計的概念,以及可用的函式。
我們可以使用 locals() 函式獲取當前作用域中變數名稱的列表。
語法
locals() --> a list contains the variables names in the current scope示例
test("hello")
func test cMsg
see cMsg + nl
x = 10
y = 20
z = 30
see locals()輸出
hello
cmsg
x
y
z我們可以使用 globals() 函式獲取全域性作用域中變數名稱的列表。
語法
globals() --> a list contains variables names in the global scope示例
x=10 y=20 z=30
test()
func test
see "message from test()" + nl +
"Global Variables:" + nl
see globals()輸出
message from test()
Global Variables:
x
y
z我們可以使用 functions() 函式獲取 Ring 語言中函式名稱的列表。
語法
functions() --> a list contains functions names示例
see functions()
func f1
see "f1" + nl
func f2
see "f2" + nl
func f3
see "f3" + nl輸出
f1
f2
f3我們可以使用 cfunctions() 函式獲取 C 語言中函式名稱的列表。
語法
cfunctions() --> a list contains functions names示例
aList = cfunctions()
See "Count : " + len(aList) + nl
for x in aList
see x + "()" + nl
next輸出
Count : 186
len()
add()
del()
get()
clock()
..... 注意:: 完整的列表已從之前的輸出中移除。
我們可以使用 islocal() 函式檢查變數是否在本地作用域中定義。
語法
islocal(cVariableName) --> returns 1 if the variable is defined in the local scope
returns 0 if the variable is not defined in the local scope示例
test()
func test
x=10 y=20
see islocal("x") + nl +
islocal("y") + nl +
islocal("z") + nl輸出
1
1
0
我們可以使用 isglobal() 函式檢查變數是否在全域性作用域中定義。
語法
isglobal(cVariableName) --> returns 1 if the variable is defined in the global scope
returns 0 if the variable is not defined in the global scope示例
x=10 y=20
test()
func test
see isglobal("x") + nl +
isglobal("y") + nl +
isglobal("z") + nl輸出
1
1
0我們可以使用 isfunction() 函式檢查 Ring 函式是否已定義。
語法
isfunction(cFunctionName) --> returns 1 if the Ring function is defined
returns 0 if the Ring function is not defined示例
see isfunction("f1") + nl +
isfunction("f2") + nl +
isfunction("f3") + nl
func f1
see "message from f1()" + nl
func f2
see "message from f2()" + nl輸出
1
1
0我們可以使用 iscfunction() 函式檢查 C 函式是否已定義。
語法
iscfunction(cFunctionName) --> returns 1 if the C function is defined
returns 0 if the C function is not defined示例
see iscfunction("len") + nl +
iscfunction("test") + nl輸出
1
1
0
我們可以使用 packages() 函式獲取包名稱的列表。
語法
packages() --> a list contains packages names示例
See packages()
Package Package1
Class class1
Func f1
Package Package2
Class class1
Func f1
Package Package3
Class class1
Func f1
Package Package4
Class class1
Func f1輸出
package1
package2
package3
package4
我們可以使用 ispackage() 函式檢查包是否已定義。
語法
ispackage(cPackageName) --> returns 1 if the Package is defined
returns 0 if the Package is not defined示例
See ispackage("package1") + nl +
ispackage("package4") + nl +
ispackage("package5") + nl +
ispackage("package3") + nl
Package Package1
Class class1
Func f1
Package Package2
Class class1
Func f1
Package Package3
Class class1
Func f1
Package Package4
Class class1
Func f1輸出
1
1
0
1
我們可以使用 classes() 函式獲取類名稱的列表。
語法
classes() --> a list contains classes names示例
See classes()
Class class1
Func f1
Class class2
Func f1
Class class3
Func f1輸出
class1
class2
class3我們可以使用 isclass() 函式檢查類是否已定義。
語法
isclass(cClassName) --> returns 1 if the Class is defined
returns 0 if the Class is not defined示例
see isclass("class4") + nl +
isclass("class3") + nl +
isclass("class2") + nl
Class class1
func f1
class class2
func f1
class class3
func f1輸出
0
1
1我們可以使用 packageclasses() 函式獲取包中類名稱的列表。
語法
packageclasses(cPackageName) --> a list contains classes names inside the package示例
see "classes in Package1" + nl
see packageclasses("Package1")
see "classes in Package2" + nl
see packageclasses("Package2")
Package Package1
Class class1
Func f1
Package Package2
Class class1
Func f1
Class class2
Func f1
Class class3
func f1
輸出
classes in Package1
class1
classes in Package2
class1
class2
class3
我們可以使用 ispackageclass() 函式檢查類是否在包中定義。
語法
ispackageclass(cPackageName,cClassName) --> returns 1 if the Class is defined
returns 0 if the Class is not defined示例
see ispackageclass("package1","class1") + nl +
ispackageclass("package1","class2") + nl +
ispackageclass("package2","class1") + nl +
ispackageclass("package2","class2") + nl
Package Package1
Class class1
Func f1
Package Package2
Class class1
Func f1
Class class2
Func f1
Class class3
func f1輸出
1
0
1
1我們可以使用 classname() 函式獲取物件的類名
語法
classname(object) --> Returns the object class name
示例
o1 = new point
o2 = new rect
see classname(o1) + nl # print point
see classname(o2) + nl # print rect
class point
class rect我們可以使用 objectid() 函式獲取物件 ID
語法
objectid(object) --> Returns the object id示例
o1 = new point
see objectid(o1) + nl
test(o1)
func test v
see objectid(v) + nl
Class point x y z輸出
021B5808
021B5808
我們可以使用 isobject() 函式檢查變數是否為物件。
語法
isobject(variable) --> Returns True if it's an object, False if it's not
我們可以使用 attributes() 函式獲取物件的屬性
語法
attributes(object) --> Returns a list contains the object attributes示例
o1 = new point
aList = attributes(o1) # we can use see attributes(o1)
for t in aList see t next # print xyz
Class Point x y z
我們可以使用 methods() 函式獲取物件的方法
語法
methods(object) --> Returns a list contains the object methods
示例
o1 = new test
aList = methods(o1)
for x in aList
cCode = "o1."+x+"()"
eval(cCode)
next
Class Test
func f1
see "hello from f1" + nl
func f2
see "hello from f2" + nl
func f3
see "hello from f3" + nl
func f4
see "hello from f4" + nl輸出
hello from f1
hello from f2
hello from f3
hello from f4
我們可以使用 isattribute() 函式測試物件是否包含一個屬性
語法
isattribute(object,cAttributeName) --> Returns True if the object contains the attribute示例
o1 = new point
see isattribute(o1,"x") + nl # print 1
see isattribute(o1,"t") + nl # print 0
see isattribute(o1,"y") + nl # print 1
see isattribute(o1,"z") + nl # print 1
class point x y z我們可以使用 isprivateattribute() 函式測試物件是否包含一個私有屬性
語法
isprivateattribute(object,cAttributeName) --> Returns True if the object
contains the private attribute示例
o1 = new person
see isprivateattribute(o1,"name") + nl +
isprivateattribute(o1,"address") + nl +
isprivateattribute(o1,"phone") + nl +
isprivateattribute(o1,"job") + nl +
isprivateattribute(o1,"salary")
Class Person
name address phone
private
job salary輸出
0
0
0
1
1
我們可以使用 ismethod() 函式測試物件的類是否包含一個方法
語法
ismethod(object,cMethodName) --> Returns True if the object class contains the method示例
o1 = new point
see ismethod(o1,"print") + nl # print 1
mylist = []
mylist + new point
see ismethod(mylist[1],"print") + nl # print 1
class point x y z
func print
see x + nl + y + nl + z + nl我們可以使用 isprivatemethod() 函式測試物件的類是否包含一個私有方法
語法
isprivatemethod(object,cMethodName) --> Returns True if the object class contains
the private method示例
o1 = new Test
see isprivatemethod(o1,"f1") + nl +
isprivatemethod(o1,"f2")
Class Test
func f1
see "message from f1()" + nl
private
func f2
see "message from f2()" + nl輸出
0
1我們可以使用 addattribute() 函式向物件狀態(而不是類)新增一個屬性(或一組屬性)
語法
AddAttribute(object,cAttributeName|aAttributesList)示例(1)
see new point {x=10 y=20 z=30}
Class Point
AddAttribute(self,["x","y","z"])示例(2)
o1 = new point
addattribute(o1,"x")
addattribute(o1,"y")
addattribute(o1,"z")
see o1 {x=10 y=20 z=30}
class point
輸出
x: 10.000000
y: 20.000000
z: 30.000000
我們可以使用 addmethod() 函式向物件的類新增一個方法。這個方法可以與來自同一類的任何物件一起使用。
語法
AddMethod(Object,cNewMethodName,cMethodName|AnonymousFunction)示例
o1 = new point { x=10 y=20 z=30 }
addmethod(o1,"print", func { see x + nl + y + nl + z + nl } )
o1.print()
Class point
x y z輸出
10
20
30我們可以使用函式名稱,而不是使用匿名函式來向類新增新方法
示例
o1 = new point { x=10 y=20 z=30 }
myfunc = func { see x + nl + y + nl + z + nl }
addmethod(o1,"print", myfunc )
addmethod(o1,"display", myfunc )
addmethod(o1,"show", myfunc )
o1.print()
o1.display()
o1.show()
Class point
x y z輸出
10
20
30
10
20
30
10
20
30由於我們將方法新增到類中,因此來自該類的任何物件都可以使用此方法
示例
o1 = new point { x=10 y=20 z=30 }
o2 = new point { x=100 y=200 z=300 }
o3 = new point { x=50 y=150 z=250 }
addmethod(o1,"print", func { see x + nl + y + nl + z + nl } )
o1.print()
o2.print()
o3.print()
Class point
x y z輸出
10
20
30
100
200
300
50
150
250我們可以使用 getattribute() 函式獲取物件的屬性值
語法
GetAttribute(oObject,cAttributeName) ---> Attribute Value示例
o1 = new point
see getattribute(o1,"name") + nl +
getattribute(o1,"x") + nl +
getattribute(o1,"y") + nl +
getattribute(o1,"z") + nl
Class Point
x=10 y=20 z=30
name = "3D-Point"輸出
3D-Point
10
20
30我們可以使用 setattribute() 函式設定物件的屬性值
語法
SetAttribute(oObject,cAttributeName,Value)示例
o1 = new person
setattribute(o1,"cName","Mahmoud")
setattribute(o1,"nSalary",1000000)
setattribute(o1,"aColors",["white","blue","yellow"])
see o1
see o1.aColors
Class Person
cName
nSalary
aColors輸出
cname: Mahmoud
nsalary: 1000000.000000
acolors: List...
white
blue
yellow
我們可以使用 MergeMethods() 函式在類之間共享方法,而無需繼承
這個函式將類方法合併到另一個類中。
語法
MergeMethods(cClassNameDestination,cClassNameSource)示例
mergemethods("count","share")
mergemethods("count2","share")
o1 = new count { test() }
o1 = new count2 { test() }
Class Share
func one
see "one" + nl
func two
see "two" + nl
func three
see "three" + nl
Class Display
Func printline
see copy("*",20) + nl
Class Count from Display
Func test
printline()
one()
two()
three()
printline()
Class Count2 from Display
Func test
three()
two()
one()
printline()輸出
********************
one
two
three
********************
three
two
one
********************