关闭

C#事件小例

513人阅读 评论(0) 收藏 举报
using System;
namespace UCOM.HR{
//定义一个ComputeSalaryCallback事件代表类型.
public delegate void ComputeSalaryCallback(string EmpID,double Salary);
public class Employee{
//声明一个事件,名称为OnComputSalaryDone,事件类型是ComputeSalaryCallback.
public event ComputeSalaryCallback OnComputSalaryDone;
public Employee(){
mstrEmpID="999";
mstrEmpName="Miles";
}
public Employee(string EmpID,double BaseSalary){
mstrEmpID=EmpID;
mdbBaseSalary=BaseSalary;
}
//定义事件触发函数Computesalary(在函数ComputeSalary被调用是就会触发OnComputSalaryDone事件)
public virtual double ComputeSalary(){
ONComputeSalaryDone(mstrEmpID,mdblBaseSalary);//触发OnComputSalaryDone事件
return mdblBaseSalary;
}
public void SetEmpID(string EmpID){
mstrEmpID=EmpID;
}
public string GetEmpID(){
return mstrEmpID;
}
public double GetBaseSalary(){
return mdblBaseSalary;
}
public void SetEmpName(string EmpName){
mstrEmpName=EmpName;
}
public string GetEmpName(){
return mstrEmpName;
}
public static void SetCompanyName(string CompanyName){
mstrCompanyName=CompanyName;
}
public static string GetCompanyName(){
return mstrCompanyName;
}
private static string matrCompanyName="";
protected string mstrEmpID="";
protected string mstrEmpName="";
protected double mdblBaseSalary=0;
}
public class Sales:employee{
public Sales(string EmpID,double BaseSalary);
base(EmpID,BaseSalary){}
public voerride double ComputeSalary(){
return mdblBaseSalary+mdbBonus;
}
public double GetBonus(){
return mdblBonus;
}
public void SetBonus(double Bonus){
mdblBonus=Bonus;
}
private double mdblBonus=0;
}
public class HumanResource{
//定义两个事件处理函数.
public static void SalaryHandler(string EmpID,double Salary){
Console.WriteLine("员工:员工代号={0},薪金={1}",EmpID,Salary);
}
public static void SalaryHandler2(string EmpID,double Salary){
Console.WriteLine("2员工:员工代号={0},薪金={1}",EmpID,Salary);
}
public static int Main(){
Employee e1= new Employee("001",5000);
//使用"+="将事件与事件处理函数连接起来.
e1.OnComputeSalaryDone += new ConputeSalaryCallback(SalaryHandler2);
e1.OnComputeSalaryDone += new ComputeSalaryCallback(SalaryHandler);
//调用事件触发函数.
e1.ComputeSalary();
return 0;
}
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57596次
    • 积分:1046
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:10篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论