zoukankan      html  css  js  c++  java
  • 成员变量初始化的步骤

    .NET加载类时:(在静态存储区,为静态变量分配存储空间同时赋予缺省值; 下一步执行且仅执行一次1,2 ,对变量初始化)

    1,先使用 = 为静态成员变量赋值,从上到下,依次赋值,没有 = 号的,缺省值;

    2,执行静态构造函数,为静态成员变量赋值;

    当使用类实例化时:

    3,先使用 = 为实例成员变量赋值,从上到下,依次赋值,没有 = 号的,缺省值;

    4,执行实例构造函数,为实例成员变量赋值;

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 初始化
     8 {
     9     class Program
    10     {
    11         private string c1 = "我是=赋值的实例变量c1";
    12         private string c2 = "我是=赋值的实例变量c2";
    13 
    14         private static string sc1 = "我是=赋值的静态变量sc1";
    15         private static string sc2 = "我是=赋值的静态变量sc2";
    16 
    17 
    18         public Program()
    19         {
    20             Console.WriteLine(sc1);
    21             Console.WriteLine(sc2);
    22 
    23             Console.WriteLine(c2);
    24             Console.WriteLine(c1);
    25             c1 = "我是构造函数赋值的实例变量c1";
    26             c2 = "我是构造函数赋值的实例变量c2";
    27             Console.WriteLine(c2);
    28             Console.WriteLine(c1);
    29         }
    30 
    31         static Program()
    32         {
    33             Console.WriteLine(sc1);
    34             Console.WriteLine(sc2);
    35             sc1 = "我是静态构造函数赋值的静态变量c1";
    36             sc2 = "我是静态构造函数赋值的静态变量c2";
    37             Console.WriteLine(sc1);
    38             Console.WriteLine(sc2);
    39         }
    40 
    41         static void Main(string[] args)
    42         {
    43             Program p = new Program();
    44             Console.Read();
    45         }
    46     }
    47 }

    输出结果:

  • 相关阅读:
    爱摘苹果的小明
    盗梦空间
    九九乘法表
    谁是最好的Coder
    画图
    黑色帽子
    a letter and a number
    运维开发面试题
    python 守护进程daemon
    kubernets 应用部署
  • 原文地址:https://www.cnblogs.com/maoshuyi/p/9744028.html
Copyright © 2011-2022 走看看