ドメイン用のData Classes
copyとequalsを備えた不変の共有モデルを作ります
「ドメイン用のData Classes」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Your Domain Needs Models
Every app describes real things like users, orders or songs. In KMP you model them once with shared data classes.
Meet the data class
A data class is a small type built to hold values. You add the keyword and list the properties in the header.
data class User(val id: Int, val name: String)It Lives in commonMain
Put your model in commonMain and both the Android and iOS apps see the exact same shape with no duplication.
// shared/src/commonMain
data class User(val id: Int, val name: String)Create an Instance
Build a model by calling it like a function and passing each value. The object now carries your data everywhere.
val ada = User(id = 1, name = "Ada")Read the Properties
Access any field with a dot. Reading properties works the same in shared code, Android and iOS.
val label = ada.name + " #" + ada.idFree toString
A data class auto-generates a readable toString, so printing a model shows its values instead of a cryptic address.
println(ada) // User(id=1, name=Ada)Value-Based equals
Two models with the same fields are considered equal. Data classes compare by content, not by memory identity.
User(1, "Ada") == User(1, "Ada") // trueCopy with Changes
The free copy function clones a model while overriding only the fields you name, keeping the original untouched.
val renamed = ada.copy(name = "Grace")Prefer Immutable val
Declare fields as val so models cannot change after creation. Immutable data is safer to share across platforms.
data class Song(val title: String, val seconds: Int)Compose Models Together
A model can hold other models as fields, letting you nest data classes to mirror real relationships in your domain.
data class Playlist(val owner: User, val songs: List<Song>)Why This Matters
One shared model means Android and iOS can never disagree about your data shape, killing a whole class of bugs. 🙂
Quick Check
Which free feature did the data class give you here?
Recap
Shared data classes in commonMain give you copy, equals and toString for free, modeling your domain once for both apps.
よくある質問
「ドメイン用のData Classes」レッスンは無料ですか?
はい。「ドメイン用のData Classes」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「ドメイン用のData Classes」で何を学びますか?
copyとequalsを備えた不変の共有モデルを作ります ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「ドメイン用のData Classes」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ドメイン用のData Classes
- 状態を表すEnumsとSealed Classes
- 共有モデルのNull Safety
- モデルのValidation Helpers