0Pricing
Kotlin Multiplatform Academy · บทเรียน

Enum และคลาสปิดผนึกสำหรับสถานะ

จำลองตัวเลือกตายตัวและรูปแบบผลลัพธ์อย่างปลอดภัย

Enum และคลาสปิดผนึกสำหรับสถานะ เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Model Fixed Choices

Some values have a known, limited set of options. Kotlin gives you enums and sealed classes to model them safely in shared code.

Meet the enum class

An enum class lists every allowed value by name. Nothing outside that list can ever sneak in.

enum class Status { ACTIVE, PAUSED, DONE }

Use an enum Value

Refer to an option by its name. Because the set is closed, your state is always one of the listed cases.

val current = Status.ACTIVE

Enums Can Carry Data

Give an enum constructor properties so each case can hold extra fixed info, like a label or numeric code.

enum class Plan(val price: Int) { FREE(0), PRO(9) }

When Cases Need Different Data

Enums share one shape. When variants need different fields, reach for a sealed class instead.

Meet the sealed class

A sealed class defines a closed family of subtypes. Each subtype can carry its own distinct data.

sealed class Result
data class Ok(val value: String) : Result()
data class Err(val message: String) : Result()

Why Sealed Is Powerful

Because the compiler knows every subtype, a sealed class models success, loading and error states with type-safe payloads.

val state: Result = Ok("loaded")

Match with when

Handle each case using when. The compiler checks you covered every variant of the sealed type.

when (state) {
  is Ok -> println(state.value)
  is Err -> println(state.message)
}

Exhaustive when, No else

When you handle all sealed cases, you can drop the else branch. Add a new subtype later and the compiler reminds you.

Shared, So Both Apps Agree

Defined in commonMain, these state types drive identical logic on Android and iOS with zero drift.

Pick the Right Tool

Use an enum for simple fixed options, and a sealed class when each case carries different data.

Quick Check

Which type should model these states?

Recap

Use shared enums for fixed options and sealed classes for variants with their own data, then match them safely with when.

คำถามที่พบบ่อย

บทเรียน “Enum และคลาสปิดผนึกสำหรับสถานะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Enum และคลาสปิดผนึกสำหรับสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Enum และคลาสปิดผนึกสำหรับสถานะ”

จำลองตัวเลือกตายตัวและรูปแบบผลลัพธ์อย่างปลอดภัย คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “Enum และคลาสปิดผนึกสำหรับสถานะ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คลาสข้อมูลสำหรับโดเมนของคุณ
  2. Enum และคลาสปิดผนึกสำหรับสถานะ
  3. ความปลอดภัยของค่า null ในโมเดล shared
  4. ตัวช่วยตรวจสอบข้อมูลในโมเดล
← กลับไปที่ Kotlin Multiplatform Academy