您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 属性、索引器、委托和事件
属性、索引器、委托和事件23目标理解属性及其不同的类型、实现理解和使用索引器实现委托定义和触发事件41-1属性简介classEmployee{privatestaticstring_name;privatestaticstring_id;staticvoidMain(string[]args){_name=Console.ReadLine();_id=Console.ReadLine();}}•直接访问字段•不经验证51-1属性简介classEmployee{privatestaticstring_name;privatestaticstring_id;publicvoidSetId(value){//验证输入长度小于2if(_id.Length2)_id=value;}publicstringGetId(){return_id;}}方法SetId(Value)和GetId()分别读取和写入职员ID…•Employeeemp;•emp.SetId(A1);•stringDepartment=emp.Get()…每次都调用GetId()和SetId()方法会很繁琐属性61-1属性简介classEmployee{privatestaticstring_name;privatestaticstring_id;publicstringId{get{return_id;}set{//验证输入长度小于2if(_id.Length2)_id=value;}}}读取ID时调用将值赋给ID时调用71-2属性类型[访问修饰符]数据类型属性名{get{};set{};}读/写属性可以赋值和检索值81-2属性类型[访问修饰符]数据类型属性名{get{};}只读属性只能检索值91-2属性类型[访问修饰符]数据类型属性名{set{};}只写属性只能赋值101-2属性类型[访问修饰符]static数据类型属性名{get{};set{};}静态属性应用于整个类而不是类的实例只能访问类的静态成员11classSavingsAccount{//类字段用于存储帐号、余额和已获利息privateint_accountNumber;privatedouble_balance;privatedouble_interestEarned;//利率是静态的,因为所有帐户获得的利息相同privatestaticdouble_interestRate;//构造函数初始化类成员publicSavingsAccount(intaccountNumber,doublebalance){this._accountNumber=accountNumber;this._balance=balance;}//只读AccountNumber属性publicintAccountNumber{get{return_accountNumber;}}演示……1-3定义和调用属性只读属性121-3定义和调用属性staticvoidMain(string[]args){//创建SavingsAccount的对象SavingsAccountobjSavingsAccount=newSavingsAccount(12345,5000);;Console.WriteLine(输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine());SavingsAccount.InterestRate=Int64.Parse(Console.ReadLine());objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:{0},objSavingsAccount.InterestEarned);}将设置InterestEarned属性131-3定义和调用属性publicdoubleInterestEarned{get{return_interestEarned;}set{//验证数据if(value0.0){Console.WriteLine(“利息不能为负数);return;}_interestEarned=value;}}将设置InterestEarned属性14staticvoidMain(string[]args){//创建SavingsAccount的对象SavingsAccountobjSavingsAccount=newSavingsAccount(12345,5000);;Console.WriteLine(输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine());SavingsAccount.InterestRate=Int64.Parse(Console.ReadLine());objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:{0},objSavingsAccount.InterestEarned);}将设置InterestRate属性publicstaticdoubleInterestRate{get{return_interestRate;}set{//验证数据if(value0.0){Console.WriteLine(“利率不能为负数);return;}else{_interestRate=value/100;}}1-3定义和调用属性15staticvoidMain(string[]args){//创建SavingsAccount的对象SavingsAccountobjSavingsAccount=newSavingsAccount(12345,5000);;Console.WriteLine(“输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine());SavingsAccount.InterestRate=Int64.Parse(Console.ReadLine());objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:{0},objSavingsAccount.InterestEarned);}将检索Balance和InterestRate属性publicdoubleBalance{get{if(_balance0)Console.WriteLine(没有可用余额);return_balance;}}1-3定义和调用属性162索引器[访问修饰符]数据类型this[数据类型标识符]{get{};set{};}语法172-1定义和调用索引器演示……classPhoto{string_title;publicPhoto(stringtitle){this._title=title;}publicstringTitle{get{return_title;}}}以Title属性表示照片将照片存放于数组photos中classAlbum{//该数组用于存放照片Photo[]photos;publicAlbum(intcapacity){photos=newPhoto[capacity];}18定义和调用索引器4-2publicPhotothis[intindex]{get{//验证索引范围if(index0||index=photos.Length){Console.WriteLine(索引无效);//使用null指示失败returnnull;}//对于有效索引,返回请求的照片returnphotos[index];}set{if(index0||index=photos.Length){Console.WriteLine(索引无效);return;}photos[index]=value;}}带有int参数的Photo索引器读/写索引器192-1定义和调用索引器publicPhotothis[stringtitle]{get{//遍历数组中的所有照片foreach(Photopinphotos){//将照片中的标题与索引器参数进行比较if(p.Title==title)returnp;}Console.WriteLine(未找到);//使用null指示失败returnnull;}}带有string参数的Photo索引器只读索引器202-1定义和调用索引器staticvoidMain(string[]args){//创建一个容量为3的相册Albumfriends=newAlbum(3);//创建3张照片Photofirst=newPhoto(Jenn);Photosecond=newPhoto(Smith);Photothird=newPhoto(Mark);//向相册加载照片friends[0]=first;friends[1]=second;friends[2]=third;//按索引检索Photoobj1Photo=friends[2];Console.WriteLine(obj1Photo.Title);//按名称检索Photoobj2Photo=friends[Jenn];Console.WriteLine(obj2Photo.Title);}213委托Multiply(int,int){….}Divide(int,int){….}在运行时确定调用哪种方法委托和方法必须具有相同的签名---publicdelegateCall(intnum1,intnum2);---223-1定义委托[访问修饰符]delegate返回类型委托名();语法classDelegates{publicdelegateintCall(intnum1,intnum2);classMath{publicintMultiply(intnum1,intnum2){//实现}publicintDivide(intnum1,intnum2){//实现}}classTestDelegates{staticvoidMain(){CallobjCall;MathobjMath=newMath();objCall=newCall(math.Multiply);}}将方法与委托关联起来23classDelegates{//委托定义publicdelegateintCall(intnum1,intnum2);classMath{//乘法方法publicintMultiply(intnum1,intnum2){returnnum1*num2;}//除法方法publicintDivide(intnum1,intnum2){returnnum1/num2;}}staticvoidMain(string[]args){//委托的对象CallobjCall;//Math类的对象MathobjMath=newMath();//将方法与委托关联起来objCall=newCall(objMath.Multiply);//将委托实例化result=objCall(5,3);System.Console.WriteLine(结果为{0},result);}将方法与委托关联起来3-1定义委托244事件抢答者宣布人抢答者“请听题~”集中注意力聆听其他人事件源事件的发布者事件的订阅人未订阅该事件
本文标题:属性、索引器、委托和事件
链接地址:https://www.777doc.com/doc-3360275 .html