zoukankan      html  css  js  c++  java
  • C#构造函数与初始化列表

     1 // Namespace Declaration
     2 using System;
     3 
     4 // helper class
     5 class OutputClass
     6 {
     7     string myString;
     8 
     9     // Constructor
    10     public OutputClass(string inputString)
    11     {
    12         myString = inputString;
    13     }
    14 
    15     //this指向特定对象,它产生了对自己带参构造函数的调用
    16     public OutputClass() : this("default string")
    17     {
    18     }
    19 
    20     // Instance Method
    21     public 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
    34 class ExampleClass
    35 {
    36     // Main begins program execution.
    37     public 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.");
    43 
    44         // Call Output class' method
    45         outCl.printString();
    46 
    47         outC2.printString();
    48     }
    49 }
  • 相关阅读:
    汉诺塔
    破损的键盘
    解方程
    运输计划
    选学霸
    子集和的目标值
    棋盘染色2
    守卫者的挑战
    飞扬的小鸟
    攻克城堡
  • 原文地址:https://www.cnblogs.com/johnpher/p/2741249.html
Copyright © 2011-2022 走看看