半人前プログラマーの技術談議

開発したもの紹介していくブログ

C# オブジェクト指向編 参照型 part2-4

今回の内容はオブジェクト指向とあまり関係ないかもしれません.

ですが知ってほしい内容です!

 

参照型

変数の型には,値型(データ型)と参照型があります.

 

値型変数は,値そのもの(直接値)を格納するのに対し,

参照型変数は,値が置いてある場所(所在地情報)を格納します.

 

C#では,次のように分類されます.

f:id:pokoshirou:20181013144029p:plain

 

今回は,配列とクラスを例に参照型を扱ってみます.

 

 

 

配列

次のプログラムを例にしてみます.

 

ソースコード

using System;

namespace Training
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] n1 = { 50, 30, 90, 10 };
            int[] n2 = new int[4];

            n2 = n1;

            // 表示
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("n1[" + i + "]" + " = " + n1[i]);
            }

            Console.WriteLine("----------");

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("n2[" + i + "]" + " = " + n2[i]);
            }


            n1[2] = 40; // 90 → 40

            Console.WriteLine("変更後");

            // 表示
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("n1[" + i + "]" + " = " + n1[i]);
            }

            Console.WriteLine("----------");

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("n2[" + i + "]" + " = " + n2[i]);
            }

        }
    }
}

 

実行結果

f:id:pokoshirou:20181013181405p:plain

 

n2にn1の参照先を代入した後,n1[2]の参照先の中身を40に代入して変更すると,n2[2]の値も40に変わっています.

 

このプログラムの流れを図に表してみます.

参照型変数は所在地情報を持っているので,仮に所在地情報を4桁のアルファベッドとして考えてみます.

f:id:pokoshirou:20181013183728p:plain

f:id:pokoshirou:20181013183739p:plain

f:id:pokoshirou:20181013183753p:plain

 

図にすると,動きが見えてくると思います.

 

 

 

クラス

クラスも配列と変わらないです!

次のプログラムを例にしてみます.

 

ソースコード

using System;

namespace Training
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] name = { "自動車", "タクシー", "バス", "消防車" };

            Car[] car = new Car[4];      // Car型の配列を宣言する

            for (int i = 0; i < 4; i++)
            {
                car[i] = new Car(i + 1, name[i]);
                car[i].Display();
                Car.Counter();
            }

            Car c = new Car(0, " ");

            c = car[2];

            car[2].name = "クッキー";

            car[2].Display();
            c.Display();
        }
    }

    public class Car
    {
        private static int count = 0;    // クラス変数

        public int number;      // 番号
        public string name;     // 名前

        public Car(int number, string name)
        {
            this.number = number;
            this.name = name;
            Car.count++;    // コンストラクタが呼び出された回数
        }

        public void Display()   // インスタンスメソッド
        {
            Console.WriteLine("ID:" + this.number + " 名前:" + this.name);
        }

        public static void Counter()    // クラスメソッド
        {
            Console.WriteLine("コンストラクタが呼び出された回数:" + Car.count);
        }
    }
}

 

実行結果

f:id:pokoshirou:20181013184845p:plain

 

参照型なので,変数cの中身も変わっています.

図は省略します.

 

 

 

今回はここまで.

次回から継承に入ります!

補足は番外partで扱っていこうと思います.