首页 新闻 会员 周边

解释一下代码

0
悬赏园豆:5 [已解决问题] 解决于 2012-05-01 19:17

 public PersonController()
            : this(new PersonBLL()) { }

Alvin的主页 Alvin | 小虾三级 | 园豆:828
提问于:2012-04-30 14:48
< >
分享
最佳答案
0
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             PersonBLL p = new PersonBLL();
13             PersonController pc = new PersonController(p); //初始化带参构造函数
14             //输出:初始化了带参构造函数
15             Console.WriteLine("---------------------------------");
16             PersonController pc1 = new PersonController();//初始化无参构造函数
17             //输出:初始化了带参构造函数
18             //      初始化了无参构造函数
19         }
20     }
21 
22     public class PersonController
23     {
24         private PersonBLL personBLL;
25 
26         public PersonController(PersonBLL p)
27         {
28             this.personBLL = p;
29             Console.WriteLine("初始化了带参构造函数");
30         }
31 
32         public PersonController()
33             : this(new PersonBLL())
34         {
35             Console.WriteLine("初始化了无参构造函数");
36         }
37     }
38 
39     public class PersonBLL
40     {
41     }
42 }

在调用无惨构造函数的时候,会先初始化带参构造函数,再明白了吧

收获园豆:2
xu_happy_you | 菜鸟二级 |园豆:222 | 2012-04-30 16:25
其他回答(1)
1

PersonController类里面有一个构造函数 public PersonController(PersonBLL p)

直接调用该构造函数初始化了public PersonController()

收获园豆:2
pandaren | 园豆:201 (菜鸟二级) | 2012-04-30 14:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册