環/教程/面向物件程式設計 (OOP)
在本章中,我們將學習如何在 Ring 程式語言中使用面向物件程式設計正規化。
我們將學習關於
- 類和物件
- 使用大括號訪問物件
- 組合
- Setter 和 Getter
- 私有屬性和方法
- 運算子過載
- 繼承
- 動態屬性
- 包
我們可以使用以下語法定義新類
語法
Class <Class Name> [From <Parent Class Name>]
[Attributes]
[Methods]
[Private
[Attributes]
[Methods]
]我們可以使用以下語法建立物件
語法
New <Object Name> [ (init method parameters) ] |
[ { access object data and methods } ] ---> Object示例
New point { x=10 y=20 z=30 print() }
Class Point x y z func print see x + nl + y + nl + z + nl.. 注意:我們可以使用 { } 來訪問物件資料和方法。
.. 提示:我們可以在類名後直接宣告類屬性。
輸出
10
20
30我們可以用另一種方式重寫相同的程式
New point # create new object using the point class
{ # access the new object attributes and methods
x = 10 # set the x attribute to 10
y = 20 # set the y attribute to 20
z = 30 # set the z attribute to 30
print() # call the print method
} # end of object access
類 Point # 定義 Point 類 x y z # 類包含三個屬性 x、y 和 z func print # 定義 print 方法 see x + nl + # 列印 x 屬性 y + nl + # 列印 y 屬性 z + nl # 列印 z 屬性
我們也可以用另一種方式編寫相同的程式
P1 = New Point
P1.x = 10
P1.y = 20
P1.z = 30
P1.Print()
Class Point x y z func print see x + nl + y + nl + z + nl.. 注意:我們可以使用點運算子在物件名後訪問物件成員。
我們也可以用另一種方式編寫相同的程式
new point { print() }
Class Point
x = 10 y = 20 z = 30
func print see x + nl + y + nl + z + nl.. 注意:我們可以在宣告類屬性時為其設定預設值。
我們也可以用另一種方式編寫相同的程式
new point(10,20,30)
Class Point
x y z
func init p1,p2,p3 x=p1 y=p2 z=p3 print()
func print see x + nl + y + nl + z + nl.. 注意:我們可以在建立新物件時直接使用 () 呼叫 init 方法
我們也可以用另一種方式編寫相同的程式
new point( [ :x = 10 , :y = 20 , :z = 30 ] )
Class Point x y z
func init aPara x = aPara[:x] y = aPara[:y] z = aPara[:z] print()
.. 提示:使用 Hash 傳遞方法引數使我們能夠建立可選的
parameters and change the order of parameters when adding them to the Hash.
我們可以使用大括號 { } 在任何時候訪問物件。
在大括號內,我們可以直接使用物件的屬性和方法。
這可以在我們使用 New 關鍵字建立物件時完成,或者在任何時候使用以下語法完成
ObjectName { access object data and methods }示例
See "Creating the Object" + nl
o1 = new Point
See "Using the Object" + nl
o1 {
x=5
y=15
z=25
print()
}
Class Point x y z func print see x + nl + y + nl + z當我們呼叫函式或方法時,可以使用大括號來訪問物件。
示例
o1 = new Point
print( o1 { x=10 y=20 z=30 } )
func print object
see object.x + nl +
object.y + nl +
object.z
Class Point x y z我們可以在同一個表示式中混合使用大括號和點運算子來訪問物件。
示例
o1 = new Point
O1 { x=10 y=20 z=30 }.print()
Class Point x y z
func print see x + nl + y + nl + z物件可以包含其他物件作為屬性。
可以使用大括號巢狀訪問物件。
示例
R1 = New Rectangle
{
Name = "Rectangle 1"
P1
{
X = 10
Y = 20
}
P2
{
X = 200
Y = 300
}
Color = "Blue"
}
see "Name : " + R1.Name + nl +
"Color: " + R1.Color + nl +
"P1 : (" + R1.P1.X + "," + R1.P1.Y + ")" + nl +
"P2 : (" + R1.P2.X + "," + R1.P2.Y + ")"
Class Rectangle
name color
p1 = new Point
p2 = new Point
Class Point x y輸出
Name : Rectangle 1
Color: Blue
P1 : (10,20)
P2 : (200,300)我們可以定義方法,在設定和獲取物件屬性時使用。
語法
Class ClassName
AttributeName
...
Func SetAttributeName
...
Func GetAttributeName
...示例
o1 = new person
o1.name = "Mahmoud" see o1.name + nl
o1 { name = "Ahmed" see name }
Class Person
name family = "Fayed"
func setname value
see "Message from SetName() Function!" + nl
name = value + " " + family
func getname
see "Message from GetName() Function!" + nl
return "Mr. " + name輸出
Message from SetName() Function!
Message from GetName() Function!
Mr. Mahmoud Fayed
Message from SetName() Function!
Message from GetName() Function!
Mr. Ahmed Fayed我們可以在類體中 private 關鍵字後定義私有屬性和方法。
示例
o1 = new person {
name = "Test"
age = 20
print()
o1.printsalary()
}
try
see o1.salary
catch
see cCatchError + nl
done
try
o1.increasesalary(1000)
catch
see cCatchError + nl
done
Class Person
name age
func print
see "Name : " + name + nl +
"Age : " + age + nl
func printsalary
see "Salary : " + salary + nl
private
salary = 15000
func increasesalary x
salary += x輸出
Name : Test
Age : 20
Salary : 15000
Error (R27) : Using private attribute from outside the class : salary
Error (R26) : Calling private method from outside the class : increasesalary我們可以將運算子方法新增到我們的類中,以允許對類物件使用運算子。
語法
Class ClassName
...
Func operator cOperator,Para
...函式運算子有兩個引數,第一個表示運算子,第二個表示運算子後的第二個引數。
示例
o1 = new point { x = 10 y = 10 print("P1 : ") }
o2 = new point { x = 20 y = 40 print("P2 : ") }
o3 = o1 + o2
o3.print("P1+P2 : ")
class point x y
func operator cOperator,Para
result = new point
switch cOperator
on "+"
result.x = x + Para.x
result.y = y + Para.y
on "-"
result.x = x - Para.x
result.y = y - Para.y
off
return result
func print cPoint
see cPoint + "X : " + x + " Y : " + y + nl輸出
P1 : X : 10 Y : 10
P2 : X : 20 Y : 40
P1+P2 : X : 30 Y : 50我們可以在類定義中使用 from 關鍵字從另一個類建立類。
語法
Class <Class Name> [From <Parent Class Name>]我們可以使用 super 物件從子類呼叫父類中的方法。
語法
func methodname
...
super.methodname()
...示例
Func main
e1 = new Employee {
Name = "test"
age = 20
job = "programmer"
salary = 20000000
print()
}
Class Human
Name Age
func print
see "Name : " + name + nl + "Age : " + age + nl
Class Employee from Human
Job Salary
func print
super.print()
see "Job : " + job + nl + "Salary : " + salary + nl輸出
Name : test
Age : 20
Job : programmer
Salary : 20000000我們可以在類名後編寫指令,以便在建立新物件時執行這些指令。
示例
o1 = new dynamicClass
see o1.var5 + nl # output 5
Class DynamicClass
for x = 1 to 10
cStr = "var" + x + " = " + x
eval(cStr)
next.. 提示:在前面的示例中,var1、var2、...、var10 將被定義為屬性。
.. 提示:前面的示例的問題是,x 和 cStr 也將被定義為屬性!
.. 注意:我們可以在字串中編寫類定義,然後使用 eval() 執行字串以定義類。
我們可以使用以下語法建立一個包(一組具有共同名稱的類)。
package PackageName
Class Class1
...
Class Class2
...
Class Class3
...
...示例
o1 = new System.output.console
o1.print("Hello World")
Package System.Output
Class Console
Func Print cText
see cText + nl.. 注意:我們可以使用點運算子作為包名稱的一部分。
我們可以使用 import 命令而不是鍵入 PackageName.ClassName 的長名稱。
當我們匯入一個包時,我們可以直接使用這個包中的任何類。
示例
import system.output
o1 = new console {
print("Hello World")
}
Package System.Output
Class Console
Func Print cText
see cText + nl我們可以使用 see 命令列印物件的 state(屬性和值)。
示例
see new point { x=10 y=20 z=30 }
class point x y z輸出
x: 10.000000
y: 20.000000
z: 30.000000