zoukankan      html  css  js  c++  java
  • 构造函数和析构函数的调用顺序

    using System;

    public class SamplesArray
    {
        
    public static void Main()
        {
            C c 
    = new C();
            S.C(); 
        }
    }
    public static class S
    {
        
    static S()   //静态构造函数中不能有访问修饰符,默认为私有构造函数,这也是私有构造函数的用法。
        {
            Console.WriteLine(
    "静态类S!");
        }
        
    public static void C()
        {
            Console.WriteLine(
    "静态类S的静态方法!");
        }
        
    /*~S()   //静态类不能包含析构函数
        {
            Console.WriteLine("静态类S的析构函数!");
        }
    */
    }

    public class A
    {
        
    static A()
        {
            Console.WriteLine(
    "A类的静态构造函数!");
        }
        
    public A()
        {
            Console.WriteLine(
    "A类的实例构造函数!");
        }
        
    ~A()
        {
            Console.WriteLine(
    "A类的析构函数!");
        }
    }
    public class B:A
    {
        
    static B()
        {
            Console.WriteLine(
    "B:A类的静态构造函数!");
        }
        
    public B()
        {
            Console.WriteLine(
    "B:A类的实例构造函数!");
        }
        
    ~B()
        {
            Console.WriteLine(
    "B:A类的析构函数!");
        }
    }
    public class C:B
    {
        
    static C()
        {
            Console.WriteLine(
    "C:B类的静态构造函数!");
        }
        
    public C()
        {
            Console.WriteLine(
    "C:B类的实例构造函数!");
        }
        
    ~C()
        {
            Console.WriteLine(
    "C:B类的析构函数!");
        }
    }

    C:B类的静态构造函数!

    B:A类的静态构造函数!

    A类的静态构造函数!

    A类的实例构造函数!

    B:A类的实例构造函数!

    C:B类的实例构造函数!

    静态类S!//

    静态类S的静态方法!

    C:B类的析构函数!

    B:A类的析构函数!

    A类的析构函数!

  • 相关阅读:
    Maven关于web.xml中Servlet和Servlet映射的问题
    intellij idea的Maven项目运行报程序包找不到的错误
    修改Maven项目默认JDK版本
    刷题15. 3Sum
    刷题11. Container With Most Water
    刷题10. Regular Expression Matching
    刷题5. Longest Palindromic Substring
    刷题4. Median of Two Sorted Arrays
    刷题3. Longest Substring Without Repeating Characters
    刷题2. Add Two Numbers
  • 原文地址:https://www.cnblogs.com/answercard/p/1842934.html
Copyright © 2011-2022 走看看