基本動作 研究。目的;復習による自己テスト。

・プロパティ

・メソッド

・コンストラクタ

・ギミック(配列ループ)

 

 

 

<<プロパティ>>

int aがないと機能しない。

set→getの順がわかりやすい

クラス設定→インスタンス

インスタンス化したプロパティで数値代入。

test1.i  だけで数値が返ってくる。

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Propety
    {
        int a;
        public int i
    {

        set { a = value+100; }

        get
        {


            return (a+10);
        }   
 
    
    }
    
    
    }

    class Program
    {
        static void Main(string args)
        {
            Propety test1 = new Propety();

            test1.i = 10;
            System.Console.WriteLine(test1.i);

        }
    }
}

 

 

<<メソッド>>

・クラス作成

・戻り値の型、 メソッド名、 (引数)、return とreturn 用の変数

確かに、プロパティと比較すると必要情報が多いので長くなりがち。

 

・利用

インスタンス化

引数用の変数を設定(多分ここ、もっと効率化できる)

 

解答用の変数を記述。

インスタンス化したクラス、メソッド名、引数;

  c = test1.tasizan(a, b);

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{

    class test
    {

        public int tasizan(int a, int b)
        {

            int c;

            c = a + b;

                return c;
        }

    }
    class Program
    {
        static void Main(string args)
        {

            test test1 = new test();

            int a = 1;
            int b = 2;
            int c ;

            c = test1.tasizan(a, b);

            Console.WriteLine(c);



        }
    }
}

 

<<コンストラクタ>>

コンストラクタの役割。

・インスタンス化したクラスに初期値を与える

・コンストラクタはクラス名と同じ

 

・引数を与えて計算もできる

(プロパティでよくね?とも思った)

・コンストラクタは複数用意できる

今回は、引数の有無でスイッチ可能にした

 

また、

インスタンス化は複数可能。

           test test1 = new test();
           test test2 = new test(1,1);

でも問題なく動作を確認。

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{

    class test
    {
    
     public test()
     {
         System.Console.WriteLine("引数なし コンストラクタを起動");
    }
     public test(int a, int b)
     {
         int c;

         c = a + b;
         Console.WriteLine("引数を与えて起動" + a + " " + b + " " + c + " ");
     }
    
    }


    class Program
    {
        static void Main(string args)
        {
            Console.WriteLine("メインクラスを起動");
           test test1 = new test();
           test test2 = new test(1,1);
           Console.WriteLine("メインクラスを終了");

        }
    }
}

<<ギミック(配列ループ)>>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string args)
        {

            String test = "Hellow World";

            for (int i = 0; i < test.Length; i++)
            {

                Console.WriteLine(test[i]);
            }
        }
    }
}