子风.NET 进阶中......

路途多艱,唯勤是岸

 

c# 对象的创建过程

1:分配静态成员的内存空间,值类型为0,引用类型为null

2: 执行静态成员的赋值。

3:执行静态的构造函数。

4:分配成员的内存空间,值为0,引用类型为null

5:执行成员的赋值。

6:分配基类静态成员的内存空间,值为0,引用类型为null

7:执行基类静态成员的赋值。

8:执行基类的静态构造函数。

9:基类的成员的声明和赋值。

10:基类的构造函数

11:构造函数

注:静态的成员和构筑函数只运行一次。

 

代码
class Program
    {
        
static void Main(string[] args)
        {
            ClB b 
= new ClB();
            b 
= new ClB();
            Console.ReadLine();
        }
    }

    
public class ClsA
    {
        
private static readonly string strA = "A";
        
private string name;
        
public string Name
        {
            
set { this.name = value; }
            
get{return name;}
        }
        
static ClsA()
        {
            Console.WriteLine(strA);
            strA 
= "A Changed";

            Console.WriteLine(strA);
            Console.WriteLine(
"A staic constructor!");
        }
        
public ClsA()
        {
            Console.WriteLine(
"A constructor!");
        }
    }

    
public class ClB:ClsA
    {
        
private static readonly string strB = "B";
        
private int age =12;

        
static ClB()
        {
            Console.WriteLine(strB);
            strB 
= "B Changed!";
            Console.WriteLine(strB);
            Console.WriteLine(
"B static constructor!");
        }

        
public ClB()
         
        {
            Console.WriteLine(age);
            Console.WriteLine(
"B constructor!");
        }
    }

 

 

posted on 2010-08-05 10:47  子风  阅读(511)  评论(0编辑  收藏  举报

导航