Skip to content

Commit 8bc8663

Browse files
committed
Add "More Resources" section.
1 parent f822dff commit 8bc8663

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

_th/tour/traits.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ Trait ใช้เพื่อแชร์ interface และ field ระห
2121

2222
วิธีที่ง่ายที่สุดในการกำหนด trait คือการประกาศด้วย keyword `trait` และ indentifier:
2323

24-
{% tabs trait-hair-color %} {% tab 'Scala 2 and 3' for=trait-hair-color %}
24+
{% tabs trait-hair-color %}
25+
{% tab 'Scala 2 and 3' for=trait-hair-color %}
2526

2627
```scala mdoc
2728
trait HairColor
2829
```
2930

30-
{% endtab %} {% endtabs %}
31+
{% endtab %}
32+
{% endtabs %}
3133

3234
trait จะมีประโยชน์อย่างยิ่งด้วยการเป็น generic type และเป็น abstract method
3335

@@ -56,9 +58,9 @@ trait Iterator[A]:
5658

5759
{% endtabs %}
5860

59-
การขยาย (extend) `trait Iterator[A]` ต้องการ type `A` และ implementation ของ method `hasNext` และ `next`
61+
การขยาย (extend) `trait Iterator[A]` ต้องมี implementation ของ method `hasNext` และ `next` ของ type `A`
6062

61-
## การใช้ Trait
63+
## การใช้งาน Trait
6264

6365
ใช้ keyword `extends` เพื่อขยาย trait จากนั้นให้ implement abstract member ใดๆ ของ trait โดยใช้ keyword `override`:
6466

@@ -198,3 +200,8 @@ animals.foreach(pet => println(pet.name)) // แสดงค่า Harry Sally
198200
199201
`trait Pet` มี abstract field `name` ซึ่ง implement ไว้ใน constructor ของคลาส `Cat` และ `Dog`\
200202
ในบรรทัดสุดท้าย เราเรียกใช้ `pet.name` ซึ่งได้มีการ implement `name` ไว้ใน subtype ใดๆ ของ trait `Pet` แล้ว
203+
204+
## แหล่งข้อมูลเพิ่มเติม
205+
206+
* Learn more about traits in the [Scala Book](/scala3/book/domain-modeling-tools.html#traits)
207+
* Use traits to define [Enum](/scala3/book/domain-modeling-fp.html#modeling-the-data)

0 commit comments

Comments
 (0)