---
title: "継承"
content_type: tutorial
url: "https://learn.unity.com/tutorial/inheritance-jp?language=ja"
language: ja
difficulty: intermediate
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2019.3"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/inheritance-jp.md?language=ja&version=4.x"
last_updated: "2026-08-04T14:23:03.000Z"
---

# 継承

継承によってコードを再利用し、関連するクラス間に強力な関係を構築する方法を学びます。

継承を使用してコードを再利用し、関連するクラス間に強力な関係を構築する方法を学びます。

## 継承

[Video](http://www.youtube.com/watch?v=F7Wu6_uzD1I)

**Fruit クラスのコード**

```csharp
using UnityEngine;
using System.Collections;

// これは基底クラスです。
// 親クラスとも呼ばれます。
public class Fruit 
{
    public string color;

    // これは Fruit クラスの最初のコンストラクターです。
    // 派生クラスには継承されません。
    public Fruit()
    {
        color = "orange";
        Debug.Log("1st Fruit Constructor Called");
    }

    // これは Fruit クラスの 2 番目のコンストラクタです。
    // 派生クラスには継承されません。
    public Fruit(string newColor)
    {
        color = newColor;
        Debug.Log("2nd Fruit Constructor Called");
    }

    public void Chop()
    {
        Debug.Log("The " + color + " fruit has been chopped.");        
    }

    public void SayHello()
    {
        Debug.Log("Hello, I am a fruit.");
    }
}
```

**Apple クラスのコード**

```csharp
using UnityEngine;
using System.Collections;

// これは派生クラスです。
// 子クラスとも呼ばれます。
public class Apple : Fruit 
{
    // これは Apple クラスの最初のコンストラクタです。
    // 実行前でも、直ちに親コンストラクタを呼び出します。
    public Apple()
    {
        // Apple が親の Fruit クラスの一部である public 変数 colorに
        // アクセスする方法に注目してください。
        color = "red";
        Debug.Log("1st Apple Constructor Called");
    }

    // これは Apple クラスの2番目のコンストラクタです。
    // "base "キーワードを使用して、どの親コンストラクタが呼び出されるかを指定しています。
    public Apple(string newColor) : base(newColor)
    {
        // このコンストラクターでは色を設定しないことに注意してください。
        // これは基底クラスのコンストラクターで、引数として渡された色を設定しているためです。
        Debug.Log("2nd Apple Constructor Called");
    }
}
```

**FruitSalad クラスのコード**

```csharp
using UnityEngine;
using System.Collections;

public class FruitSalad : MonoBehaviour 
{
    void Start () 
    {
        // デフォルトコンストラクターを使って
        // 継承がどのように行われているか見てみましょう。
        Debug.Log("Creating the fruit");
        Fruit myFruit = new Fruit();
        Debug.Log("Creating the apple");
        Apple myApple = new Apple();

        // Fruit クラスのメソッドを呼び出します。
        myFruit.SayHello();
        myFruit.Chop();

        // Apple クラスのメソッドを呼び出します。
        // Apple クラスが Fruit クラスのすべての public メソッドに
        // アクセスできることに注意してください。
        myApple.SayHello();
        myApple.Chop();

        // 次は、文字列を読み取るコンストラクターを使用して
        // 継承がどのように行われているか見てみましょう。
        Debug.Log("Creating the fruit");
        myFruit = new Fruit("yellow");
        Debug.Log("Creating the apple");
        myApple = new Apple("green");

        // Fruit クラスのメソッドを呼び出します。
        myFruit.SayHello();
        myFruit.Chop();

        // Apple クラスのメソッドを呼び出します。
        // Apple クラスが Fruit クラスのすべての public メソッドに
        // アクセスできることに注意してください。
        myApple.SayHello();
        myApple.Chop();
    }
}
```
