在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
C#的类初始化顺序和Java以及C++类初始化顺序是不同的,曾经我被这个问题害惨了。 using System; namespace Test { public class A { private static Display d1 = new Display("基类静态变量"); private Display d2 = new Display("基类变量"); private static readonly Display d3 = new Display("基类静态readonly常量"); private readonly Display d4 = new Display("基类 readonly常量"); public A() { Console.WriteLine("基类 构造函数"); } } class B : A { private static Display d1 = new Display("子类静态变量"); private Display d2 = new Display("子类变量"); private static readonly Display d3 = new Display("子类静态readonly常量"); private readonly Display d4 = new Display("子类readonly常量"); public B() { Console.WriteLine("子类 构造函数"); } } /// <summary> /// 用于显示的类 /// </summary> class Display { public Display(string str) { Console.WriteLine(str); } } class programe { static void Main(string[] args) { B b = new B(); } } }
而对于java呢,源码如下:
package com.vision.paintTest; class A { private static Display d1 = new Display("基类静态变量"); private Display d2 = new Display("基类变量"); private static final Display d3 = new Display("基类静态final常量"); private final Display d4 = new Display("基类 final常量"); public A() { System.out.println("基类 构造函数"); } } class B extends A { private static Display d1 = new Display("子类静态变量"); private Display d2 = new Display("子类变量"); private static final Display d3 = new Display("子类静态final常量"); private final Display d4 = new Display("子类final常量"); public B() { System.out.println("子类 构造函数"); } } class Display { public Display(String str) { System.out.println(str); } } public class Mytest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub B b = new B(); } }
结果如下:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论