this关键字实现串联构造函数调用

  在一个类中如果需要实现多个自定义构造函数,通常做法是在构造函数中实现各自的业务逻辑,如果这些业务逻辑的实现并非截然不同的话,显然不符合oop编程思想,极不利于维护,当然,我们也可以通过将相同的逻辑部分封装成一个方法,但还有一种更为合理简单的方法,下面就通过this关键字来实现串联构造函数做一简单示例.
1 publicclass Person
2 {
3 publicstring Name { get; set; }
4 publicint? Age { get; set; }
5
6 public Person(string name, int? age)
7 {
8 Name = name;
9 Age = age??100;
10 if (age >100)
11 {
12 Age =80;
13 }
14 }
15
16 public Person() : this("",null) { }
17 public Person(string name) : this(name, null) { }
18 public Person(int age) : this("", age) { }
19 publicvoid Display()
20 {
21 Console.WriteLine("Name:{0},Age:{1}\n", Name, Age);
22 }
23 }
class Program
{
staticvoid Main(string[] args)
{
Person p1
=new Person();
p1.Display();
Person p2
=new Person(50);
p2.Display();
Person p3
=new Person("bobo");
p3.Display();
Person p4
=new Person("BOBO", 110);
p4.Display();

Console.ReadLine();
}

}
 这样的做法就是让一个接受参数最多的构造函数做"主构造函数",
且在主构造函数中实现必须的业务逻辑,其余的构造函数只要使用this关键字把传入的参数转发给主构造函数,并且提供必须的其它参数,这样子,我们整个类中需要我们操心的就是那个主构造函数了,其余构造函数基本上可以为空(注意:如果构造函数链中还有实现各自的逻辑,那么实际上是先执行主构造函数的代码,再执行各自逻辑),使用这种做法,真正的工作都交给了一个构造函数,类定义就会更简洁、更易维护、简化了编程任务。
 
补充下面是java和C#的this构造函数的引用对比,语法上JAVA更简洁
using System;

namespace ceshiThis
{
    /// <summary>
    /// Description of Preson.
    /// </summary>
    public class Preson
    {
        string name;
        int age;
        string address;
//---------------------------------------------------------------------------------------------------------        
//   下面是C#中用 this相互调用构造函数
//---------------------------------------------------------------------------------------------------------
        public Preson():this("",0)
        {
            System.Console.WriteLine("这是无参构造函数!");
        }
        
        public Preson(string name,int age):this(name,age,"")
        {
    
            this.name=name;
            this.age=age;
            System.Console.WriteLine("这是两个参数构造函数!");
        }
        
                
        public Preson(string name,int age,string address)
        {
        
        
            this.address=address;
            System.Console.WriteLine("这是三个参数构造函数!");
        }
        
//---------------------------------------------------------------------------------------------------------        
//   下面是java中用 this相互调用构造函数
//---------------------------------------------------------------------------------------------------------
//        public Preson()
//        {
//            System.out.println("这是无参构造函数!");
//        }
//    
//        public Preson(string name,int age)
//        {
//            this();       //必须第一个调用;
//            this.name=name;
//            this.age=age;
//            System.out.println("这是两个参数构造函数!");
//        }
//        
//                
//        public Preson(string name,int age,string address)
//        {
//            this(name,age);
//        
//            this.address=address;
//            System.out.println("这是三个参数构造函数!");
//        }
//---------------------------------------------------------------------------------------------------    
        
    }
}

 

posted @ 2011-06-08 22:28  bobozhang  阅读(422)  评论(0编辑  收藏  举报