2017年9月6日 星期三

[C# ] 繼承的使用範例 - class


繼承


範例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main(string[] args)
{
inher testInher = new inher();
Console.Read();
}
}
class ExampleClass
{
public ExampleClass()
{
Console.WriteLine(" 00 message of ExampleClass: in class ExampleClass()");
}
}
class inher : ExampleClass
{
public inher()
{
Console.WriteLine(" 01 message of inher(): in class inher : ExampleClass");
}
}
}
//==============================
// run result as following:
//==============================
// 00 message of ExampleClass: in class ExampleClass()
// 01 message of inher(): in class inher : ExampleClass
view raw gistfile1.txt hosted with ❤ by GitHub


執行結果:

 00 message of ExampleClass: in class ExampleClass()
 01 message of inher(): in class inher : ExampleClass

可以看到,當透過 new 實體化 testInher 變數時候,會依序執行 建構式 ,
因此會出現上列message


沒有留言:

張貼留言