【初心者向け】プログラミングの「オブジェクト指向」入門

プログラミング初心者向けにオブジェクト指向の基本概念を分かりやすく解説。クラス、オブジェクト、継承などの重要な概念を実例で学びます。

Learning Next 運営
24 分で読めます

【初心者向け】プログラミングの「オブジェクト指向」入門

みなさん、プログラミングを学んでいて「オブジェクト指向」という言葉を聞いたことはありませんか?

「オブジェクト指向って何?」「クラスとかオブジェクトとか、専門用語が多くて難しそう」と感じたことはありませんか?

この記事では、プログラミング初心者向けにオブジェクト指向の基本概念を分かりやすく解説します。日常生活の例を使って説明するので、難しいと感じる必要はありません。

オブジェクト指向とは何か?

身近な例で理解しよう

オブジェクト指向を理解するために、まず「車」を例に考えてみましょう。

車には「色」「メーカー」「燃料タンクの容量」などの特徴があります。 車には「走る」「止まる」「曲がる」などの動作があります。

オブジェクト指向では、このような特徴と動作をまとめて「オブジェクト」として扱います。

プログラミングでの考え方

プログラミングにおけるオブジェクト指向は、以下のような考え方です。

// 車のオブジェクト例
const car = {
// 特徴(プロパティ)
color: "赤",
maker: "トヨタ",
fuelCapacity: 50,
// 動作(メソッド)
start: function() {
console.log("エンジンを始動します");
},
stop: function() {
console.log("エンジンを停止します");
},
accelerate: function() {
console.log("加速します");
}
};

このように、関連する特徴と動作を一つのまとまりとして扱うのがオブジェクト指向の基本です。

なぜオブジェクト指向が重要なのか

オブジェクト指向には、以下のような利点があります。

現実世界のものをプログラムで表現しやすくなります。 コードの整理整頓がしやすくなります。 同じようなものを効率的に作成できます。

クラスとオブジェクトの違い

クラスは「設計図」

クラスは、オブジェクトを作るための設計図のようなものです。

// 車のクラス(設計図)
class Car {
constructor(color, maker, fuelCapacity) {
this.color = color;
this.maker = maker;
this.fuelCapacity = fuelCapacity;
}
start() {
console.log(`${this.maker}${this.color}い車のエンジンを始動します`);
}
stop() {
console.log("エンジンを停止します");
}
accelerate() {
console.log("加速します");
}
}

クラスそのものは、まだ実際の車ではありません。 車を作るための設計図や仕様書のようなものです。

オブジェクトは「実際のもの」

オブジェクトは、クラスを使って作られた実際の「もの」です。

// クラスからオブジェクトを作成
const myCar = new Car("赤", "トヨタ", 50);
const friendCar = new Car("青", "ホンダ", 40);
// それぞれのオブジェクトは独立している
myCar.start(); // "トヨタの赤い車のエンジンを始動します"
friendCar.start(); // "ホンダの青い車のエンジンを始動します"

一つのクラスから、複数の異なるオブジェクトを作成できます。

分かりやすい例え

クラスとオブジェクトの関係を、もう少し分かりやすく例えてみましょう。

たい焼きの型(クラス)→ たい焼き(オブジェクト)
- たい焼きの型:一つの型で何個でもたい焼きを作れる
- たい焼き:それぞれが独立した存在で、中身が違うかもしれない
クッキーの型(クラス)→ クッキー(オブジェクト)
- クッキーの型:星形、ハート形などの形を定義
- クッキー:実際に食べられる個別のクッキー

型が一つあれば、同じ形のものを何個でも作れますよね。

オブジェクト指向の基本要素

カプセル化

カプセル化は、関連するデータと処理を一つにまとめることです。

// 銀行口座のクラス例
class BankAccount {
constructor(accountNumber, initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// 残高を確認する
getBalance() {
return this.balance;
}
// お金を入金する
deposit(amount) {
if (amount > 0) {
this.balance += amount;
console.log(`${amount}円を入金しました。残高:${this.balance}`);
}
}
// お金を出金する
withdraw(amount) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
console.log(`${amount}円を出金しました。残高:${this.balance}`);
} else {
console.log("出金できません");
}
}
}

銀行口座に関する情報と操作が一つのクラスにまとまっています。

継承

継承は、既存のクラスを基にして新しいクラスを作る仕組みです。

// 基本的な動物クラス
class Animal {
constructor(name) {
this.name = name;
}
eat() {
console.log(`${this.name}が食べています`);
}
sleep() {
console.log(`${this.name}が眠っています`);
}
}
// 犬クラス(動物クラスを継承)
class Dog extends Animal {
constructor(name, breed) {
super(name); // 親クラスのコンストラクタを呼び出し
this.breed = breed;
}
// 犬固有の行動
bark() {
console.log(`${this.name}がワンワンと吠えています`);
}
}
// 猫クラス(動物クラスを継承)
class Cat extends Animal {
constructor(name, color) {
super(name);
this.color = color;
}
// 猫固有の行動
meow() {
console.log(`${this.name}がニャーニャーと鳴いています`);
}
}

継承により、共通の部分を再利用しながら、それぞれの特徴を追加できます。

// 継承を使った例
const myDog = new Dog("ポチ", "柴犬");
const myCat = new Cat("タマ", "三毛猫");
// 親クラスのメソッドも使用可能
myDog.eat(); // "ポチが食べています"
myCat.sleep(); // "タマが眠っています"
// 子クラス固有のメソッドも使用可能
myDog.bark(); // "ポチがワンワンと吠えています"
myCat.meow(); // "タマがニャーニャーと鳴いています"

ポリモーフィズム

ポリモーフィズムは、同じメソッド名でも、オブジェクトによって異なる動作をすることです。

// 図形の基本クラス
class Shape {
calculateArea() {
return 0;
}
}
// 円クラス
class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}
calculateArea() {
return Math.PI * this.radius * this.radius;
}
}
// 四角形クラス
class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}
calculateArea() {
return this.width * this.height;
}
}
// 同じメソッド名でも異なる計算をする
const circle = new Circle(5);
const rectangle = new Rectangle(4, 6);
console.log(circle.calculateArea()); // 円の面積計算
console.log(rectangle.calculateArea()); // 四角形の面積計算

同じcalculateAreaメソッドでも、オブジェクトによって異なる計算が実行されます。

実践的な例:ゲームキャラクター

基本的なキャラクタークラス

ゲームのキャラクターを例に、オブジェクト指向を実践してみましょう。

// 基本キャラクタークラス
class Character {
constructor(name, health, attackPower) {
this.name = name;
this.health = health;
this.maxHealth = health;
this.attackPower = attackPower;
}
// 攻撃する
attack(target) {
console.log(`${this.name}${target.name}を攻撃!`);
target.takeDamage(this.attackPower);
}
// ダメージを受ける
takeDamage(damage) {
this.health -= damage;
if (this.health < 0) {
this.health = 0;
}
console.log(`${this.name}${damage}のダメージを受けた!残りHP:${this.health}`);
if (this.health === 0) {
console.log(`${this.name}は倒れた...`);
}
}
// 回復する
heal(amount) {
this.health += amount;
if (this.health > this.maxHealth) {
this.health = this.maxHealth;
}
console.log(`${this.name}${amount}回復した!現在HP:${this.health}`);
}
}

基本的なキャラクターの動作を定義しています。

特化したキャラクタークラス

基本クラスを継承して、特化したキャラクターを作成します。

// 戦士クラス
class Warrior extends Character {
constructor(name) {
super(name, 100, 20); // 高体力、高攻撃力
this.shield = 10;
}
// 特殊攻撃
powerAttack(target) {
console.log(`${this.name}の強力な攻撃!`);
target.takeDamage(this.attackPower * 2);
}
// ダメージを受ける時の処理をオーバーライド
takeDamage(damage) {
const actualDamage = Math.max(0, damage - this.shield);
console.log(`${this.name}のシールドが${Math.min(damage, this.shield)}ダメージを軽減!`);
super.takeDamage(actualDamage);
}
}
// 魔法使いクラス
class Mage extends Character {
constructor(name) {
super(name, 60, 15); // 低体力、中攻撃力
this.mana = 50;
}
// 魔法攻撃
magicAttack(target) {
if (this.mana >= 10) {
console.log(`${this.name}の魔法攻撃!`);
this.mana -= 10;
target.takeDamage(this.attackPower * 1.5);
console.log(`残りMP:${this.mana}`);
} else {
console.log("MPが足りません!");
}
}
// 回復魔法
healingMagic(target) {
if (this.mana >= 15) {
console.log(`${this.name}の回復魔法!`);
this.mana -= 15;
target.heal(30);
console.log(`残りMP:${this.mana}`);
} else {
console.log("MPが足りません!");
}
}
}

それぞれのキャラクターに特有の能力を追加しています。

ゲームの実行例

作成したクラスを使って、実際にゲームを動かしてみましょう。

// キャラクターを作成
const warrior = new Warrior("勇者");
const mage = new Mage("魔法使い");
console.log("=== バトル開始 ===");
// 戦士が通常攻撃
warrior.attack(mage);
// 魔法使いが魔法攻撃
mage.magicAttack(warrior);
// 戦士が強力な攻撃
warrior.powerAttack(mage);
// 魔法使いが回復魔法を自分に使用
mage.healingMagic(mage);
console.log("=== バトル終了 ===");

オブジェクト指向の特徴を活かして、拡張性の高いゲームシステムを作成できます。

オブジェクト指向のメリット

コードの再利用性

一度作成したクラスは、何度でも使用できます。

// 同じクラスから複数のオブジェクトを作成
const hero1 = new Warrior("アルフレッド");
const hero2 = new Warrior("ベルンハルト");
const hero3 = new Mage("セラフィナ");
// それぞれ独立したオブジェクトとして動作
hero1.attack(hero2);
hero3.healingMagic(hero2);

新しいキャラクターが必要になっても、既存のクラスを再利用できます。

保守性の向上

関連する機能がまとめられているため、修正や改良が容易です。

// 例:すべてのキャラクターに経験値システムを追加
class Character {
constructor(name, health, attackPower) {
this.name = name;
this.health = health;
this.maxHealth = health;
this.attackPower = attackPower;
this.experience = 0; // 経験値を追加
this.level = 1; // レベルを追加
}
// 経験値を獲得
gainExperience(exp) {
this.experience += exp;
console.log(`${this.name}${exp}の経験値を獲得!`);
// レベルアップ判定
if (this.experience >= this.level * 100) {
this.levelUp();
}
}
// レベルアップ
levelUp() {
this.level++;
this.maxHealth += 10;
this.health = this.maxHealth;
this.attackPower += 5;
console.log(`${this.name}がレベルアップ!レベル${this.level}になりました!`);
}
}

基本クラスに機能を追加するだけで、すべての子クラスに機能が追加されます。

理解しやすさ

現実世界のものをモデル化するため、プログラムの構造が理解しやすくなります。

// 直感的に理解できるクラス設計
class Book {
constructor(title, author, pages) {
this.title = title;
this.author = author;
this.pages = pages;
this.currentPage = 0;
}
read() {
if (this.currentPage < this.pages) {
this.currentPage++;
console.log(`${this.title}を読んでいます...(${this.currentPage}/${this.pages}ページ)`);
} else {
console.log(`${this.title}を読み終わりました!`);
}
}
}
const book = new Book("プログラミング入門", "田中太郎", 300);
book.read(); // 本を読む動作が直感的

初心者が注意すべきポイント

過度な設計を避ける

最初は、シンプルなクラス設計から始めましょう。

// 最初はシンプルに
class Student {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
study() {
console.log(`${this.name}が勉強しています`);
}
}
// 慣れてきたら機能を追加
class Student {
constructor(name, grade) {
this.name = name;
this.grade = grade;
this.subjects = [];
this.scores = {};
}
addSubject(subject) {
this.subjects.push(subject);
}
setScore(subject, score) {
this.scores[subject] = score;
}
getAverageScore() {
const scores = Object.values(this.scores);
return scores.reduce((sum, score) => sum + score, 0) / scores.length;
}
}

機能は段階的に追加していくことをおすすめします。

適切な抽象化レベル

現実世界とプログラムの世界のバランスを考えましょう。

// 適切な抽象化レベル
class Calculator {
add(a, b) {
return a + b;
}
subtract(a, b) {
return a - b;
}
multiply(a, b) {
return a * b;
}
divide(a, b) {
if (b === 0) {
throw new Error("ゼロで割ることはできません");
}
return a / b;
}
}

必要な機能を過不足なく実装することが重要です。

命名規則の統一

クラス名、メソッド名、プロパティ名を統一された規則で命名しましょう。

// 良い命名例
class UserAccount { // クラス名:大文字始まり
constructor(userName) { // 引数:キャメルケース
this.userName = userName; // プロパティ:キャメルケース
this.isActive = true;
}
activateAccount() { // メソッド名:動詞+名詞
this.isActive = true;
}
deactivateAccount() {
this.isActive = false;
}
}

一貫した命名規則により、コードの可読性が向上します。

実践的な学習方法

身近なものをモデル化

日常生活の身近なものをクラスとして設計してみましょう。

// 携帯電話をモデル化
class Phone {
constructor(brand, model) {
this.brand = brand;
this.model = model;
this.batteryLevel = 100;
this.isOn = false;
}
turnOn() {
this.isOn = true;
console.log("電源をオンにしました");
}
turnOff() {
this.isOn = false;
console.log("電源をオフにしました");
}
makeCall(number) {
if (this.isOn && this.batteryLevel > 0) {
console.log(`${number}に電話をかけています...`);
this.batteryLevel -= 5;
} else {
console.log("電話をかけることができません");
}
}
}

身近なものをモデル化することで、オブジェクト指向の考え方が身につきます。

段階的に機能を追加

最初はシンプルな機能から始めて、徐々に機能を追加していきましょう。

// 最初のバージョン
class TodoItem {
constructor(title) {
this.title = title;
this.completed = false;
}
complete() {
this.completed = true;
}
}
// 機能を追加したバージョン
class TodoItem {
constructor(title, priority = 'medium') {
this.title = title;
this.completed = false;
this.priority = priority;
this.createdAt = new Date();
this.dueDate = null;
}
complete() {
this.completed = true;
this.completedAt = new Date();
}
setDueDate(date) {
this.dueDate = date;
}
isOverdue() {
return this.dueDate && new Date() > this.dueDate && !this.completed;
}
}

段階的に機能を追加することで、設計の改善方法を学べます。

まとめ

オブジェクト指向は、プログラミングにおける重要な考え方の一つです。

現実世界のものをモデル化することで、理解しやすく保守性の高いプログラムを作成できます。クラス、オブジェクト、継承、カプセル化、ポリモーフィズムなどの基本概念を理解することが第一歩です。

最初は難しく感じるかもしれませんが、身近な例から始めて段階的に学習を進めることが大切です。 完璧を目指すよりも、まずはシンプルなクラスを作成することから始めてみませんか?

ぜひ、今回学んだ内容を参考に、オブジェクト指向プログラミングに挑戦してみてください。 継続的に練習することで、より良いプログラムを書けるようになり、プログラミングの楽しさをより深く味わえるようになるでしょう!

関連記事