第十四节 构造方法、静态成员、静态构造函数、析构函数和构造重载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例1:构造方法 class Student { //构造方法是一个与类名相同而且无返回值的方法 //构造方法的作用初始化成员变量 //定义方式: //访问修饰符 构造方法名(参数列表) //{ // //方法主体 //} public Student(string name,int age ,string hobby,int popularity) { //this:当前实例(new的对象是谁就是谁的实例) this.Name = name; this.Age = age; this.Hobby = hobby; this.Popularity = popularity; } private string name; public string Name { set{name=value;} get{return name;} } private int age; public int Age { set {age=value; } get { return age;} } private string hobby; public string Hobby { set{hobby=value;} get{return hobby;} } private int popularity; public int Popularity { set { popularity = value; } get { return popularity; } } public void Att() { Console.WriteLine("姓名:{0}\t年龄:{1}\t爱好:{2}\t受欢迎度:{3}",name,age,hobby,popularity); } } —————————————————————————————————————————————————— 案例1.1 class Program { static void Main(string[] args) { Student zhang = new Student("张三", 25, "唱唱歌", 100); zhang.Att(); Student zhou = new Student("周杰杰", 29, "打打双结棍", 110); zhou.Att(); Console.ReadKey(); } } ———————————————————————————————————————————————————————————————————————————————————————————————————————————————— 案例2 静态成员 class StaticExample { //定义静态变量,静态成员属于类不属于实例,静态成员通过类名调用(类名.静态成员) public static int counter; public StaticExample() { counter++; } public static void Display() { Console.WriteLine("StaticExample类创建了{0}个类",counter); } } ———————————————————————————————————————————————————————— 案例2.1 class Program { static void Main(string[] args) { StaticExample s1 = new StaticExample(); StaticExample s2 = new StaticExample(); StaticExample s3 = new StaticExample(); StaticExample s4 = new StaticExample(); StaticExample.Display(); Console.ReadKey(); } } ——————————————————————————————————————————————————————————————————————————————————————— 案例3 静态构造函数 class Bus { static int count; //在构造函数前加static 关键字为静态构造函数(注:静态构造函数前面不能加访问修饰符) //静态构造函数的作用:初始化静态成员,静态构造函数永远只能被执行一次 //静态构造函数必须是无参数 static Bus() { count = 10; Console.WriteLine("Bus类的静态构造函数被调用...."); } public static void Drive() { Console.WriteLine("公共汽车启动了........"); } } ———————————————————————————————————————————————— 案例3.1 class Program { static void Main(string[] args) { Bus.Drive(); Bus.Drive(); Bus.Drive(); Bus.Drive(); Console.ReadKey(); } } —————————————————————————————————————————————————————————————————————————————————— 案例4 static class TempratureCovert { public static double CtoF(double c)//c:摄氏 f:华氏 { return (c * 9 / 5) + 32; } public static double FtoC(double f) { return (f - 32) * 5 / 9; } } —————————————————————————————————————————————————— 案例4.1 static void Main(string[] args) { Console.WriteLine("请输入(1,2)"); Console.WriteLine("1.摄氏转华氏"); Console.WriteLine("2.华氏转摄氏"); string operation = Console.ReadLine(); double c; double f; switch (operation) { case "1": Console.Write("请输入摄氏温度:"); c = double.Parse(Console.ReadLine()); f=TempratureCovert.CtoF(c); Console.WriteLine("摄氏温度{0}转华氏温度后{1}", c, f); break; case "2": Console.Write("请输入华氏温度:"); f= double.Parse(Console.ReadLine()); c = TempratureCovert.FtoC(f); Console.WriteLine("华氏温度{0}转摄氏温度后{1}", f, c); break; default: Console.WriteLine("无效的选择........."); break; } Console.ReadKey(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
案例5析构函数 class Destructor { //对象销毁,调用析构函数 int num1, num2, total; public Destructor(int num1,int num2) { this.num1 = num1; this.num2 = num2; } public void Add() { total = num1 + num2; Console.WriteLine("{0}+{1}={2}", num1, num2, total); } //定义析构函数 ~Destructor()// ~ 鼻音 { Console.WriteLine("Destructor析构被调用............"); } } —————————————————————————————————————————————— 案例5.1 class Program { static void Main(string[] args) { //按ctrl+F5 调试 Destructor d = new Destructor(10,5); d.Add(); } } ———————————————————————————————————————————————————————————————————————————————————————————————————————————— 案例6 构造重载 class Employee { public Employee(string zw,decimal xs,string bm) { this.Zw = zw; this.Xs = xs; this.Bm = bm; } public Employee(string xm,string zw, decimal xs, string bm) : this(zw, xs, bm) //: 等于调用前面的 { this.Xm = xm; } public void Display() { Console.WriteLine("姓名:{0},职位:{1},薪水:{2},部门:{3}", xm, zw, xs, bm); } private string xm="张三"; public string Xm { set { xm = value; } get {return xm; } } private string zw="职员"; public string Zw { set { zw = value; } get { return zw; } } private decimal xs; public decimal Xs { set { if (zw.Equals("职员")) { if (value < 3000) { xs = value; } else { xs = 3000; } } else { xs = value; } } get { return xs; } } private string bm; public string Bm { set { bm = value; } get { return bm; } } public string zwjs(string f1) { return f1; } } ———————————————————————————————————————————————————— 案例6.1 class Program { static void Main(string[] args) { Employee emp1 = new Employee("软件工程师", 5000, "研发部"); emp1.Display(); Employee emp2 = new Employee("李四", "工程师", 2500, "销售部"); emp2.Display(); Console.ReadKey(); } } —————————————————————————————————————————————————————————————————————————————————————————————————————— 案例7 命名空间导入以及类的调用 using System; using System.Collections.Generic; using System.Text; //导入命名空间 using adminDepartment; namespace e7 { class Program { static void Main(string[] args) { manager m1 = new manager();//就能调用或者不导入写 adminDepartment.manager Console.ReadKey(); } } } namespace adminDepartment { class manager { public void dis() { Console.WriteLine("我是管理部门的经理"); } } } namespace ITDepartment { class manager { public void dis() { Console.WriteLine("我是IT部门的经理"); } } } ———————————————————————————————————————————————————————————————————————————————————————————————————————————— 案例8.1 namespace Sam { class Montor { public void ListModel() { Console.WriteLine("Sam的型号 15,16,19"); } } } —————————————————————————————— 案例8.2 namespace Sony { class Monitor { public void ListModel() { Console.WriteLine("Sony显示器的型号:12,54,63"); } } } —————————————————————————————————————————————— 案例8.3 namespace e8 { /// <summary> /// 明明空间 /// </summary> class Program { static void Main(string[] args) { Sony.Monitor sm = new Sony.Monitor(); sm.ListModel(); Sam.Montor mm = new Sam.Montor(); mm.ListModel(); Console.ReadKey(); } } }