aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/id-id/doc/objects/object-oriented-programming.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/id-id/doc/objects/object-oriented-programming.md')
-rw-r--r--doc/docs/id-id/doc/objects/object-oriented-programming.md158
1 files changed, 79 insertions, 79 deletions
diff --git a/doc/docs/id-id/doc/objects/object-oriented-programming.md b/doc/docs/id-id/doc/objects/object-oriented-programming.md
index 6a8559e..96c19d7 100644
--- a/doc/docs/id-id/doc/objects/object-oriented-programming.md
+++ b/doc/docs/id-id/doc/objects/object-oriented-programming.md
@@ -1,8 +1,8 @@
1# Object Oriented Programming 1# Pemrograman Berorientasi Objek
2 2
3In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. 3Dalam contoh-contoh ini, kode Lua yang dihasilkan mungkin tampak berat. Sebaiknya fokus dulu pada makna kode YueScript, lalu lihat kode Lua jika Anda ingin mengetahui detail implementasinya.
4 4
5A simple class: 5Kelas sederhana:
6 6
7```yuescript 7```yuescript
8class Inventory 8class Inventory
@@ -31,15 +31,15 @@ class Inventory
31 31
32</YueDisplay> 32</YueDisplay>
33 33
34A class is declared with a class statement followed by a table-like declaration where all of the methods and properties are listed. 34Kelas dideklarasikan dengan pernyataan `class` diikuti deklarasi mirip tabel di mana semua method dan properti dicantumkan.
35 35
36The new property is special in that it will become the constructor. 36Properti `new` bersifat khusus karena akan menjadi konstruktor.
37 37
38Notice how all the methods in the class use the fat arrow function syntax. When calling methods on a instance, the instance itself is sent in as the first argument. The fat arrow handles the creation of a self argument. 38Perhatikan bahwa semua method di kelas menggunakan sintaks fungsi panah tebal. Saat memanggil method pada instance, instance itu sendiri dikirim sebagai argumen pertama. Panah tebal menangani pembuatan argumen `self`.
39 39
40The @ prefix on a variable name is shorthand for self.. @items becomes self.items. 40Prefiks `@` pada nama variabel adalah singkatan untuk `self.`. `@items` menjadi `self.items`.
41 41
42Creating an instance of the class is done by calling the name of the class as a function. 42Membuat instance kelas dilakukan dengan memanggil nama kelas sebagai fungsi.
43 43
44```yuescript 44```yuescript
45inv = Inventory! 45inv = Inventory!
@@ -56,11 +56,11 @@ inv\add_item "pants"
56 56
57</YueDisplay> 57</YueDisplay>
58 58
59Because the instance of the class needs to be sent to the methods when they are called, the \ operator is used. 59Karena instance kelas perlu dikirim ke method saat dipanggil, operator `\` digunakan.
60 60
61All properties of a class are shared among the instances. This is fine for functions, but for other types of objects, undesired results may occur. 61Semua properti kelas dibagikan di antara instance. Ini baik untuk fungsi, tetapi untuk jenis objek lain dapat menimbulkan hasil yang tidak diinginkan.
62 62
63Consider the example below, the clothes property is shared amongst all instances, so modifications to it in one instance will show up in another: 63Pertimbangkan contoh di bawah ini, properti `clothes` dibagikan di antara semua instance, sehingga perubahan di satu instance akan terlihat di instance lainnya:
64 64
65```yuescript 65```yuescript
66class Person 66class Person
@@ -74,7 +74,7 @@ b = Person!
74a\give_item "pants" 74a\give_item "pants"
75b\give_item "shirt" 75b\give_item "shirt"
76 76
77-- will print both pants and shirt 77-- akan mencetak pants dan shirt
78print item for item in *a.clothes 78print item for item in *a.clothes
79``` 79```
80<YueDisplay> 80<YueDisplay>
@@ -91,13 +91,13 @@ b = Person!
91a\give_item "pants" 91a\give_item "pants"
92b\give_item "shirt" 92b\give_item "shirt"
93 93
94-- will print both pants and shirt 94-- akan mencetak pants dan shirt
95print item for item in *a.clothes 95print item for item in *a.clothes
96``` 96```
97 97
98</YueDisplay> 98</YueDisplay>
99 99
100The proper way to avoid this problem is to create the mutable state of the object in the constructor: 100Cara yang benar untuk menghindari masalah ini adalah membuat state yang dapat berubah di konstruktor:
101 101
102```yuescript 102```yuescript
103class Person 103class Person
@@ -114,9 +114,9 @@ class Person
114 114
115</YueDisplay> 115</YueDisplay>
116 116
117## Inheritance 117## Pewarisan
118 118
119The extends keyword can be used in a class declaration to inherit the properties and methods from another class. 119Kata kunci `extends` dapat digunakan dalam deklarasi kelas untuk mewarisi properti dan method dari kelas lain.
120 120
121```yuescript 121```yuescript
122class BackPack extends Inventory 122class BackPack extends Inventory
@@ -137,18 +137,18 @@ class BackPack extends Inventory
137 137
138</YueDisplay> 138</YueDisplay>
139 139
140Here we extend our Inventory class, and limit the amount of items it can carry. 140Di sini kita memperluas kelas Inventory, dan membatasi jumlah item yang bisa dibawa.
141 141
142In this example, we don't define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor. 142Dalam contoh ini, kita tidak mendefinisikan konstruktor pada subclass, sehingga konstruktor kelas induk dipanggil ketika membuat instance baru. Jika kita mendefinisikan konstruktor, kita bisa menggunakan method `super` untuk memanggil konstruktor induk.
143 143
144Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class. 144Setiap kali sebuah kelas mewarisi dari kelas lain, ia mengirim pesan ke kelas induk dengan memanggil method `__inherited` pada kelas induk jika ada. Fungsi menerima dua argumen, kelas yang diwarisi dan kelas anak.
145 145
146```yuescript 146```yuescript
147class Shelf 147class Shelf
148 @__inherited: (child) => 148 @__inherited: (child) =>
149 print @__name, "was inherited by", child.__name 149 print @__name, "was inherited by", child.__name
150 150
151-- will print: Shelf was inherited by Cupboard 151-- akan mencetak: Shelf was inherited by Cupboard
152class Cupboard extends Shelf 152class Cupboard extends Shelf
153``` 153```
154<YueDisplay> 154<YueDisplay>
@@ -158,7 +158,7 @@ class Shelf
158 @__inherited: (child) => 158 @__inherited: (child) =>
159 print @__name, "was inherited by", child.__name 159 print @__name, "was inherited by", child.__name
160 160
161-- will print: Shelf was inherited by Cupboard 161-- akan mencetak: Shelf was inherited by Cupboard
162class Cupboard extends Shelf 162class Cupboard extends Shelf
163``` 163```
164 164
@@ -166,27 +166,27 @@ class Cupboard extends Shelf
166 166
167## Super 167## Super
168 168
169**super** is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class. 169**super** adalah kata kunci khusus yang dapat digunakan dengan dua cara: sebagai objek, atau dipanggil seperti fungsi. Ia hanya memiliki fungsi khusus ketika berada di dalam kelas.
170 170
171When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above) 171Ketika dipanggil sebagai fungsi, ia akan memanggil fungsi dengan nama yang sama di kelas induk. `self` saat ini akan otomatis dikirim sebagai argumen pertama. (Seperti pada contoh pewarisan di atas)
172 172
173When super is used as a normal value, it is a reference to the parent class object. 173Ketika `super` digunakan sebagai nilai normal, ia merupakan referensi ke objek kelas induk.
174 174
175It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class. 175Ia dapat diakses seperti objek biasa untuk mengambil nilai di kelas induk yang mungkin tertutup oleh kelas anak.
176 176
177When the \ calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned. 177Ketika operator pemanggilan `\` digunakan dengan `super`, `self` disisipkan sebagai argumen pertama alih-alih nilai `super` itu sendiri. Saat menggunakan `.` untuk mengambil fungsi, fungsi mentah dikembalikan.
178 178
179A few examples of using super in different ways: 179Beberapa contoh penggunaan `super` dengan cara berbeda:
180 180
181```yuescript 181```yuescript
182class MyClass extends ParentClass 182class MyClass extends ParentClass
183 a_method: => 183 a_method: =>
184 -- the following have the same effect: 184 -- berikut memiliki efek yang sama:
185 super "hello", "world" 185 super "hello", "world"
186 super\a_method "hello", "world" 186 super\a_method "hello", "world"
187 super.a_method self, "hello", "world" 187 super.a_method self, "hello", "world"
188 188
189 -- super as a value is equal to the parent class: 189 -- super sebagai nilai sama dengan kelas induk:
190 assert super == ParentClass 190 assert super == ParentClass
191``` 191```
192<YueDisplay> 192<YueDisplay>
@@ -194,28 +194,28 @@ class MyClass extends ParentClass
194```yue 194```yue
195class MyClass extends ParentClass 195class MyClass extends ParentClass
196 a_method: => 196 a_method: =>
197 -- the following have the same effect: 197 -- berikut memiliki efek yang sama:
198 super "hello", "world" 198 super "hello", "world"
199 super\a_method "hello", "world" 199 super\a_method "hello", "world"
200 super.a_method self, "hello", "world" 200 super.a_method self, "hello", "world"
201 201
202 -- super as a value is equal to the parent class: 202 -- super sebagai nilai sama dengan kelas induk:
203 assert super == ParentClass 203 assert super == ParentClass
204``` 204```
205 205
206</YueDisplay> 206</YueDisplay>
207 207
208**super** can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self. 208**super** juga dapat digunakan di sisi kiri Function Stub. Perbedaan utamanya adalah, alih-alih fungsi hasil terikat pada nilai `super`, fungsi terikat pada `self`.
209 209
210## Types 210## Tipe
211 211
212Every instance of a class carries its type with it. This is stored in the special __class property. This property holds the class object. The class object is what we call to build a new instance. We can also index the class object to retrieve class methods and properties. 212Setiap instance kelas membawa tipenya sendiri. Ini disimpan di properti khusus `__class`. Properti ini memuat objek kelas. Objek kelas adalah yang kita panggil untuk membuat instance baru. Kita juga dapat mengindeks objek kelas untuk mengambil method dan properti kelas.
213 213
214```yuescript 214```yuescript
215b = BackPack! 215b = BackPack!
216assert b.__class == BackPack 216assert b.__class == BackPack
217 217
218print BackPack.size -- prints 10 218print BackPack.size -- mencetak 10
219``` 219```
220<YueDisplay> 220<YueDisplay>
221 221
@@ -223,45 +223,45 @@ print BackPack.size -- prints 10
223b = BackPack! 223b = BackPack!
224assert b.__class == BackPack 224assert b.__class == BackPack
225 225
226print BackPack.size -- prints 10 226print BackPack.size -- mencetak 10
227``` 227```
228 228
229</YueDisplay> 229</YueDisplay>
230 230
231## Class Objects 231## Objek Kelas
232 232
233The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class. 233Objek kelas adalah yang kita buat saat menggunakan pernyataan `class`. Objek kelas disimpan dalam variabel dengan nama yang sama dengan kelas.
234 234
235The class object can be called like a function in order to create new instances. That's how we created instances of classes in the examples above. 235Objek kelas dapat dipanggil seperti fungsi untuk membuat instance baru. Begitulah cara kita membuat instance kelas pada contoh di atas.
236 236
237A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base. 237Sebuah kelas terdiri dari dua tabel: tabel kelas itu sendiri dan tabel base. Base digunakan sebagai metatable untuk semua instance. Semua properti yang dicantumkan dalam deklarasi kelas ditempatkan di base.
238 238
239The class object's metatable reads properties from the base if they don't exist in the class object. This means we can access functions and properties directly from the class. 239Metatable objek kelas membaca properti dari base jika tidak ada di objek kelas. Ini berarti kita dapat mengakses fungsi dan properti langsung dari kelas.
240 240
241It is important to note that assigning to the class object does not assign into the base, so it's not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below. 241Penting untuk dicatat bahwa assignment ke objek kelas tidak meng-assign ke base, sehingga itu bukan cara yang valid untuk menambahkan method baru ke instance. Sebagai gantinya, base harus diubah secara eksplisit. Lihat field `__base` di bawah.
242 242
243The class object has a couple special properties: 243Objek kelas memiliki beberapa properti khusus:
244 244
245The name of the class as when it was declared is stored as a string in the __name field of the class object. 245Nama kelas saat dideklarasikan disimpan sebagai string di field `__name` pada objek kelas.
246 246
247```yuescript 247```yuescript
248print BackPack.__name -- prints Backpack 248print BackPack.__name -- mencetak Backpack
249``` 249```
250<YueDisplay> 250<YueDisplay>
251 251
252```yue 252```yue
253print BackPack.__name -- prints Backpack 253print BackPack.__name -- mencetak Backpack
254``` 254```
255 255
256</YueDisplay> 256</YueDisplay>
257 257
258The base object is stored in __base. We can modify this table to add functionality to instances that have already been created and ones that are yet to be created. 258Objek base disimpan di `__base`. Kita dapat memodifikasi tabel ini untuk menambahkan fungsionalitas ke instance yang sudah dibuat maupun yang akan dibuat.
259 259
260If the class extends from anything, the parent class object is stored in __parent. 260Jika kelas memperluas kelas lain, objek kelas induk disimpan di `__parent`.
261 261
262## Class Variables 262## Variabel Kelas
263 263
264We can create variables directly in the class object instead of in the base by using @ in the front of the property name in a class declaration. 264Kita dapat membuat variabel langsung di objek kelas alih-alih di base dengan menggunakan `@` di depan nama properti pada deklarasi kelas.
265 265
266```yuescript 266```yuescript
267class Things 267class Things
@@ -269,7 +269,7 @@ class Things
269 269
270Things\some_func! 270Things\some_func!
271 271
272-- class variables not visible in instances 272-- variabel kelas tidak terlihat pada instance
273assert Things().some_func == nil 273assert Things().some_func == nil
274``` 274```
275<YueDisplay> 275<YueDisplay>
@@ -280,13 +280,13 @@ class Things
280 280
281Things\some_func! 281Things\some_func!
282 282
283-- class variables not visible in instances 283-- variabel kelas tidak terlihat pada instance
284assert Things().some_func == nil 284assert Things().some_func == nil
285``` 285```
286 286
287</YueDisplay> 287</YueDisplay>
288 288
289In expressions, we can use @@ to access a value that is stored in the __class of self. Thus, @@hello is shorthand for self.__class.hello. 289Dalam ekspresi, kita dapat menggunakan `@@` untuk mengakses nilai yang disimpan di `__class` milik `self`. Jadi, `@@hello` adalah singkatan dari `self.__class.hello`.
290 290
291```yuescript 291```yuescript
292class Counter 292class Counter
@@ -298,7 +298,7 @@ class Counter
298Counter! 298Counter!
299Counter! 299Counter!
300 300
301print Counter.count -- prints 2 301print Counter.count -- mencetak 2
302``` 302```
303<YueDisplay> 303<YueDisplay>
304 304
@@ -312,12 +312,12 @@ class Counter
312Counter! 312Counter!
313Counter! 313Counter!
314 314
315print Counter.count -- prints 2 315print Counter.count -- mencetak 2
316``` 316```
317 317
318</YueDisplay> 318</YueDisplay>
319 319
320The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua's colon syntax. 320Semantik pemanggilan `@@` mirip dengan `@`. Memanggil nama `@@` akan meneruskan kelas sebagai argumen pertama menggunakan sintaks kolon Lua.
321 321
322```yuescript 322```yuescript
323@@hello 1,2,3,4 323@@hello 1,2,3,4
@@ -330,11 +330,11 @@ The calling semantics of @@ are similar to @. Calling a @@ name will pass the cl
330 330
331</YueDisplay> 331</YueDisplay>
332 332
333## Class Declaration Statements 333## Pernyataan Deklarasi Kelas
334 334
335In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object. 335Di dalam badan deklarasi kelas, kita bisa memiliki ekspresi normal selain pasangan key/value. Dalam konteks ini, `self` sama dengan objek kelas.
336 336
337Here is an alternative way to create a class variable compared to what's described above: 337Berikut cara alternatif untuk membuat variabel kelas dibandingkan yang dijelaskan di atas:
338 338
339```yuescript 339```yuescript
340class Things 340class Things
@@ -349,9 +349,9 @@ class Things
349 349
350</YueDisplay> 350</YueDisplay>
351 351
352These expressions are executed after all the properties have been added to the base. 352Ekspresi ini dieksekusi setelah semua properti ditambahkan ke base.
353 353
354All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access: 354Semua variabel yang dideklarasikan di badan kelas bersifat lokal terhadap properti kelas. Ini berguna untuk menempatkan nilai privat atau fungsi pembantu yang hanya dapat diakses oleh method kelas:
355 355
356```yuescript 356```yuescript
357class MoreThings 357class MoreThings
@@ -374,11 +374,11 @@ class MoreThings
374 374
375</YueDisplay> 375</YueDisplay>
376 376
377## @ and @@ Values 377## Nilai @ dan @@
378 378
379When @ and @@ are prefixed in front of a name they represent, respectively, that name accessed in self and self.__class. 379Ketika `@` dan `@@` diprefiks di depan sebuah nama, masing-masing merepresentasikan nama tersebut yang diakses di `self` dan `self.__class`.
380 380
381If they are used all by themselves, they are aliases for self and self.__class. 381Jika digunakan sendirian, keduanya adalah alias untuk `self` dan `self.__class`.
382 382
383```yuescript 383```yuescript
384assert @ == self 384assert @ == self
@@ -393,7 +393,7 @@ assert @@ == self.__class
393 393
394</YueDisplay> 394</YueDisplay>
395 395
396For example, a quick way to create a new instance of the same class from an instance method using @@: 396Contohnya, cara cepat untuk membuat instance baru dari kelas yang sama dari method instance menggunakan `@@`:
397 397
398```yuescript 398```yuescript
399some_instance_method = (...) => @@ ... 399some_instance_method = (...) => @@ ...
@@ -406,15 +406,15 @@ some_instance_method = (...) => @@ ...
406 406
407</YueDisplay> 407</YueDisplay>
408 408
409## Constructor Property Promotion 409## Promosi Properti Konstruktor
410 410
411To reduce the boilerplate code for definition of simple value objects. You can write a simple class like: 411Untuk mengurangi boilerplate saat mendefinisikan objek nilai sederhana, Anda dapat menulis kelas sederhana seperti:
412 412
413```yuescript 413```yuescript
414class Something 414class Something
415 new: (@foo, @bar, @@biz, @@baz) => 415 new: (@foo, @bar, @@biz, @@baz) =>
416 416
417-- Which is short for 417-- Yang merupakan singkatan dari
418 418
419class Something 419class Something
420 new: (foo, bar, biz, baz) => 420 new: (foo, bar, biz, baz) =>
@@ -429,7 +429,7 @@ class Something
429class Something 429class Something
430 new: (@foo, @bar, @@biz, @@baz) => 430 new: (@foo, @bar, @@biz, @@baz) =>
431 431
432-- Which is short for 432-- Yang merupakan singkatan dari
433 433
434class Something 434class Something
435 new: (foo, bar, biz, baz) => 435 new: (foo, bar, biz, baz) =>
@@ -441,7 +441,7 @@ class Something
441 441
442</YueDisplay> 442</YueDisplay>
443 443
444You can also use this syntax for a common function to initialize a object's fields. 444Anda juga bisa menggunakan sintaks ini untuk fungsi umum guna menginisialisasi field objek.
445 445
446```yuescript 446```yuescript
447new = (@fieldA, @fieldB) => @ 447new = (@fieldA, @fieldB) => @
@@ -458,9 +458,9 @@ print obj
458 458
459</YueDisplay> 459</YueDisplay>
460 460
461## Class Expressions 461## Ekspresi Kelas
462 462
463The class syntax can also be used as an expression which can be assigned to a variable or explicitly returned. 463Sintaks kelas juga bisa digunakan sebagai ekspresi yang dapat di-assign ke variabel atau di-return secara eksplisit.
464 464
465```yuescript 465```yuescript
466x = class Bucket 466x = class Bucket
@@ -477,9 +477,9 @@ x = class Bucket
477 477
478</YueDisplay> 478</YueDisplay>
479 479
480## Anonymous classes 480## Kelas Anonim
481 481
482The name can be left out when declaring a class. The __name attribute will be nil, unless the class expression is in an assignment. The name on the left hand side of the assignment is used instead of nil. 482Nama bisa dihilangkan saat mendeklarasikan kelas. Atribut `__name` akan bernilai nil, kecuali ekspresi kelas berada dalam assignment. Nama di sisi kiri assignment digunakan sebagai ganti nil.
483 483
484```yuescript 484```yuescript
485BigBucket = class extends Bucket 485BigBucket = class extends Bucket
@@ -498,7 +498,7 @@ assert Bucket.__name == "BigBucket"
498 498
499</YueDisplay> 499</YueDisplay>
500 500
501You can even leave off the body, meaning you can write a blank anonymous class like this: 501Anda bahkan bisa menghilangkan badan kelas, artinya Anda bisa menulis kelas anonim kosong seperti ini:
502 502
503```yuescript 503```yuescript
504x = class 504x = class
@@ -511,9 +511,9 @@ x = class
511 511
512</YueDisplay> 512</YueDisplay>
513 513
514## Class Mixing 514## Pencampuran Kelas
515 515
516You can do mixing with keyword `using` to copy functions from either a plain table or a predefined class object into your new class. When doing mixing with a plain table, you can override the class indexing function (metamethod `__index`) to your customized implementation. When doing mixing with an existing class object, the class object's metamethods won't be copied. 516Anda bisa melakukan mixing dengan kata kunci `using` untuk menyalin fungsi dari tabel biasa atau objek kelas yang sudah didefinisikan ke kelas baru Anda. Saat mixing dengan tabel biasa, Anda dapat mengganti fungsi pengindeksan kelas (metamethod `__index`) dengan implementasi kustom. Saat mixing dengan objek kelas yang sudah ada, metamethod objek kelas tidak akan disalin.
517 517
518```yuescript 518```yuescript
519MyIndex = __index: var: 1 519MyIndex = __index: var: 1
@@ -530,7 +530,7 @@ class Y using X
530y = Y! 530y = Y!
531y\func! 531y\func!
532 532
533assert y.__class.__parent ~= X -- X is not parent of Y 533assert y.__class.__parent ~= X -- X bukan parent dari Y
534``` 534```
535<YueDisplay> 535<YueDisplay>
536 536
@@ -549,7 +549,7 @@ class Y using X
549y = Y! 549y = Y!
550y\func! 550y\func!
551 551
552assert y.__class.__parent ~= X -- X is not parent of Y 552assert y.__class.__parent ~= X -- X bukan parent dari Y
553``` 553```
554 554
555</YueDisplay> 555</YueDisplay>