繼承
範例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
執行結果:
00 message of ExampleClass: in class ExampleClass()
01 message of inher(): in class inher : ExampleClass
可以看到,當透過 new 實體化 testInher 變數時候,會依序執行 建構式 ,
因此會出現上列message
沒有留言:
張貼留言