Rexx - 面向物件



按照環境章節中的說明安裝ooRexx後,您將能夠使用類和物件。請注意,以下所有程式碼都需要在ooRexx直譯器中執行。普通的Rexx直譯器將無法執行此面向物件的程式碼。

類和方法宣告

類的定義使用以下語法宣告。

語法

::class classname 

其中classname是賦予類的名稱。

類中的方法使用以下語法宣告定義。

語法

::method methodname 

其中methodname是賦予方法的名稱。

類的屬性使用以下語法宣告定義。

語法

::attribute propertyname 

其中propertyname是賦予屬性的名稱。

示例

以下是Rexx中類的示例。

::class student 
::attribute StudentID 
::attribute StudentName 

關於上述程式,需要注意以下幾點。

  • 類的名稱是student。
  • 該類有兩個屬性,StudentID和StudentName。

Getter和Setter方法

Getter和Setter方法用於自動設定和獲取屬性的值。在Rexx中,當您使用attribute關鍵字宣告屬性時,Getter和Setter方法已經就位。

示例

::class student 
::attribute StudentID 
::attribute StudentName 

在上例中,將存在StudentId和StudentName的Getter和Setter方法。

以下程式顯示瞭如何使用它們。

/* Main program */ 
value = .student~new 
value~StudentID = 1 
value~StudentName = 'Joe' 
say value~StudentID 
say value~StudentName 

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 

上述程式的輸出將如下所示。

1 
Joe 

例項方法

可以透過~new運算子從類建立物件。可以按照以下方式呼叫類中的方法。

Object~methodname 

其中methodname是需要從類中呼叫的方法。

示例

以下示例顯示瞭如何從類建立物件以及如何呼叫相應的方法。

/* Main program */ 
value = .student~new 
value~StudentID = 1 
value~StudentName = 'Joe' 
value~Marks1 = 10 
value~Marks2 = 20 
value~Marks3 = 30 
total = value~Total(value~Marks1,value~Marks2,value~Marks3) 
say total 

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method 'Total' 
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

上述程式的輸出將如下所示。

60 

建立多個物件

也可以建立一個類的多個物件。以下示例將顯示如何實現這一點。

在這裡,我們建立了三個物件(st、st1和st2),並相應地呼叫它們的例項成員和例項方法。

讓我們來看一個如何建立多個物件的示例。

示例

/* Main program */ 
st = .student~new 
st~StudentID = 1 
st~StudentName = 'Joe' 
st~Marks1 = 10 
st~Marks2 = 20 
st~Marks3 = 30 
total = st~Total(st~Marks1,st~Marks2,st~Marks3) 
say total  

st1  =  .student~new 
st1~StudentID = 2 
st1~StudentName = 'John' 
st1~Marks1 = 10 
st1~Marks2 = 20 
st1~Marks3 = 40 
total = st1~Total(st1~Marks1,st1~Marks2,st1~Marks3) 
say total  

st2  =  .student~new 
st2~StudentID = 3 
st2~StudentName = 'Mark' 
st2~Marks1 = 10 
st2~Marks2 = 20 
st2~Marks3 = 30 
total = st2~Total(st2~Marks1,st2~Marks2,st2~Marks3) 
say total  

exit 0 
::class student 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method 'Total' 
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

上述程式的輸出將如下所示。

60 
70 
60 

繼承

繼承可以定義為一個類獲取另一個類的屬性(方法和欄位)的過程。透過使用繼承,資訊以分層的方式進行管理。

繼承其他屬性的類稱為子類(派生類、子類),其屬性被繼承的類稱為超類(基類、父類)。

讓我們看看Rexx中繼承的示例。在下面的示例中,我們建立一個名為Person的類。從那裡我們使用subclass關鍵字建立一個Student類作為Person的子類

示例

/* Main program */ 
st = .student~new 
st~StudentID = 1 
st~StudentName = 'Joe' 
st~Marks1 = 10 
st~Marks2 = 20 
st~Marks3 = 30 
say st~Total(st~Marks1,st~Marks2,st~Marks3)  

exit 0 
::class Person 
::class student subclass Person 
::attribute StudentID 
::attribute StudentName 
::attribute Marks1 
::attribute Marks2 
::attribute Marks3 
::method 'Total' 
use arg a,b,c 
return (ABS(a) + ABS(b) + ABS(c)) 

上述程式的輸出將如下所示。

60
廣告
© . All rights reserved.