クラスとオブジェクト
オブジェクト指向プログラミング(OOP)はJavaの核心です。このレッスンでは、クラスとオブジェクトの基礎を学びます。
オブジェクト指向プログラミングとは
OOPはデータとそのデータを操作するメソッドを一緒にバンドルするプログラミングパラダイムです。
OOP vs プロシージャルプログラミング
| 特徴 | プロシージャル | オブジェクト指向 |
|---|---|---|
| 思考スタイル | ステップバイステップの実行 | オブジェクトで整理 |
| コード構造 | 関数 | クラスとオブジェクト |
| 最適な場面 | 簡単なタスク | 複雑なシステム |
クラスの定義
クラスはオブジェクトのテンプレートで、属性とメソッドを定義します。
構文
JAVA
public class クラス名 {
// 属性(メンバ変数)
データ型 属性名;
// メソッド(メンバメソッド)
戻り値の型 メソッド名(パラメータリスト) {
// メソッド本体
}
}
例:Studentクラスを定義
JAVA
public class Student {
// 属性
String name;
int age;
double score;
// メソッド
public void study() {
System.out.println(name + "は勉強中");
}
public void showInfo() {
System.out.println("名前: " + name + ", 年齢: " + age + ", スコア: " + score);
}
}
オブジェクトの作成
newキーワードを使用してオブジェクトを作成します。
構文
JAVA
クラス名 オブジェクト名 = new クラス名();
例:オブジェクトの作成と使用
JAVA
public class StudentTest {
public static void main(String[] args) {
// オブジェクトを作成
Student stu1 = new Student();
// 属性を設定
stu1.name = "Alice";
stu1.age = 20;
stu1.score = 95.5;
// メソッドを呼び出し
stu1.study(); // Aliceは勉強中
stu1.showInfo(); // 名前: Alice, 年齢: 20, スコア: 95.5
// 別のオブジェクトを作成
Student stu2 = new Student();
stu2.name = "Bob";
stu2.age = 22;
stu2.score = 88.0;
stu2.study(); // Bobは勉強中
stu2.showInfo(); // 名前: Bob, 年齢: 22, スコア: 88.0
}
}
コンストラクタ
コンストラクタはオブジェクト作成時に属性を初期化するために使用されます。
特徴
| 特徴 | 説明 |
|---|---|
| クラス名と同じ名前 | 必須 |
| 戻り値の型なし | voidすらない |
| newで自動呼び出し | 手動呼び出し不要 |
| オーバーロード可能 | 複数のコンストラクタ |
引数なしコンストラクタ
JAVA
public class Student {
String name;
int age;
// 引数なしコンストラクタ
public Student() {
System.out.println("Studentオブジェクトが作成されました");
}
}
引数付きコンストラクタ
JAVA
public class Student {
String name;
int age;
// 引数付きコンストラクタ
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
例:コンストラクタ
JAVA
public class Student {
String name;
int age;
double score;
// 引数なしコンストラクタ
public Student() {
this.name = "不明";
this.age = 0;
this.score = 0;
}
// 引数付きコンストラクタ
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 全引数コンストラクタ
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public void showInfo() {
System.out.println("名前: " + name + ", 年齢: " + age + ", スコア: " + score);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.showInfo(); // 名前: 不明, 年齢: 0, スコア: 0
Student s2 = new Student("Alice", 20);
s2.showInfo(); // 名前: Alice, 年齢: 20, スコア: 0.0
Student s3 = new Student("Bob", 22, 95.5);
s3.showInfo(); // 名前: Bob, 年齢: 22, スコア: 95.5
}
}
⚠️ 注意: コンストラクタを定義しない場合、Javaは自動的に引数なしコンストラクタを提供します。引数付きコンストラクタを定義した場合、引数なしコンストラクタは自動的に提供されません。
thisキーワード
thisは現在のオブジェクトへの参照です。
用途1:メンバ変数とローカル変数を区別
JAVA
public class Student {
String name;
public void setName(String name) {
this.name = name; // this.nameはメンバ変数、nameはパラメータ
}
}
用途2:他のコンストラクタを呼び出し
JAVA
public class Student {
String name;
int age;
public Student() {
this("不明", 0); // 引数付きコンストラクタを呼び出し
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
例:thisの使用
JAVA
public class Point {
int x, y;
public Point() {
this(0, 0);
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int x, int y) {
this.x += x;
this.y += y;
}
public void show() {
System.out.println("点: (" + x + ", " + y + ")");
}
public static void main(String[] args) {
Point p = new Point(3, 5);
p.show(); // 点: (3, 5)
p.move(2, -1);
p.show(); // 点: (5, 4)
}
}
オブジェクトのメモリモデル
メモリ割り当て
JAVA
Student stu = new Student("Alice", 20);
stu(参照変数)はスタックメモリに格納new Student()(オブジェクト)はヒープメモリに格納stuはオブジェクトのアドレスを保持
例:同じオブジェクトへの複数の参照
JAVA
public class MemoryDemo {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = s1; // s2とs1は同じオブジェクトを指す
System.out.println(s1.name); // Alice
System.out.println(s2.name); // Alice
s2.name = "Bob";
System.out.println(s1.name); // Bob(s1も変更される)
System.out.println(s2.name); // Bob
}
}
複数のオブジェクト
JAVA
public class MultiObject {
public static void main(String[] args) {
// 複数のオブジェクトを作成
Student[] students = new Student[3];
students[0] = new Student("Alice", 20);
students[1] = new Student("Bob", 22);
students[2] = new Student("Charlie", 21);
// 走査
for (Student s : students) {
s.showInfo();
}
}
}
メソッドパラメータとしてのオブジェクト
JAVA
public class StudentUtil {
// パラメータとしてのオブジェクト
public static void printStudent(Student stu) {
System.out.println(stu.name + " - " + stu.age);
}
// オブジェクトを返す
public static Student createStudent(String name, int age) {
return new Student(name, age);
}
public static void main(String[] args) {
Student alice = new Student("Alice", 20);
printStudent(alice); // Alice - 20
Student bob = createStudent("Bob", 22);
bob.showInfo();
}
}
❓ よくある質問
Q クラスとオブジェクトの違いは何ですか?
A クラスはテンプレートです。オブジェクトはインスタンスです。クラスは属性とメソッドの構造を定義し、オブジェクトは具体的なインスタンスです。
Q コンストラクタに戻り値を設定できますか?
A いいえ。コンストラクタには戻り値の型がありません—voidすらありません。
Q thisを省略できますか?
A はい、メンバ変数とローカル変数が同じ名前でない場合。ただし、thisを追加するとコードがより明確になります。
📖 まとめ
- クラスはオブジェクトのテンプレートで、属性とメソッドを定義
- オブジェクトはnewキーワードを使用して作成
- コンストラクタはオブジェクトを初期化し、オーバーロード可能
- thisは現在のオブジェクトを参照し、変数の区別とコンストラクタの呼び出しに使用
- オブジェクトはヒープメモリに、参照はスタックメモリに格納
📝 演習
- Rectangleクラス: 長さと幅の属性、面積と周長を計算するメソッドを持つ矩形クラスを定義
- BankAccountクラス: 入金、出金、残高確認メソッドを持つ銀行口座クラスを定義
- オブジェクト配列: 5つのStudentオブジェクトを作成し、最高スコアを持つものを見つける
次のレッスン
次のレッスンでは、カプセル化を学びます — オブジェクトデータを保護する方法。



