总结:C#语法系列——构造函数
2010-10-27 21:10
323 查看
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class parent { public static string str = "parent类的static静态字段"; //静态构造函数前不能有访问限制符。 //在 类被实例化、访问类的静态成员 时会自动调用静态构造函数,并且只调用 1次! static parent() { Console.WriteLine("0.父类parent的无参数static构造函数"); } public static void Fun() { Console.WriteLine("父类parent的Fun静态方法"); } public parent() { Console.WriteLine("1.父类parent的无参数构造函数"); } public parent(int I) { Console.WriteLine("2.父类parent的(int I)参数构造函数"); } //若父类中有带参数的构造函数,系统不会生成不带参数的构造函数,此时系统会报错 //若父类中没有带参数的构造函数,系统会生成不带参数的构造函数,此时系统不会报错 //以后书写最好的方法:父类中都要书写一个无参数的构造函数,不管无参构造函数中有没有操作 } class son : parent { public son() { Console.WriteLine("3.子类son的无参数构造函数"); } public son(int a) { Console.WriteLine("4.子类son的(int a)参数构造函数"); } //自动先调用冒号后面的构造函数。在这里this(i)是son类的public son(int a) public son(string str, int i) : this(i) { Console.WriteLine("5.子类son的(string str, int i) : this(i)参数构造函数"); } //自动先调用冒号后面的构造函数。在这里base(i)是parent类的public parent(int I) public son(string str, string str2, int i) : base(i) { Console.WriteLine("6.子类son的(string str, string str2, int i) : base(i)构造函数"); } } class construct { static void Main() { Console.WriteLine(parent.str); parent.Fun(); Console.WriteLine(); son son1 = new son(8); Console.WriteLine(); son son2 = new son("dd", 8);//先调用父类的构造函数,在调用子类的构造函数 Console.WriteLine(); son son3 = new son("dd", "ff", 8); Console.ReadLine(); } //还有一种通过 tprivate修饰的私有构造函数 的类,其特点是限制了类不能被实例化,类中都是static静态的成员。 } }
相关文章推荐
- 深入理解C# 3.x的新特性系列总结
- C#知识点总结系列:C# 数据结构
- C#部分语法总结
- 【C#小知识】C#中一些易混淆概念总结(二)--------构造函数,this关键字,部分类,枚举
- 用于 C# 的 SQL 基本语法总结
- C# 3.x的新特性系列总结
- C#知识点总结系列:4、C#中Monitor和Lock以及区别
- C#基础系列问题四数据结构常用术语总结
- c#学习之与c++的语法区别总结
- C#知识点总结系列:C# 数据结构
- C#经典语法总结(二)属性和字段
- C#中一些易混淆概念总结(二)--------构造函数,this关键字,部分类,枚举
- C#语法之---多线程、并发与并行概念总结
- C#知识点总结系列:1、C#中Hashtable、Dictionary详解以及写入和读取对比
- .Net C#语法 构造函数中this和base
- C# 语法之类和结构体的区别(个人总结)
- C#基础系列——语法
- c#语法糖代码——继承中构造函数的问题,this&base比较
- C#2.0类和对象学习系列之构造函数
- C#总结(二)—构造函数和析构函数