オーバーロード

メソッドを引数がある場合やない場合でいくつかの形で使用できるようにする

サンプル

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication31
{
    class Program
    {
        static void Main(string[] args)
        {
            Test4 a;
            a = new Test4();
 
            a.testFunc1();         // 引数なし
            a.testFunc1("a");      // string型
            a.testFunc1(10);       // int型
            a.testFunc1("a", 900); // 引数の数を増やしても対応(定義は必要)
 
            Console.ReadKey();
 
        }
    }
 
    class Test4
    {
        public Test4()
        {
            Console.WriteLine("コンストラクタ");
        }
 
        ~Test4()
        {
            Console.WriteLine("デストラクタ");
        }
 
        public void testFunc1()
        {
            Console.WriteLine("引数なし");
        }
 
        public void testFunc1(string a)
        {
            Console.WriteLine("(string) a={0}", a);
        }
 
        public void testFunc1(int a)
        {
            Console.WriteLine("(int) a={0}", a);
        }
 
        public void testFunc1(string a, int b)
        {
            Console.WriteLine("(a,b) a={0}, b={1}", a, b);
        }
    }
 
}
 
 
 




最終更新:2011年04月09日 23:31