您的位置:首页 > 其它

第十四节 构造方法、静态成员、静态构造函数、析构函数和构造重载

2012-06-20 20:59 405 查看
View Code

案例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();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐