C#构造函数与初始化列表 1// Namespace Declaration
2using System;
3
4// helper class
5class OutputClass
6 {
7string myString;
8
9// Constructor
10public OutputClass(string inputString)
11    {
12        myString = inputString;
13    }
14
15//this指向特定对象,它产⽣了对⾃⼰带参构造函数的调⽤
16public OutputClass() : this("default string")
17    {
18    }
19
20// Instance Method
21public void printString()
22    {
23        Console.WriteLine("{0}", myString);
24    }
25
26// Destructor
27    ~OutputClass()
28    {
29// Some resource cleanup routines
30    }
31 }
32
33// Program start class
34class ExampleClass
35 {
36// Main begins program execution.
37public static void Main()
38    {
39// Instance of OutputClass
40        OutputClass outCl = new OutputClass();
41
42        OutputClass outC2 = new OutputClass("This is printed by the output class.");
writeline函数
43
44// Call Output class' method
45        outCl.printString();
46
47        outC2.printString();
48    }
49 }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。