ユーザ用ツール

サイト用ツール


programming:継承時のコンストラクタ呼び出し

実行結果

$ dotnet run
This is SuperClassSample Constructor.Argument is 1234
This is ChildClassSample Constructor.

ソース

Program.cs
using System;
 
namespace console
{
    class Program
    {
        static void Main(string[] args)
        {
            ChildClassSample Child = new ChildClassSample();
        }
    }
 
    public class SuperClassSample
    {
        public SuperClassSample(int IntArg)
        {
            Console.WriteLine("This is SuperClassSample Constructor.Argument is " + IntArg);
        }
    }
 
    public class ChildClassSample : SuperClassSample
    {
        // we need "base(some integer variable)" because compiler can not know how to call SuperClass Constructor.
        public ChildClassSample() : base(1234)
        {
            Console.WriteLine("This is ChildClassSample Constructor.");
        }
    }
}
programming/継承時のコンストラクタ呼び出し.txt · 最終更新: 2017/08/09 04:48 by dot