study-note

クラス

目次

概要

クラス定義

[アクセス修飾子] class クラス名 {
  フィールド定義;
  コンストラクタ;
  メソッド定義;
}

コンストラクタ

種類

  1. デフォルトコンストラクタ
    • 引数なし
  2. 引数月コンストラクタ
    • デフォルトコンストラクタは自動で作られない
    • newのときに引数を与えないとエラーになる

目的

プログラム例

class Person {
  String name;
  int age;

  Person(String name, int age) {
    this.name = name;
    this.age = age;
  }
}

補足

thisキーワード

カプセル化

セッター

void setA(int a) { this.a = a; }

ゲッター

int getA() { return this.a; }

その他

ガーベージコレクタ

staticフィールド/メソッド