Ring/課程/使用巢狀結構的宣告式程式設計
在本章中,我們將學習如何在面向物件的基礎上使用巢狀結構構建宣告式程式設計的世界。
我們將學習關於
- 在列表中建立物件
- 組合和透過引用返回物件和列表
- 在物件訪問結束時執行程式碼
- 面向物件的頂部宣告式程式設計
我們可以在列表定義期間在列表中建立物件。我們也可以使用 Add() 函式或 + 運算子隨時將物件新增到列表中。
示例
alist = [new point, new point, new point] # create list contains three objects
alist + [1,2,3] # add another item to the list
see "Item 4 is a list contains 3 items" + nl
see alist[4]
add(alist , new point)
alist + new point
alist[5] { x = 100 y = 200 z = 300 }
alist[6] { x = 50 y = 150 z = 250 }
see "Object inside item 5" + nl
see alist[5]
see "Object inside item 6" + nl
see alist[6]
class point x y z輸出
Item 4 is a list contains 3 items
1
2
3
Object inside item 5
x: 100.000000
y: 200.000000
z: 300.000000
Object inside item 6
x: 50.000000
y: 150.000000
z: 250.000000當我們使用組合並將物件作為類屬性之一時,當我們返回該物件時,它將透過引用返回。
如果被呼叫者使用了賦值運算子,則會建立物件的另一個副本。
呼叫者可以避免使用賦值運算子,並直接使用返回的引用訪問該物件。
如果屬性是列表(而不是物件),則也會執行相同操作。
.. 注意:: 物件和列表使用相同的規則處理。當您將它們傳遞給函式時,它們是透過引用傳遞的,當您將它們從函式中返回時,它們是透過值返回的,除非它是物件屬性,在這種情況下會執行透過引用返回。
示例
o1 = new Container
myobj = o1.addobj() # the assignment will create another copy
myobj.x = 100
myobj.y = 200
myobj.z = 300
see o1.aobjs[1] # print the object inside the container
see myobj # print the copy
Class Container
aObjs = []
func addobj
aobjs + new point
return aobjs[len(aobjs)] # return object by reference
Class point
x = 10
y = 20
z = 30輸出
x: 10.000000
y: 20.000000
z: 30.000000
x: 100.000000
y: 200.000000
z: 300.000000示例(2)
func main
o1 = new screen {
content[point()] {
x = 100
y = 200
z = 300
}
content[point()] {
x = 50
y = 150
z = 250
}
}
see o1.content[1]
see o1.content[2]
Class Screen
content = []
func point
content + new point
return len(content)
Class point
x = 10
y = 20
z = 30輸出
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000示例(3)
func main
o1 = New Screen {
point() { # access the object using reference
x = 100
y = 200
z = 300
}
point() { # access the object using reference
x = 50
y = 150
z = 250
}
}
see o1.content[1]
see o1.content[2]
Class Screen
content = []
func point
content + new point
return content[len(content)] # return the object by reference
Class point x=10 y=20 z=30輸出
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000我們可以使用 { } 訪問物件,以使用物件屬性和方法。
如果物件包含名為 BraceEnd() 的方法,則該方法將在物件訪問結束之前執行。
示例
New Point { See "How are you?" + nl }
Class Point x y z
func braceend
see "I'm fine, Thank you!" + nl輸出
How are you?
I'm fine, Thank you!接下來的功能使我們能夠在面向物件的基礎上使用巢狀結構構建和使用宣告式程式設計環境
- 使用 {} 訪問物件屬性和方法
- BraceEnd() 方法
- 透過引用返回物件
- Setter/Getter 方法(可選)
示例
# Declartive Programming (Nested Structures)
Screen()
{
point()
{
x = 100
y = 200
z = 300
}
point()
{
x = 50
y = 150
z = 250
}
}
# Functions and Classes
Func screen return new screen
Class Screen
content = []
func point
content + new point
return content[len(content)]
func braceend
see "I have " + len(content) + " points!"
Class point
x=10 y=20 z=30
func braceend
see self輸出
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000
I have 2 points!當我們可以避免在方法名稱後面編寫 () 時,我們可以獲得更好的結果和更漂亮的程式碼,因為這些方法不接受引數。此功能沒有由 Ring 語言直接提供,因為物件方法和物件屬性之間存在差異。當我們為物件屬性定義一個 getter 方法時,我們可以對程式碼的語法獲得類似的效果。例如,而不是定義 point() 方法,我們將定義 point 屬性,然後定義 getpoint() 方法,該方法將在您嘗試獲取 point 屬性的值時執行。由於我們直接編寫變數名,不帶 (),因此我們可以編寫 point 而不是 point(),而 getpoint() 方法將為我們建立物件並返回物件引用。
示例
new Container
{
Point
{
x=10
y=20
z=30
}
}
Class Container
aObjs = []
point
func getpoint
aObjs + new Point
return aObjs[len(aObjs)]
Class Point x y z
func braceend
see "3D Point" + nl + x + nl + y + nl + z + nl輸出
3D Point
10
20
30