0Pricing
Kotlin Multiplatform Academy · レッスン

expect/actualのクラスとプロパティ

関数以外にexpect/actualで宣言できるものを学びます

「expect/actualのクラスとプロパティ」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Beyond Functions

expect/actual is not limited to functions. You can also declare whole classes, properties, and even objects that differ per platform.

An expect Property

Need a value that each platform defines? Declare an expect val in commonMain and let every target supply the actual value.

expect val platformName: String

Each Target Sets It

Android and iOS each provide an actual val. The property reads like a constant in shared code but resolves per platform.

actual val platformName: String = "iOS"

An expect Class

For richer behavior, declare an expect class. It lists the members shared code can use, while each platform fills in the bodies.

expect class Platform() {
  val name: String
}

The Android actual Class

The androidMain actual class implements every declared member. Here name returns an Android-specific string.

actual class Platform actual constructor() {
  actual val name = "Android"
}

Match Members Exactly

Each actual class must declare every member the expect promised, with the same names and types. Missing one is a compile error.

Constructors Need actual Too

If the expect class has a constructor, mark the platform one as an actual constructor. The compiler checks that they line up.

Use It Like Any Class

In shared code you just create and use it. The right actual backs the object, so common logic stays unaware of the platform.

val name = Platform().name

Add Platform-Only Extras

An actual class may add extra private members beyond the expect. Only what the expect declares is visible to shared code.

Prefer the Smallest Tool

Use an expect property for a value, an expect function for an action, and an expect class only when you need state plus behavior. ✨

A Familiar Modern Note

Recent Kotlin nudges you toward interfaces plus expect/actual functions over expect classes, but classes still work well today.

Quick Check

Consider what an actual class owes its expect.

Recap

You saw that expect/actual covers properties and classes too. Declare the shape in commonMain, then fulfill every member in each platform's actual. 🎉

よくある質問

「expect/actualのクラスとプロパティ」レッスンは無料ですか?

はい。「expect/actualのクラスとプロパティ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。

「expect/actualのクラスとプロパティ」で何を学びますか?

関数以外にexpect/actualで宣言できるものを学びます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Kotlin Multiplatform Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「expect/actualのクラスとプロパティ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?

はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. expect/actualが存在する理由
  2. ターゲットごとにプラットフォーム名を取得する
  3. expect/actualのクラスとプロパティ
  4. よくあるミスとコンパイラーエラー
← Kotlin Multiplatform Academyに戻る