类的实例化过程(C#为例)
2011-09-17 00:40
239 查看
1.属性、方法不需要初始化,因为这些全部是指针。
2.初始化派生类的静态字段。
3.初始化派生类的非静态字段。
4.初始化基类的静态字段。
5.初始化基类的非静态字段。
6.调用基类的构造函数。
7.调用派生类的构造函数。
其实就是实例化的顺序,用语言本身或者调试都是不错的验证方法.估计其他oop也差不多这个顺序吧
外加注释:
静态字段和静态构造函数,是初次使用该类的时候初始化和运行的
所以,如果以前已经使用过某类,就不会再初始化该类的静态字段
一个更突显过程的例子:
class Program
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}
class Tester // 该Tester可帮我们跟踪实例化的过程
{
public Tester(string msg) { Console.WriteLine(msg); }
}
class A
{
static Tester AStatic = new Tester( "A static ");
Tester AInstance = new Tester( "A instance ");
public A() { AInstance = new Tester( "A constructor "); }
}
class B : A
{
static Tester BStatic = new Tester( "B static ");
Tester BInstance = new Tester( "B instance ");
public B() { BInstance = new Tester( "B constructor "); }
}
class C : B
{
static Tester CStatic = new Tester( "C static ");
Tester CInstance = new Tester( "C instance ");
public C() { CInstance = new Tester( "C constructor "); }
}
结果:
C static
C instance
B static
B instance
A static
A instance
A constructor
B constructor
C constructor
2.初始化派生类的静态字段。
3.初始化派生类的非静态字段。
4.初始化基类的静态字段。
5.初始化基类的非静态字段。
6.调用基类的构造函数。
7.调用派生类的构造函数。
其实就是实例化的顺序,用语言本身或者调试都是不错的验证方法.估计其他oop也差不多这个顺序吧
外加注释:
静态字段和静态构造函数,是初次使用该类的时候初始化和运行的
所以,如果以前已经使用过某类,就不会再初始化该类的静态字段
一个更突显过程的例子:
class Program
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine();
}
}
class Tester // 该Tester可帮我们跟踪实例化的过程
{
public Tester(string msg) { Console.WriteLine(msg); }
}
class A
{
static Tester AStatic = new Tester( "A static ");
Tester AInstance = new Tester( "A instance ");
public A() { AInstance = new Tester( "A constructor "); }
}
class B : A
{
static Tester BStatic = new Tester( "B static ");
Tester BInstance = new Tester( "B instance ");
public B() { BInstance = new Tester( "B constructor "); }
}
class C : B
{
static Tester CStatic = new Tester( "C static ");
Tester CInstance = new Tester( "C instance ");
public C() { CInstance = new Tester( "C constructor "); }
}
结果:
C static
C instance
B static
B instance
A static
A instance
A constructor
B constructor
C constructor
相关文章推荐
- C# 子类实例化过程
- c# 类实例化过程
- C#调用SQL 存储过程的两个方法
- c#存储过程基础
- C#获取存储过程的Return返回值和Output输出参数值
- 黑马程序员---对象实例化过程
- C# 数据库存储过程的讲解应用
- [翻译]使用C#创建SQL Server的存储过程(Visual Studio 2005 + SQL Server 2005)
- C#中利用存储过程(含有参数)返回dataset
- JVM总括四-类加载过程、双亲委派模型、对象实例化过程
- c#执行存储过程的简单实例
- C# 创建调用存储过程
- C#数据库编程 command对象调用存储过程
- c#中富文本编辑器Simditor带图片上传的全部过程(项目不是mvc架构)
- C# 连接数据库执行语句、存储过程
- 在存储过程中取得刚刚插入的记录的某个字段,将其输出,由C#接受OUTPUT返回参数
- Java子类实例化的过程
- C# asp.net SQL存储过程写法Insert 插入语句,及其在asp.net中的调用方法
- C#由于出现被放弃的mutex,等待过程结束
- C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息