60
42

More than 5 years have passed since last update.

TypeScript アクセス修飾子

Last updated at Posted at 2016-04-17

アクセス修飾子

  • public

クラス外からのアクセス可能。

  • private

クラス内でのみアクセス可能。

  • protected

継承クラス内でのみアクセス可能。

  • static

静的なのでクラスで共有。

//スーパークラス定義
class Super {
    constructor() {
        this.public(); //true
        this.private(); //true
        this.protected(); //true
        this.static(); //コンパイルエラー
    }
    public public(): boolean {
        return true;
    }
    private private(): boolean {
        return true;
    }
    protected protected(): boolean {
        return true;
    }
    static static(): boolean {
        return true;
    }
}

//サブクラス定義
class Sub extends Super {
    constructor() {
        super();
        this.public(); //true
        this.private(); //コンパイルエラー
        this.protected(); //true
        this.static(); //コンパイルエラー
    }
}

//インスタンス
let test = new Sub();
test.public(); //true
test.private(); //コンパイルエラー
test.protected(); //コンパイルエラー
test.static(); //コンパイルエラー

//クラス静的メンバ
Sub.public(); //コンパイルエラー
Sub.private(); //コンパイルエラー
Sub.protected(); //コンパイルエラー
Sub.static(); //true
60
42
1

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
60
42