了解构造函数

私有构造函数

如果一个类里面有私有构造函数,则该类不能以这种构造函数来初始化,通常只能调用它的静态方法。单例模式中比较常用。

如果一个类里面既有私有构造函数,也有公有构造函数,则该类可以被初始化,但仅仅是按照公有构造函数的方式。所以说,类里面含有私有构造函数,所以类不能被初始化是不严谨的。

如果类中的所有方法都是静态的,可考虑使整个类成为静态的。

public class father 
   { 
       private father() 
       { 
           Console.WriteLine("father"); 
       }

       public father(string a) 
       { 
           Console.WriteLine("father"); 
       } 
   }

father f = new father();//can't be initialled 
father f1 = new father("ww");//ok

 protected构造函数

在初始化方面和私有构造函数相同。

public class father 
   { 
       protected father() 
       { 
           Console.WriteLine("father"); 
       }

       public father(string a) 
       { 
           Console.WriteLine("father"); 
       } 
   }

father f = new father();//can't be initialled 
father f1 = new father("ww");//ok

 继承性

当父类没有默认构造函数,或者默认构造函数被重写。子类继承的时候,要相应的调用父类的被重写的构造函数,不然编译会出错。

public class father 
   { 
       public father(string a) 
       { 
           Console.WriteLine("father"); 
       } 
   }

   public class son : father 
   { 
       public son(string a):base(a)//必须这样写。 
       { 
           Console.WriteLine("father"); 
       } 
   }

当父类的构造函数是私有的,子类想要“继承”该构造函数但却不能访问(构造函数不能被继承,此处的意思为子类初始化的时候,必定要调用到父类的构造函数),所以子类就没法实现继承了。但子类可以继承父类中的公有的构造函数。

所以说,对于类中含有私有构造函数,该类不能被继承的说法是不严谨的。严谨的说法是,当类中仅有一个私有构造函数,则该类不能被继承。若一定要继承该类,指定继承父类的一个公有构造函数,则还是可以被继承的。

public class father 
   { 
       private father()//私有构造函数 
       { 
       }

       public father(string a) 
       { 
           Console.WriteLine("father"); 
       } 
   }

   public class son : father 
   { 
       public son():base("ssss") //不指定会导致编译出错 
       { }

       public son(string a):base(a) 
       { 
           Console.WriteLine("father"); 
       } 
   }

 对于类中含有protected构造函数,该类的继承性不受影响。

public class father 
   { 
       protected father() 
       { 
       }

   }

   public class son : father 
   { 
   }

 静态构造函数

静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。

public class father 
    { 
        static father() 
        { 
            Console.WriteLine("father"); 
        }

    }

father f = new father();//print 
father f1 = new father();//no print 静态构造函数已经被调用过一次了,不会再次调用。

静态构造函数函数的继承性体现在如下,当你实例化子类的时候,接下来发生的事情如下:

子类的静态构造函数=》父类的静态构造函数=》父类的构造函数=》子类的构造函数。

可以这么去理解,最新调用的是子类的静态构造函数,然后接下来是子类的构造函数,但是子类的构造函数运行前要先调用父类的构造函数,于是,程序去运行父类的构造函数,但是只要你去调用父类,肯定会先运行父类的静态构造函数,除非静态构造函数已经被运行过一次。所以初始运行的时候就会有如上的序列。

当再次实例化子类,则只剩下:父类的构造函数=》子类的构造函数。

看如下代码:

 public class father 
   { 
       static father() 
       { 
           Console.WriteLine("Sfather"); 
       }

       public father() 
       { 
           Console.WriteLine("father"); 
       }

   }

   public class son : father 
   { 
       static son() 
       { 
           Console.WriteLine("Sson"); 
       }

       public son() 
       { 
           Console.WriteLine("son"); 
       } 
   }

 son s=new son(); 
son s1 =new son();

 运行结果为:

Sson 
Sfather 
father 
son 
-- 
father 
son

 静态类

类可以声明为 static 的,以指示它仅包含静态成员。不能使用 new关键字创建静态类的实例。静态类在加载包含该类的程序或命名空间时由 .NET Framework 公共语言运行库 (CLR) 自动加载。

使用静态类

它们仅包含静态成员和静态方法。

它们不能被实例化。

不能被继承。

它们不能包含实例构造函数。但是它能有静态构造函数。

 拷贝构造函数

与有些语言不同,C# 不提供复制构造函数。如果您创建了新的对象并希望从现有对象复制值,您必须自行编写适当的方法。

class Person 

    private string name; 
    private int age;

    // Copy constructor. 
    public Person(Person previousPerson) 
    { 
        name = previousPerson.name; 
        age = previousPerson.age; 
    }

    // Instance constructor. 
    public Person(string name, int age) 
    { 
        this.name = name; 
        this.age = age; 
    }

    // Get accessor. 
    public string Details 
    { 
        get 
        { 
            return name + " is " + age.ToString(); 
        } 
    } 
}

class TestPerson 

    static void Main() 
    { 
        // Create a new person object. 
        Person person1 = new Person("George", 40);

        // Create another new object, copying person1. 
        Person person2 = new Person(person1); 
        System.Console.WriteLine(person2.Details); 
    } 
}















本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/576533,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值