DEMO[C#]关于类构造函数的调用顺序的一点补充[针对书C#入门经典所举范例的不足]...

通知:新增MSN群帐号 www.msdn@hotmail.com,欢迎大家进入讨论交流!(像添加好友一样地添加该群)

引用了C#入门经典中的一段代码
Page176to177(中文第3版)
原代码用断点调试配合书中的范例容易给读者产生误解,如果没有亲手实践过的朋友可能会被书中提及的顺序所迷惑,因为用断点调试的时候顺序刚好是反过来的。
先说说书中的代码:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  CA_Page176to177
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class MyBaseClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyBaseClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MyBaseClass(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class MyDerivedClass : MyBaseClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyDerivedClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MyDerivedClass(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MyDerivedClass(int i, int j)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Event1
InBlock.gif
            MyDerivedClass myObj1 = new MyDerivedClass();
InBlock.gif            Console.WriteLine();
InBlock.gif            
//Event2
InBlock.gif
            MyDerivedClass myObj2 = new MyDerivedClass(4);
InBlock.gif            Console.WriteLine();
InBlock.gif            
//Event3
InBlock.gif
            MyDerivedClass myObj3 = new MyDerivedClass(4,8);
InBlock.gif            Console.WriteLine();
InBlock.gif            Console.ReadKey();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


分别再每个构造函数处设置断点,然后按F5进行调试,针对Event1,先进入的是public MyDerivedClass()再进入public MyBaseClass(),而书中是用事件被调用的顺序进行描述的,
(书中写:事件的发生顺序为
1、System.Object.Object()

2、MyBaseClass.MyBaseClass()
3、MyDerivedClass.MyDerivedClass()

初学者可能对这个现象表示迷惑
因为程序的顺序是先3后2的。(其他的代码类似)
其实这个代码的实现过程是先上后下,就是先通过声明一个类,找到这个类的源头(先派生后基类),再从基类开始构造对象。
为了避免这样的疑惑,我还是写了这个DEMO。至少在不断点的时候你可以坚定书上的想法,等以后经验多了,就不会被迷惑了。如果你已经被迷惑了,那就赶快看看吧。
这个DEMO写得不好,原因是因为构造函数本不应该进行输出的(建议),但为了更简单明了,就直接写上Console.WriteLine()来解释迷惑。(理论上是不违反规则的)
下面就是DEMO的代码:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  CA_Page176to177
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class MyBaseClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyBaseClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"In MyBaseClass()");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MyBaseClass(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"In MyBaseClass(int i)");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class MyDerivedClass : MyBaseClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyDerivedClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"In MyDerivedClass()");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MyDerivedClass(int i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"In MyDerivedClass(int i)");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//public MyDerivedClass(int i, int j)
InBlock.gif        
//{
InBlock.gif        
//    Console.WriteLine("In MyDerivedClass(int i,int j)");
InBlock.gif        
//}
InBlock.gif
        public MyDerivedClass(int i, int j)
InBlock.gif            : 
base(i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"In MyDerivedClass(int i,int j):base(i)");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//Event1
InBlock.gif
            MyDerivedClass myObj1 = new MyDerivedClass();
InBlock.gif            Console.WriteLine();
InBlock.gif            
//Event2
InBlock.gif
            MyDerivedClass myObj2 = new MyDerivedClass(4);
InBlock.gif            Console.WriteLine();
InBlock.gif            
//Event3
InBlock.gif
            MyDerivedClass myObj3 = new MyDerivedClass(4,8);
InBlock.gif            Console.WriteLine();
InBlock.gif            Console.ReadKey();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


程序输出:

In MyBaseClass()
In MyDerivedClass()

In MyBaseClass()
In MyDerivedClass(int i)

In MyBaseClass(int i)
In MyDerivedClass(int i,int j):base(i)

转载于:https://www.cnblogs.com/volnet/archive/2006/11/10/556047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值