OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / ja-jp / source / oop.txt
1 .. index:: 
2         single: オブジェクト指向プログラミング (OOP); はじめに
3
4 ====================================
5 オブジェクト指向プログラミング (OOP)
6 ====================================
7
8 Ring でオブジェクト指向プログラミングのパラダイムを扱う方法を学びます。
9
10 * クラスとオブジェクト
11 * 括弧を用いたオブジェクトへのアクセス
12 * コンポジション
13 * Setter と Getter
14 * プライベート属性とメソッド
15 * 演算子のオーバーロード
16 * 継承
17 * 動的属性
18 * パッケージ
19 * オブジェクトの表示
20 * Find() とオブジェクトのリスト
21 * Sort() とオブジェクトのリスト
22 * Self.属性 および Self.メソッド() の用法
23 * This.属性 および This.メソッド() の用法
24
25 .. index:: 
26         pair: オブジェクト指向プログラミング (OOP); クラスとオブジェクト
27
28
29 クラスとオブジェクト
30 ====================
31
32 このシンタックスで新しいクラスを定義できます。
33
34 文法:
35
36 .. code-block:: ring
37
38         Class <クラス名> [From|<|: <親クラス名>]
39                 [属性]
40                 [メソッド]
41                 [Private
42                   [属性]
43                   [メソッド]
44                 ]
45
46 また、このシンタックスでオブジェクトを作成できます。
47
48 文法:
49
50 .. code-block:: ring
51
52         New <オブジェクト名> [ (init メソッドの仮引数) ] |
53         [ { オブジェクトのデータ、およびメソッドへのアクセス } ]   ---> オブジェクト
54
55 用例:
56
57 .. code-block:: ring
58
59         New point { x=10  y=20  z=30  print() }
60         Class Point x y z func print see x + nl + y + nl + z + nl 
61
62 .. note:: { } でオブジェクトのデータとメソッドへアクセスできます。
63
64 .. tip:: クラス名の直後にクラスの属性を宣言できます。
65
66 実行結果:
67
68 .. code-block:: ring
69
70         10
71         20
72         30
73
74 別の記法で同じプログラムを書き直せます。
75
76 .. code-block:: ring
77
78         New point                       # point クラスで新しいオブジェクトを作成
79         {                               # 新しいオブジェクトの属性、およびメソッドへのアクセス
80                 x = 10                  # 属性 x へ 10 を設定
81                 y = 20                  # 属性 y へ 20 を設定
82                 z = 30                  # 属性 z へ 30 を設定
83                 print()                 # print メソッドの呼び出し
84         }                               # オブジェクトのアクセスを終了
85         
86         
87         Class Point                    # Point クラスの定義
88                 x y z                   # クラスには属性 x, y および z があります。
89                 func print             # print メソッドの定義
90                         see x + nl +   # 属性 x の表示
91                         y + nl +       # 属性 y の表示
92                         z + nl         # 属性 z の表示
93
94
95 また、別の方法で同じプログラムを書くことができます。
96
97 .. code-block:: ring
98
99         P1 = New Point
100         P1.x = 10
101         P1.y = 20
102         P1.z = 30
103         P1.Print()
104         Class Point x y z func print see x + nl + y + nl + z + nl       
105
106 .. note:: オブジェクト名の直後にドット演算子を用いるとオブジェクトのメンバへアクセスできます。
107
108 また、別の方法で同じプログラムを書くことができます。
109
110 .. code-block:: ring
111
112         new point { print() }   
113         Class Point
114                 x = 10  y = 20  z = 30
115                 func print see x + nl + y + nl + z + nl
116
117 .. note:: クラスの属性を宣言する時にクラスの属性へデフォルトの値を設定できます。
118
119 また、別の方法で同じプログラムを書くことができます。
120
121 .. code-block:: ring
122
123         new point(10,20,30)
124         Class Point
125                 x y z 
126                 func init p1,p2,p3 x=p1 y=p2 z=p3 print()
127                 func print see x + nl + y + nl + z + nl
128
129 .. note:: 新しいオブジェクトを作成するときに () を用いる init メソッドを直接呼び出せます。
130
131 また、別の方法で同じプログラムを書くことができます。
132
133 .. code-block:: ring
134
135         new point( [ :x = 10 , :y = 20 , :z = 30 ] )
136         Class Point x y z
137               func init aPara x = aPara[:x] y = aPara[:y] z = aPara[:z] print()
138               func print see x + nl + y + nl + z + nl
139
140 .. tip:: メソッドの仮引数を渡すためにハッシュを使用する場合は、
141          オプションの仮引数の作成時、およびハッシュへ追加時は仮引数の順序を変更できます。
142
143 .. index:: 
144         pair: オブジェクト指向プログラミング (OOP); 括弧を用いたオブジェクトへのアクセス
145
146 括弧を用いたオブジェクトへのアクセス
147 ====================================
148
149 括弧 { } で、いつでもオブジェクトへアクセスできます。
150
151 括弧内ではオブジェクトの属性とメソッドを直接使えます。
152
153 これは New キーワードによるオブジェクトの作成、またはこのシンタックスを必要なときに使えます。
154
155 .. code-block:: ring
156
157         オブジェクト名 { オブジェクトのデータ、およびメソッドへのアクセス }
158
159 用例:
160
161 .. code-block:: ring
162
163         See "Creating the Object" + nl
164         o1 = new Point
165         See "Using the Object" + nl
166         o1 {
167                 x=5     
168                 y=15
169                 z=25    
170                 print()
171         }
172         Class Point x y z func print see x + nl + y + nl + z 
173
174 括弧は関数、またはメソッドの呼び出し時にオブジェクトへアクセスするために使えます。
175
176 用例:
177
178 .. code-block:: ring
179
180         o1 = new Point
181
182         print( o1 { x=10 y=20 z=30 } )
183         
184         func print object
185                 see object.x + nl + 
186                     object.y + nl + 
187                     object.z 
188
189         Class Point x y z
190
191 括弧とドット演算子を併用して同じ式のオブジェクトへアクセスできます。
192
193
194 用例:
195
196 .. code-block:: ring
197
198         o1 = new Point
199
200         O1 { x=10 y=20 z=30 }.print()
201         
202         Class Point x y z
203                 func print see x + nl + y + nl + z
204
205 .. index:: 
206         pair: オブジェクト指向プログラミング (OOP); コンポジション
207
208 コンポジション
209 ==============
210
211 オブジェクトでは、ほかのオブジェクトの属性を所有できます。
212
213 アクセスしたいオブジェクトを括弧で入れ子にすることで実現できます。
214
215 用例:
216
217 .. code-block:: ring
218
219         R1 = New Rectangle 
220         {
221
222                 Name = "Rectangle 1"
223
224                 P1 
225                 {
226                         X = 10
227                         Y = 20
228                 }
229
230                 P2 
231                 {
232                         X = 200
233                         Y = 300
234                 }       
235
236                 Color = "Blue"
237
238         }
239         
240         see "Name : " + R1.Name + nl +
241             "Color: " + R1.Color + nl +
242             "P1   : (" + R1.P1.X + "," + R1.P1.Y + ")" + nl + 
243             "P2   : (" + R1.P2.X + "," + R1.P2.Y + ")"  
244
245         Class Rectangle
246                 name  color
247                 p1 = new Point
248                 p2 = new Point
249
250         Class Point x y 
251
252 実行結果:
253
254 .. code-block:: ring
255
256         Name : Rectangle 1
257         Color: Blue
258         P1   : (10,20)
259         P2   : (200,300)
260
261 .. index:: 
262         pair: オブジェクト指向プログラミング (OOP); Setter と Getter
263
264 Setter と Getter
265 =================
266
267 オブジェクトの属性を設定 (Setter)、または取得 (Getter) 時に用いるメソッドを定義できます。
268
269 文法:
270
271 .. code-block:: ring
272
273         Class [クラス名]
274
275                 [属性名]
276                 ...
277
278                 Func Set[属性名]
279                         ...
280
281                 Func Get[属性名]
282                         ...
283
284 用例:
285
286 .. code-block:: ring
287
288         o1 = new person
289
290         o1.name = "Mahmoud"  see o1.name + nl
291
292         o1 { name = "Ahmed"  see name }
293
294         Class Person
295
296                 name family = "Fayed"
297
298                 func setname value
299                         see "Message from SetName() Function!" + nl
300                         name = value + " " + family
301
302                 func getname
303                         see "Message from GetName() Function!" + nl
304                         return "Mr. " + name
305
306 実行結果:
307
308 .. code-block:: ring
309
310         Message from SetName() Function!
311         Message from GetName() Function!
312         Mr. Mahmoud Fayed
313         Message from SetName() Function!
314         Message from GetName() Function!
315         Mr. Ahmed Fayed
316
317 .. index:: 
318         pair: オブジェクト指向プログラミング (OOP); プライベート属性とメソッド
319
320 プライベート属性とメソッド
321 ==========================
322
323 クラスの本体内では private キーワードの後にプライベートな属性とメソッドを定義できます。
324
325 用例:
326
327 .. code-block:: ring
328
329         o1 = new person {
330                 name = "Test"
331                 age = 20
332                 print()
333                 o1.printsalary()
334         }
335
336         try
337                 see o1.salary
338         catch
339                 see cCatchError + nl
340         done
341
342         try
343                 o1.increasesalary(1000)
344         catch
345                 see cCatchError + nl
346         done
347
348         Class Person
349
350                 name age 
351
352                 func print
353                         see "Name   : " + name + nl + 
354                             "Age    : " + age + nl 
355
356                 func printsalary
357                         see "Salary : " + salary + nl 
358
359                 private
360
361                 salary = 15000
362
363                 func increasesalary x
364                         salary += x
365
366 実行結果:
367
368 .. code-block:: ring
369
370         Name   : Test
371         Age    : 20
372         Salary : 15000
373         Error (R27) : Using private attribute from outside the class : salary
374         Error (R26) : Calling private method from outside the class : increasesalary
375
376 .. index:: 
377         pair: オブジェクト指向プログラミング (OOP); 演算子のオーバーロード
378
379 演算子のオーバーロード
380 ======================
381
382 クラスオブジェクトで演算子を使用可能にするには、クラスへ **operator** メソッドを追加します。
383
384 文法:
385
386 .. code-block:: ring
387
388         Class ClassName
389
390                 ...
391
392                 Func operator cOperator,Para
393
394                         ...
395
396 関数の演算子は二種類の仮引数を扱います。一つ目は演算子を、二つ目は演算子の後にある第二仮引数を意味します。
397
398 用例:
399
400 .. code-block:: ring
401
402         o1 = new point { x = 10 y = 10 print("P1    : ") }
403         o2 = new point { x = 20 y = 40 print("P2    : ") }
404
405         o3 = o1 + o2
406         o3.print("P1+P2 : ")
407
408         class point x y
409
410                 func operator cOperator,Para
411                         result = new point      
412                         switch cOperator
413                         on "+"
414                                 result.x = x + Para.x
415                                 result.y = y + Para.y
416                         on "-"
417                                 result.x = x - Para.x
418                                 result.y = y - Para.y
419                         off
420                         return result
421
422                 func print cPoint
423                         see cPoint + "X : " + x + " Y : " + y + nl
424
425 実行結果:
426
427 .. code-block:: ring
428
429         P1    : X : 10 Y : 10
430         P2    : X : 20 Y : 40
431         P1+P2 : X : 30 Y : 50
432
433
434 この用例は stdlib.ring にある List クラスからの引用です。
435
436 .. code-block:: ring
437
438         Func operator cOperator,Para
439                 result = new list
440                 switch cOperator
441                         on "+"
442                                 if isobject(para)
443                                         for t in Para.vValue
444                                                 vValue + t
445                                         next
446                                 but islist(para)
447                                         for t in Para
448                                                 vValue + t
449                                         next
450                                 ok
451                         on "len"
452                                 return len( vValue )
453                         on "[]"
454                                 return &vValue[para]
455                 off
456                 return result
457
458 “len” 演算子は制御構造 (for in) で使用されます。
459
460 “[]” 演算子はリスト項目のアクセスをするときに使用されます。
461 この場合、 & 演算子は数値による参照で文字列などの項目の値を返すときに使用します。
462 よって、項目へアクセスするときに項目の更新ができます。
463
464
465 用例2
466
467 .. code-block:: ring
468
469         func main 
470
471         See "----1"+nl
472             a1 = new BigNumber( "123" )
473             a2 = new BigNumber( "456" )
474             a3 = new BigNumber( "789" )
475         See nl+"----2"+nl  
476                 a1.print()
477                 a2.print()
478                 a3.print()
479         See nl+"----3"+nl      
480             a2 = a1 + "45"  
481         See nl+"----4"+nl  
482             a2.print()      
483         See nl+"----5"+nl  
484             a3 = a1 + a2    
485         See nl+"----6"+nl  
486             a3.print()      
487         See nl+"----7"+nl
488
489         ###==================================
490         Func FuncAdd( num1, num2)
491            Sum = 0 + num1 + num2    ### Para.aData isNumber
492            Sum = "" +Sum            ### Para.adata isString
493         return Sum                  ### クラスからの返値
494         ###===================================
495
496         class BigNumber 
497
498             ### 変数
499             aData = "468"
500
501             ### INIT 関数のデフォルト値
502             func init aPara 
503                 ? "INIT aPara: " ? aPara
504                 if isString(aPara)
505                     aData = aPara 
506                 else 
507                     aData = "" + aPara
508                 ok
509
510             ### そのほかの関数
511             func operator cOperator, Para
512                     whatType = Type(Para)
513                     ? nl+"WhatType-PARA: "+ whatType ? Para 
514                     ? nl+"Operator: " ? cOperator  ? nl+"PARA: " ? Para ? "    ______" ? nl
515                                     if whatType = "STRING"
516                                        dataInfo = Para
517                                        ? "dataInfo String: " ? dataInfo
518                                     but whatType = "NUMBER"
519                                        datinfo  = "" + Para
520                                        ? "dataInfo Number: " ? dataInfo
521                                     else whatType = "OBJECT"
522                                        dataInfo = "" + para.aData  
523                                        ? "dataInfo OBJECT: " ? dataInfo
524                                     ok
525                                   ? "dataInfo USING: " ? dataInfo  
526                     ### Para.aData は初めて渡されたときには存在しません (メンバのオブジェクト)。
527                     ### "self" が代入されたときの結果は isObject です。
528                     result = self   
529                     switch cOperator
530                         on "+"
531                              answer = FuncAdd( aData, dataInfo )
532                              ? nl+"AnswerString - FunAdd aData, dataInfo: " ? answer  
533                              ### result = Self はオブジェクトであるため、 aData メンバへオブジェクトを代入します
534                              result.aData = answer
535                         off
536                     ### Result = Self はオブジェクトです
537                     return result   
538
539             func print 
540                 ? nl+"ClassPrint aData: " ? aData       
541
542 .. index:: 
543         pair: オブジェクト指向プログラミング (OOP); 継承
544
545 継承
546 ====
547
548 From キーワードを使用したクラスの定義で別のクラスからクラスを作成できます。
549
550 文法:
551
552 .. code-block:: ring
553
554         Class <クラス名> [From <親クラスの名前>]
555
556 super オブジェクトで子クラスから親クラスのメソッドを呼び出せます。
557
558 文法:
559
560 .. code-block:: ring
561
562         func methodname
563                 ...
564                 super.methodname()
565                 ...
566
567 用例:
568
569 .. code-block:: ring
570
571         Func main
572                 e1 = new Employee {
573                         Name = "test"
574                         age = 20
575                         job = "programmer"
576                         salary = 20000000
577                         print()
578                 }
579         
580
581         Class Human 
582                 Name Age
583                 func print
584                         see "Name : " + name + nl + "Age  : " + age + nl
585
586         Class Employee from Human
587                 Job Salary
588                 func print
589                         super.print()
590                         see "Job  : " + job + nl + "Salary : " + salary + nl
591
592 実行結果:
593
594 .. code-block:: ring
595
596         Name : test
597         Age  : 20
598         Job  : programmer
599         Salary : 20000000
600
601 .. index:: 
602         pair: オブジェクト指向プログラミング (OOP); 動的属性
603
604 動的属性
605 ========
606
607 クラス名末尾に命令を記述すると、新しいオブジェクトが作成されたときに実行します。
608
609 用例:
610
611 .. code-block:: ring
612
613         o1 = new dynamicClass
614         see o1.var5 + nl        # 5 を出力
615
616         Class DynamicClass
617                 for x = 1 to 10
618                         cStr = "var" + x + " = " + x
619                         eval(cStr)
620                 next
621
622 .. tip:: 前述の用例では var1, var2, ..., var10 は属性として定義されています。
623
624 .. tip:: 前述の用例における問題は x および cStr が同じ属性として定義されていることです!
625
626 .. note:: 文字列内にクラスの定義を記述できます。
627           また、 eval() 関数で文字列を実行するとクラスを定義できます。
628
629 .. index:: 
630         pair: オブジェクト指向プログラミング (OOP); パッケージ
631
632 パッケージ
633 ==========
634
635 このシンタックスでパッケージ (共通の名前によるクラスのグループ) を作成できます。
636
637 .. code-block:: ring
638
639         package PackageName
640                 Class Class1
641                         ...
642                 Class Class2
643                         ...
644                 Class Class3
645                         ...
646                 ...
647
648 用例:
649
650 .. code-block:: ring
651
652         o1 = new System.output.console
653         o1.print("Hello World")
654         
655         Package System.Output
656                 Class Console
657                         Func Print cText
658                                 see cText + nl
659
660 .. note:: パッケージ名にドット演算子を使えます。
661
662 パッケージ.クラス名 (PackageName.ClassName) という長い名前を入力する代わりに import 命令を使えます。
663
664 パッケージをインポートする場合は、このパッケージを指定のクラスで直接使えます。 
665
666 用例:
667
668 .. code-block:: ring
669
670         import system.output
671         o1 = new console {
672                 print("Hello World")
673         }
674         Package System.Output
675                 Class Console
676                         Func Print cText
677                                 see cText + nl
678
679 .. index:: 
680         pair: オブジェクト指向プログラミング (OOP); オブジェクトの表示
681
682 オブジェクトの表示
683 ==================
684
685 see 命令はオブジェクトの状態 (属性と値) を表示します。
686
687 用例:
688
689 .. code-block:: ring
690
691         see new point { x=10 y=20 z=30 }
692         class point x y z
693
694 実行結果:
695
696 .. code-block:: ring
697
698         x: 10.000000
699         y: 20.000000
700         z: 30.000000
701
702 .. index:: 
703         pair: オブジェクト指向プログラミング (OOP); Find() とオブジェクトのリスト
704
705 Find() とオブジェクトのリスト
706 =============================
707
708 Find() 関数はオブジェクトのリスト内部を検索します。
709
710 文法:
711
712 .. code-block:: ring
713
714         Find(List,ItemValue,nColumn,cAttribute) ---> 項目のインデックス
715
716 用例:
717
718 .. code-block:: ring
719
720         myList1 = [new Company {position=3 name="Mahmoud" symbol="MHD"},
721                    new Company {position=2 name="Bert" symbol="BRT"},
722                    new Company {position=1 name="Ring" symbol="RNG"}
723                   ]
724
725         see find(mylist1,"Bert",1,"name") + nl
726         see find(mylist1,"Ring",1,"name") + nl
727         see find(mylist1,"Mahmoud",1,"name") + nl
728         see find(mylist1,"RNG",1,"symbol") + nl
729         see find(mylist1,"MHD",1,"symbol") + nl
730         see find(mylist1,"BRT",1,"symbol") + nl
731         see find(mylist1,3,1,"position") + nl
732         see find(mylist1,1,1,"position") + nl
733         see "Other" + nl
734         see find(mylist1,"test",1,"name") + nl
735         see find(mylist1,"test",0,"name") + nl
736         see find(mylist1,"test",5,"name") + nl
737
738         class company position name symbol
739
740 実行結果:
741
742 .. code-block:: ring
743
744         2
745         3
746         1
747         3
748         1
749         2
750         1
751         3
752         Other
753         0
754         0
755         0
756
757 .. index:: 
758         pair: オブジェクト指向プログラミング (OOP); Sort() とオブジェクトのリスト
759
760 Sort() とオブジェクトのリスト
761 =============================
762
763 Sort() 関数はオブジェクトの属性に基づきオブジェクトのリストを整列します。
764
765 文法:
766
767 .. code-block:: ring
768
769         Sort(List,nColumn,cAttribute) ---> オブジェクトの属性に基づいて整列されたリスト
770
771 用例:
772
773 .. code-block:: ring
774
775         myList1 = [
776                         new Company {position=3 name="Mahmoud" symbol="MHD"},
777                         new Company {position=2 name="Bert" symbol="BRT"},
778                         new Company {position=8 name="Charlie" symbol="CHR"},
779                         new Company {position=6 name="Easy" symbol="FEAS"},
780                         new Company {position=7 name="Fox" symbol="EFOX"},
781                         new Company {position=5 name="Dog" symbol="GDOG"},
782                         new Company {position=4 name="George" symbol="DGRG"},
783                         new Company {position=1 name="Ring" symbol="RNG"}
784                   ]
785
786         see sort(mylist1,1,"name")
787         see copy("*",70) + nl
788         see sort(mylist1,1,"symbol")
789         see copy("*",70) + nl
790         see sort(mylist1,1,"position")
791
792         class company position name symbol
793
794 実行結果:
795
796 .. code-block:: ring
797
798
799         position: 2.000000
800         name: Bert
801         symbol: BRT
802         position: 8.000000
803         name: Charlie
804         symbol: CHR
805         position: 5.000000
806         name: Dog
807         symbol: GDOG
808         position: 6.000000
809         name: Easy
810         symbol: FEAS
811         position: 7.000000
812         name: Fox
813         symbol: EFOX
814         position: 4.000000
815         name: George
816         symbol: DGRG
817         position: 3.000000
818         name: Mahmoud
819         symbol: MHD
820         position: 1.000000
821         name: Ring
822         symbol: RNG
823         **********************************************************************
824         position: 2.000000
825         name: Bert
826         symbol: BRT
827         position: 8.000000
828         name: Charlie
829         symbol: CHR
830         position: 4.000000
831         name: George
832         symbol: DGRG
833         position: 7.000000
834         name: Fox
835         symbol: EFOX
836         position: 6.000000
837         name: Easy
838         symbol: FEAS
839         position: 5.000000
840         name: Dog
841         symbol: GDOG
842         position: 3.000000
843         name: Mahmoud
844         symbol: MHD
845         position: 1.000000
846         name: Ring
847         symbol: RNG
848         **********************************************************************
849         position: 1.000000
850         name: Ring
851         symbol: RNG
852         position: 2.000000
853         name: Bert
854         symbol: BRT
855         position: 3.000000
856         name: Mahmoud
857         symbol: MHD
858         position: 4.000000
859         name: George
860         symbol: DGRG
861         position: 5.000000
862         name: Dog
863         symbol: GDOG
864         position: 6.000000
865         name: Easy
866         symbol: FEAS
867         position: 7.000000
868         name: Fox
869         symbol: EFOX
870         position: 8.000000
871         name: Charlie
872         symbol: CHR
873
874 .. index:: 
875         pair: オブジェクト指向プログラミング (OOP); Self.属性 と Self.メソッド() の用法
876
877 Self.属性 と Self.メソッド() の用法
878 ==============================================
879
880 クラスの範囲内 (クラス名末尾、およびメソッドの前) ならびに、クラスのメソッドは Self.属性 と Self.メソッド() を使えます。
881
882 .. code-block:: ring
883
884         Class Point
885                 self.x = 10
886                 self.y = 20
887                 self.z = 30
888                 func print
889                         see self.x + nl + self.y + nl + self.z + nl
890
891 .. note:: クラスの属性を定義するために、クラスの範囲内で Self.属性 を使うとクラスの属性はグローバル変数との名前衝突から保護されます。
892
893 .. tip:: Self.属性 でクラスの属性を定義するときに、同名のグローバル変数が存在する場合は属性の定義は行われずにグローバル変数が使用されます。
894
895 グローバル変数名と属性名の間で起こる名前衝突については「スコープの規則」をご確認ください。
896
897 これは何が起きているのですか?
898
899 理由
900
901 * クラスの範囲内ではグローバル変数へアクセスできます。
902 * 変数定義前に Ring は変数の検索を行い、見つかれば使用します。
903
904 .. note:: グローバル変数を避けるために Main 関数の使用、および名前を $ から始めてください。
905
906 .. tip:: 大規模プログラムでは Self.属性 でクラスの保護、およびメンバの定義してください。
907
908
909 .. index:: 
910         pair: オブジェクト指向プログラミング (OOP); This.属性 と This.メソッド() の用法
911
912 This.属性 と This.メソッド() の用法
913 =======================================
914
915 クラス内にあるメソッドはオブジェクトのスコープへ直接的なアクセスできます。
916 属性の読み書き、またはメソッドの呼び出しでは Self.属性 あるいは Self.メソッド() の使用は不要です。
917
918 また、括弧 { } でメソッドの内側から別のオブジェクトへアクセスできます。
919 この場合は、現在のオブジェクトの有効範囲は括弧内へ変更されます。
920
921 括弧内のクラスの属性、およびメソッドへのアクセスする方法はありますか?
922
923 これは This.属性 と This.メソッド() で実現できます。
924
925 用例:
926
927 .. code-block:: ring
928
929         new point  
930
931         class point 
932                 x=10 y=20 z=30
933                 print()
934                 func print
935                         new UI {
936                                 display(this.x,this.y,this.z)
937                         }
938
939         Class UI
940                 func display x,y,z
941                         see x + nl + y + nl + z + nl
942
943
944 .. index:: 
945         pair: オブジェクト指向プログラミング (OOP); クラス範囲で This を Self として使用
946
947 クラス範囲で This を Self として使用
948 ====================================
949
950 クラス範囲とはクラス名の後、およびすべてのメソッドの前に出現する範囲のことです。
951
952 クラス範囲で This を Self として使えます。
953
954 用例:
955
956 .. code-block:: ring
957
958         func main
959
960                 o1 = new program {
961                         test()
962                 }
963
964                 ? o1
965
966         class program 
967
968                 this.name = "My Application"
969                 this.version = "1.0"
970                 ? name ? version        
971
972                 func test
973                         ? "Name    = " + name 
974                         ? "Version = " + version
975
976 実行結果:
977
978 .. code-block:: none
979
980         My Application
981         1.0
982         Name    = My Application
983         Version = 1.0
984         name: My Application
985         version: 1.0
986
987 .. note:: 弓括弧は現在の有効なオブジェクトを変更しますが、This はクラスを指したままにできます。
988
989 .. tip:: This と Self には違いがあります。 Self が指している現在の有効なオブジェクトは弓括弧で変更できます。
990
991 ほとんどの場合、クラス範囲で This あるいは Self を使う必要はないことを覚えていてください。
992
993 このように記述することもできます。
994
995
996 .. code-block:: ring
997
998         class program name version
999
1000 または、
1001
1002 .. code-block:: ring
1003
1004         class program name="My Application" version="1.0"
1005
1006 .. note:: 同名で定義されたグローバル変数との衝突を回避するために、クラス範囲で This あるいは Self を使用します。
1007
1008
1009 .. index:: 
1010         pair: オブジェクト指向プログラミング (OOP); オブジェクト属性のデフォルト値
1011
1012 オブジェクト属性のデフォルト値
1013 ==============================
1014
1015 オブジェクト属性のデフォルト値は NULL です。
1016
1017 Ring では、 NULL 値は空文字列または "NULL" を有する文字列です。
1018
1019 isNULL() 関数で NULL 値を確認できます。
1020
1021 用例:
1022
1023 .. code-block:: ring
1024
1025         oProgram = new Program
1026         ? oProgram.name
1027         ? oProgram.version
1028         ? isNULL(oProgram.name)
1029         ? isNULL(oProgram.version)
1030         oProgram { name="My Application" version="1.0" }
1031         ? isNULL(oProgram.name)
1032         ? isNULL(oProgram.version)
1033         ? oProgram
1034
1035         class program
1036                 name 
1037                 version
1038
1039 実行結果:
1040
1041 .. code-block:: none
1042
1043         NULL
1044         NULL
1045         1
1046         1
1047         0
1048         0
1049         name: My Application
1050         version: 1.0