C# 类初始化顺序(转)

C#的类初始化顺序和Java以及C++类初始化顺序是不同的,曾经我被这个问题害惨了。
对于C#和Java,其共同点都是先变量后构造函数,先静态后普通 
区别在于,C#是子类变量->父类变量->父类构造函数->子类构造函数,才轮到构造函数,但是Java的是父类变量->父类构造函数->子类变量->子类构造函数.

 现在给出测试代码,用事实说话:

  1. using System; 
  2.  
  3. namespace Test 
  4.     public class A 
  5.     { 
  6.  
  7.         private static Display d1 = new Display("基类静态变量"); 
  8.         private Display d2 = new Display("基类变量"); 
  9.         private static readonly Display d3 = new Display("基类静态readonly常量"); 
  10.         private readonly Display d4 = new Display("基类 readonly常量"); 
  11.  
  12.         public A() 
  13.         { 
  14.             Console.WriteLine("基类 构造函数"); 
  15.         } 
  16.  
  17.     } 
  18.  
  19.     class B : A 
  20.     { 
  21.         private static Display d1 = new Display("子类静态变量"); 
  22.         private Display d2 = new Display("子类变量"); 
  23.         private static readonly Display d3 = new Display("子类静态readonly常量"); 
  24.         private readonly Display d4 = new Display("子类readonly常量"); 
  25.  
  26.  
  27.         public B() 
  28.         { 
  29.             Console.WriteLine("子类 构造函数"); 
  30.         } 
  31.   
  32.     } 
  33.  
  34.     /// <summary> 
  35.     /// 用于显示的类 
  36.     /// </summary> 
  37.     class Display 
  38.     { 
  39.         public Display(string str) 
  40.         { 
  41.             Console.WriteLine(str); 
  42.         } 
  43.     } 
  44.  
  45.     class programe 
  46.     { 
  47.         static void Main(string[] args) 
  48.         { 
  49.             B b = new B(); 
  50.  
  51.         } 
  52.     } 
  53.  
  1. 子类静态变量 
  2. 子类静态readonly常量 
  3. 子类变量 
  4. 子类readonly常量 
  5. 基类静态变量 
  6. 基类静态readonly常量 
  7. 基类变量 
  8. 基类 readonly常量 
  9. 基类 构造函数 
  10. 子类 构造函数 

而对于java呢,源码如下:

 

  1. package com.vision.paintTest; 
  2.  
  3. class A { 
  4.  
  5.     private static Display d1 = new Display("基类静态变量"); 
  6.     private Display d2 = new Display("基类变量"); 
  7.     private static final Display d3 = new Display("基类静态final常量"); 
  8.     private final Display d4 = new Display("基类 final常量"); 
  9.  
  10.     public A() { 
  11.         System.out.println("基类 构造函数"); 
  12.     } 
  13.  
  14.  
  15. class B extends A { 
  16.     private static Display d1 = new Display("子类静态变量"); 
  17.     private Display d2 = new Display("子类变量"); 
  18.     private static final Display d3 = new Display("子类静态final常量"); 
  19.     private final Display d4 = new Display("子类final常量"); 
  20.  
  21.     public B() { 
  22.         System.out.println("子类 构造函数"); 
  23.     } 
  24.  
  25.  
  26. class Display { 
  27.     public Display(String str) { 
  28.         System.out.println(str); 
  29.     } 
  30.  
  31. public class Mytest { 
  32.  
  33.     /** 
  34.      * @param args 
  35.      */ 
  36.     public static void main(String[] args) { 
  37.         // TODO Auto-generated method stub 
  38.         B b = new B(); 
  39.     } 
  40.  

结果如下:

  1. 基类静态变量 
  2. 基类静态final常量 
  3. 子类静态变量 
  4. 子类静态final常量 
  5. 基类变量 
  6. 基类 final常量 
  7. 基类 构造函数 
  8. 子类变量 
  9. 子类final常量 
  10. 子类 构造函数 

转载于:https://www.cnblogs.com/naariah/articles/2119339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值