《属性索引.ppt》由会员分享,可在线阅读,更多相关《属性索引.ppt(32页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、第六章第六章属性、索引器、委托和事件回顾 继承是获得现有类的功能的过程 创建新类所根据的基础类称为基类或父类,新建的类则称为派生类或子类base 关键字用于从派生类中访问基类成员 override 关键字用于修改方法、属性或索引器。new 访问修饰符用于显式隐藏继承自基类的成员抽象类是指至少包含一个抽象成员(尚未实现的方法)的类。抽象类不能实例化 重写方法就是修改基类中方法的实现。virtual 关键字用于修改方法的声明 显式接口实现是用于在名称不明确的情况下确定成员函数实现的是哪一个接口 2目标理解属性及其不同的类型、实现理解和使用索引器实现委托定义和触发事件3属性简介 3-1class E
2、mployeeprivate static string _name;private static string _id;static void Main(string args)_name=Console.ReadLine();_id=Console.ReadLine();直接访问字段 不经验证 4属性简介 3-2class Employee private static string _name;private static string _id;public void SetId(int value)/验证输入长度小于 2if(_id.Length 2)_id=value;public
3、string GetId()return _id;方法 SetId(Value)和 GetId()分别读取和写入职员 ID Employee emp;emp.SetId(A1);string Department=emp.GetId()每次都调用 GetId()和 SetId()方法会很繁琐属性5属性简介 3-3class Employee private static string _name;private static string _id;public string Id get return _id;set/验证输入长度小于 2if(_id.Length 2)_id=value;读取
4、 ID 时调用 将值赋给 ID 时调用 6属性类型 4-1 access_modifer datatype PropertyName get;set;读/写属性可以赋值和检索值7属性类型 4-2访问修饰符 数据类型 属性名get;只读属性只能检索值8属性类型 4-3访问修饰符 数据类型 属性名set;只写属性只能赋值9属性类型 4-4访问修饰符static 数据类型 属性名get;set;静态属性应用于整个类而不是类的实例只能访问类的静态成员10class SavingsAccount/类字段用于存储帐号、余额和已获利息private int _accountNumber;private do
5、uble _balance;private double _interestEarned;/利率是静态的,因为所有帐户获得的利息相同private static double _interestRate;/构造函数初始化类成员public SavingsAccount(int accountNumber,double balance)this._accountNumber=accountNumber;this._balance =balance;/只读 AccountNumber 属性public int AccountNumbergetreturn _accountNumber;演示:示例
6、1定义和调用属性 4-1 只读属性 11定义和调用属性 4-2static void Main(string args)/创建 SavingsAccount 的对象SavingsAccount objSavingsAccount=new SavingsAccount(12345,5000);Console.WriteLine(输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine();SavingsAccount.InterestRate=Int64.Parse(Console.ReadLi
7、ne();objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:0,objSavingsAccount.InterestEarned);将设置 InterestEarned 属性12定义和调用属性 4-2public double InterestEarnedgetreturn _interestEarned;set/验证数据if(value 0.0)Console.WriteLine(“利息 不能为负数);return;
8、_interestEarned=value;13定义和调用属性 4-3static void Main(string args)/创建 SavingsAccount 的对象SavingsAccount objSavingsAccount=new SavingsAccount(12345,5000);Console.WriteLine(输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine();SavingsAccount.InterestRate=Int64.Parse(Console.Re
9、adLine();objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:0,objSavingsAccount.InterestEarned);将设置 InterestRate 属性public static double InterestRategetreturn _interestRate;set/验证数据if(value 0.0)Console.WriteLine(“利率不能为负数);return;else_inte
10、restRate=value/100;14定义和调用属性 4-4static void Main(string args)/创建 SavingsAccount 的对象SavingsAccount objSavingsAccount=new SavingsAccount(12345,5000);Console.WriteLine(“输入到现在为止已获得的利息和利率);objSavingsAccount.InterestEarned=Int64.Parse(Console.ReadLine();SavingsAccount.InterestRate=Int64.Parse(Console.Read
11、Line();objSavingsAccount.InterestEarned+=objSavingsAccount.Balance*SavingsAccount.InterestRate;Console.WriteLine(获得的总利息为:0,objSavingsAccount.InterestEarned);将检索 Balance 和 InterestRate 属性public double Balancegetif(_balance 0)Console.WriteLine(没有可用余额);return _balance;15索引器access_modifertype thisdataty
12、pe identifierget;set;语法16定义和调用索引器 4-1 演示:示例 2class Photostring _title;public Photo(string title)this._title=title;public string Titlegetreturn _title;以 Title 属性表示照片17定义和调用索引器 4-1 演示:示例 2将照片存放于数组 photos 中class Album/该数组用于存放照片Photo photos;public Album(int capacity)photos=new Photocapacity;18定义和调用索引器定义
13、和调用索引器 4-3public Photo thisstring titleget/遍历数组中的所有照片foreach(Photo p in photos)/将照片中的标题与索引器参数进行比较 if(p.Title=title)return p;Console.WriteLine(未找到);/使用 null 指示失败return null;带有 string 参数的 Photo 索引器只读索引器19定义和调用索引器 4-2public Photo thisint indexget/验证索引范围if(index=photos.Length)Console.WriteLine(索引无效);/使用
14、 null 指示失败return null;/对于有效索引,返回请求的照片return photosindex;setif(index=photos.Length)Console.WriteLine(索引无效);return;photosindex=value;带有 int 参数的 Photo 索引器读/写索引器20定义和调用索引器 4-4static void Main(string args)/创建一个容量为 3 的相册Album friends=new Album(3);/创建 3 张照片Photo first=new Photo(Jenn);Photo second=new Photo
15、(Smith);Photo third=new Photo(Mark);/向相册加载照片friends0=first;friends1=second;friends2=third;/按索引检索Photo obj1Photo=friends2;Console.WriteLine(obj1Photo.Title);/按名称检索Photo obj2Photo=friendsJenn;Console.WriteLine(obj2Photo.Title);21委托Multiply(int,int).Divide(int,int).可以引用任何方法,将在运行时决定委托和方法必须具有相同的签名-public
16、 delegate Call(int num1,int num2);-22演示:示例 3定义委托 2-1 访问修饰符 delegate 返回类型 委托名();语法class Delegates public delegate int Call(int num1,int num2);class Math public int Multiply(int num1,int num2)/实现 public int Divide(int num1,int num2)/实现 class TestDelegates static void Main()Call objCall;Math objMath=ne
17、w Math();objCall=new Call(math.Multiply);将方法与委托关联起来23定义委托 2-2class Delegates/委托定义public delegate int Call(int num1,int num2);class Math/乘法方法public int Multiply(int num1,int num2)return num1*num2;/除法方法public int Divide(int num1,int num2)return num1/num2;static void Main(string args)/委托的对象Call objCall
18、;/Math 类的对象Math objMath=new Math();/将方法与委托关联起来objCall=new Call(objMath.Multiply);/将委托实例化result=objCall(5,3);System.Console.WriteLine(结果为 0,result);将方法与委托关联起来24事件抢答者 宣布人宣布人抢答者“请听题”集中注意力聆听其他人事件源事件的发布者事件的订阅人未订阅该事件 定义事件 为对象订阅该事件 将发生的事件通知给订阅人25定义事件访问修饰符 event 委托名 事件名;语法public delegate void delegateMe();p
19、rivate event delegateMe eventMe;26订阅事件 eventMe+=new delegateMe(objA.Method);eventMe+=new delegateMe(objB.Method);27通知订阅对象 if(condition)eventMe();调用订阅特定事件的对象的所有委托28演示:示例 4示例class TestEventsSTAThreadstatic void Main(string args)/委托的对象 Delegate objDelegate=new Delegate();/ClassA 的对象 ClassA objClassA=ne
20、w ClassA();/ClassB 的对象 ClassB objClassB=new ClassB();/订阅该事件 objDelegate.NotifyEveryOne+=new Delegate.MeDelegate(objClassA.DispMethod);objDelegate.NotifyEveryOne+=new Delegate.MeDelegate(objClassB.DispMethod);objDelegate.Notify();29演示:示例 4示例class Delegate/定义委托public delegate void MeDelegate();/定义事件pu
21、blic event MeDelegate NotifyEveryOne;public void Notify()/如果事件不为 nullif(NotifyEveryOne!=null)Console.WriteLine(触发事件:);/触发事件NotifyEveryOne();30演示:示例 4示例class ClassApublic void DispMethod()Console.WriteLine(“Class A 已接到 NotifyEveryOne 事件的通知!);/第二个类class ClassBpublic void DispMethod()Console.WriteLine(“Class B 已接到 NotifyEveryOne 事件的通知!);31总结属性通过使用访问器读取和写入类中的字段,对字段进行保护属性分类为以下四种不同的类型:读/写属性只读属性只写属性可以在类中定义索引器,允许使用下标对该类的对象中的数据进行访问索引器必须总是命名为 this,因为对它们的访问是通过其所属的对象进行的委托包含对方法而不是方法名称的引用C#中的事件允许一个对象将发生的事件或修改通知其他对象32