---
title: "オーバーライド"
content_type: tutorial
url: "https://learn.unity.com/tutorial/overriding-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/overriding-jp.md?language=ja&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/tutorial/overriding-jp.md?language=ja&version=4.x"
last_updated: "2026-08-04T14:23:47.000Z"
---

# オーバーライド

C# で基底クラスのメンバーを子クラスのメンバーでオーバーライドする方法を練習します。

基底クラスのメンバーを子クラスのメンバーでオーバーライドする方法を学びます。

## オーバーライド

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

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

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

public class Fruit 
{
    public Fruit ()
    {
        Debug.Log("1st Fruit Constructor Called");
    }

    // これらのメソッドは仮想メソッドであるため、子クラスでオーバーライドできます。
    public virtual void Chop ()
    {
        Debug.Log("The fruit has been chopped.");        
    }

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

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

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

public class Apple : Fruit 
{
    public Apple ()
    {
        Debug.Log("1st Apple Constructor Called");
    }

    // これらのメソッドは override 指定がされているため、
    // 親クラスの仮想メソッドをオーバーライドできます。
    public override void Chop ()
    {
        base.Chop();
        Debug.Log("The apple has been chopped.");        
    }

    public override void SayHello ()
    {
        base.SayHello();
        Debug.Log("Hello, I am an apple.");
    }
}
```

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

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

public class FruitSalad : MonoBehaviour 
{    
    void Start () 
    {
        Apple myApple = new Apple();

        // Apple クラスのバージョンのメソッドが
        // Fruit クラスのバージョンのメソッドをオーバーライドすることに注意してください。
        // また、Apple クラスのバージョンは、Fruit クラスのバージョンを
        // "base" キーワードを使って呼び出すため、両バージョンが呼び出されることにも注意してください。
        myApple.SayHello();
        myApple.Chop();    

        // オーバーライドは、ポリモーフィズムを使う状況でも役立ちます。
        // Fruit クラスのメソッドは仮想メソッドで、
        // Apple クラスのメソッドそれらをオーバーライドしているため
        // Apple クラスのオブジェクトを Fruit クラスのオブジェクトにアップキャストする場合
        // Apple クラスのバージョンのメソッドが使用されます。
        Fruit myFruit = new Apple();
        myFruit.SayHello();
        myFruit.Chop();
    }
}
```
