0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Kotlin】sealed class(シールドクラス)

Last updated at Posted at 2022-07-31

はじめに

今回は『sealed class(シールドクラス)』について記事を残します
シールドクラスがどういったものか や、使い方などを書いていきます

Android公式 シールドクラス

sealed class(シールドクラス)とは

公式にもある通り、継承をより詳細に制御できる
つまり クラス定義でsealedを付けると、「そのクラスを継承できるクラス」を限定することが出来る
というものです。

シールドクラスとは 直訳すると 「封印されたクラス」「密封されたクラス」といったようになると思いますが、
シールドクラスが宣言されたファイル内でしかそのシールドクラスは継承できないということになります。

使い方

クラスを定義するときに classの前に sealed をつけるだけです。

シールドクラスを使うメリット

・エラー発生時にエラー箇所を探す、確認する箇所が少なくできる
・独自のサブクラスを設定できる
・インスタンスの状態を後から変更することもできる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?