构造函数
来源:互联网 发布:eclipse 无法访问网络 编辑:程序博客网 时间:2024/05/01 08:12
• 定义:构造函数是类中一种特别类型的方法,每次创建类的实例时都将调用它。• • • using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 构造函数演示{ classProgram { staticvoid Main(string[] args) { classAA1 = new classA(); A1.Count = 11; Console.WriteLine("输出结果是:{0}",A1.Count);//通过属性赋值传递参数 classAA2 = new classA(10);//通过构造¨函数传递参数 Console.WriteLine(A2.Count); Console.ReadKey(); } } publicclassclassA { publicclassA ()//空构造函数 { } publicclassA(int count)//构造函数重载 { this.count= count; } intcount; publicint Count { get{ return count; } set { count =value; } }}publicstructclassA1//结构中不能包含无参函数 { publicclassA1(int count)//构造函数重载 { this.count= count; } intcount; publicint Count { get{ return count; } set{ count = value; } } }}