C#高级编程.ppt_第1页
C#高级编程.ppt_第2页
C#高级编程.ppt_第3页
C#高级编程.ppt_第4页
C#高级编程.ppt_第5页
已阅读5页,还剩54页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第八章,C#高级编程,2,目标,理解继承 在C# 中使用继承 在C#中使用接口 在C#中使用方法的重写 理解属性及其不同的类型、实现 理解和使用索引器 实现委托 定义和触发事件,3,继承 2-1,Class Base / 成员变量 int basevar; / 成员函数 Base_fun1() / 定义 . .,Class Derived : Base / 成员变量 int derivedvars; / 成员函数 Derived_fun1() / 定义 . .,基类,void main() Derived dr_obj = new Derived() ; dr_obj.Base_fun1();

2、 ,无需重新编写代码,派生类,4,狗,马,继承 2-2,动物,基类,派生类,继承的层次结构示例,Class Animal / 成员变量 int eyes, nose; Animal() eyes = 2; nose = 1; Pet_Animal() / 定义 ,基类,Class Dog : Animal / 成员变量 / 成员函数 private Barking() / 定义 private Wagging_Tail() ,派生类,5,继承 C# 中的类,public class Graduate: Student, Employee / 成员变量 / 成员函数 ,多重继承,允许多重接口实现

3、,6,演示,public class Student:Person private string _school; private uint _eng; private uint _math; private uint _sci; public void GetMarks() Console.WriteLine(“请输入学校名称); _school = Console.ReadLine(); Console.WriteLine(请分别输入英语、数学和自然科学的分数。); _eng = uint.Parse(Console.ReadLine(); _math = uint.Parse(Conso

4、le.ReadLine(); _sci = uint.Parse(Console.ReadLine(); Console.WriteLine(“所得总分为:0,_eng+_math+_sci); ,派生类,public class Person private string _name; private uint _age; public void GetInfo() Console.WriteLine(请输入您的姓名和年龄); _name = Console.ReadLine(); _age = uint.Parse(Console.ReadLine(); public void DispI

5、nfo() Console.WriteLine(尊敬的 0,您的年龄为 1 , _name, _age); ,基类,static void Main(string args) Student objStudent = new Student(); objStudent.GetInfo(); objStudent.DispInfo(); objStudent.GetMarks(); ,调用的基类成员,无法实现 GetInfo() 和 DispInfo() 方法,7,演示,public class Person private string _name; private uint _age; pu

6、blic void GetInfo() Console.WriteLine(请输入您的姓名和年龄); _name = Console.ReadLine(); _age = uint.Parse(Console.ReadLine(); public void DislayInfo() Console.WriteLine(尊敬的 0,您的年龄为 1, _name, _age); ,public class Student:Person private string _schoolname; private uint _engscore; private uint _mathscore; priva

7、te uint _sciscore; private uint _totscore; public uint GetMarks() Console.WriteLine(“请输入学校名称); _schoolname = Console.ReadLine(); Console.WriteLine(请输入英语、数学和自然科学的分数。 ); _engscore = uint.Parse(Console.ReadLine(); _mathscore = uint.Parse(Console.ReadLine(); _sciscore = uint.Parse(Console.ReadLine(); _t

8、otscore = _engscore + _mathscore + _sciscore; Console.WriteLine(所得总分为:0 ,_totscore); return _totscore; ,基类,派生类,public class UnderGraduate:Student public void Check() Console.WriteLine(要上升一级,要求总分不低于 150 ); if(this.GetMarks() =150) Console.WriteLine(“pass); else Console.WriteLine(“not pass); ,派生类,publ

9、ic static void Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DisplayInfo(); objUnderGraduate.Check(); ,8,用于从派生类中访问基类成员 可以使用 base 关键字调用基类的构造函数,关键字 base,9,调用 base 构造函数,public class Student:Person private uint id; /调用 Person 构造函数 pu

10、blic Student(string name,uint age,uint id):base(name,age) this.id = id; Console.WriteLine(id); ,:base 关键字将调用 Person 类构造函数,10,演示,public class Person public string _name; public uint _age; public Person(string name, uint age) this._name = name; this._age = age; Console.WriteLine(_name); Console.WriteL

11、ine(_age); ,public class Student:Person private uint _id; public Student(string name, uint age, uint id):base(name, age) this._id = id; Console.WriteLine(_id); ,还将调用 Base 构造函数,static void Main(string args) /构造 Student Student objStudent = new Student(XYZ, 45, 001); ,11,关键字 override,Class Base / 成员变量

12、 int basevar; / 成员函数 GetInfo() / 定义 . .,Class Derived : Base / 成员变量 int derivedvars; / 成员函数 override GetInfo() / 定义 . .,基类,派生类,base 方法的新实现,12,关键字 virtual,访问修饰符 virtual 返回类型 方法名 ( 参数列表 ) . / Virtual 方法实现 . ,public virtual void Func() Console.WriteLine(“这是 virtual 方法,可以被重写); ,13,/将执行派生类的变量,/要求 new 访问修

13、饰符,将输出派生类中的 val,相同字段,new,隐藏继承成员,关键字 new,14,class Student public virtual void StuInfo() Console.WriteLine(“此方法显示学生信息); ,class ExpStudent: Student public override void StuInfo() base.StuInfo(); Console.WriteLine(“此方法重写 base 方法); ,static void Main(string args) ExpStudent objStudent = new ExpStudent();

14、objStudent.StuInfo(); Student objSuper = objStudent; objSuper.StuInfo(); ,15,抽象类和抽象方法 2-1,abstract class ClassOne /类实现 ,访问修饰符,派生类的基类,不能实例化,16,abstract class Base / 成员变量 int basevar; / 成员函数 abstract void base_fun1(parameters); / 无法实现 . ,抽象方法,class Derived : Base / 成员变量 int derivedvars; / 成员函数 overrid

15、e void Base_fun1(parameters) / 实际实现 . ,抽象类,派生类,提供,重写方法,原型,必须重写,抽象类和抽象方法 2-2,17,演示,using System; namespace Example_1 abstract class ABC public abstract void AFunc(); public void BFunc() Console.WriteLine(“这是一个非抽象方法!); class Derv : ABC public override void AFunc() Console.WriteLine(“这是一个抽象方法! ); ,抽象类

16、不能实例化,派生类 重写方法,static void Main(string args) Derv objB = new Derv(); objB.AFunc(); objB.BFunc(); ,18,abstract class MyAbs public abstract void AbMethod(); /派生类 class MyClass : MyAbs public override void AbMethod() Console.WriteLine(“在 MyClass 中实现的抽象方法); /派生自 MyClass 的子类 class SubMyClass:MyClass publ

17、ic void General() /未实现 AbMethod 抽象方法 Console.WriteLine(在 SubMyClass 中未实现的抽象方法 ); ,static void Main(string args) SubMyClass objSubClass = new SubMyClass(); objSubClass.General(); ,19,接口 3-1,OFF,ON,请按开关按钮:ON/OFF,两种方法 ON OFF,20,接口 3-2,class IBase void method1(); int method2(); /没有实现 . ,接口,interface,只有方

18、法声明,没有实现,21,public interface IPicture int DelImage(); void ShowImage(); ,隐式声明为 public,无访问修饰符,示例中的 IPicture接口用于演示接口,接口 3-3,22,演示,public class MyPicture : IPicture /第一个方法的实现 public int DelImage() Console.WriteLine(“DelImage 实现!); return(1); /第二个方法的实现 public void ShowImage() Console.WriteLine(“ShowImag

19、e 实现!); ,static void Main(string args) MyPicture objM = new MyPicture(); objM.ShowImage(); int t = objM.DelImage(); Console.WriteLine(t); ,派生自 IPicture 接口,23,演示,public interface IPicture int DelImage(); void ShowImage(); public class MyPicture : BaseIO, IPicture public int DelImage() Console.WriteLi

20、ne(“DelImage 实现!); return(1); public void ShowImage() Console.WriteLine(“ShowImage 实现!); ,public class BaseIO public void Open() Console.WriteLine(BaseIO 的 Open 方法); ,static void Main(string args) MyPicture objM = new MyPicture(); objM.ShowImage(); int val = objM.DelImage(); Console.WriteLine(val);

21、objM.Open(); ,24,多重接口实现,C# 不允许多重类继承 但C#允许多重接口实现 这意味着一个类可以实现多个接口,25,演示,public interface IPictManip void ApplyAlpha(); /第二个接口 public interface IPicture int DelImage(); void ShowImage(); public class BaseIO public void Open() Console.WriteLine(“BaseIO 的 Open 方法); ,static void Main(string args) MyPictur

22、e objM = new MyPiture(); objM.ShowImage(); objM.DelImage(); objM.ApplyAlpha(); objM.Open(); ,26,显式接口实现,在 C# 中,只要不发生命名冲突,就完全可以允许多重接口实现,public interface IPictManip void ApplyAlpha(); ,public interface IPicture void ApplyAlpha(); ,public class MyPicture : BaseIO, IPicture, IPictManip public int ApplyAl

23、pha() . . ,?,使用显式接口实现,27,演示,public class MyPicture : BaseIO, IPicture, IPictManip public int DelImage() Console.WriteLine(“DelImage 实现!); return(1); public void ApplyAlpha() Console.WriteLine(“ApplyAlpha 实现!); void IPicture.ShowImage() Console.WriteLine(“ShowImage 的 IPicture 实现); void IPictManip.Sho

24、wImage() Console.WriteLine(“ShowImage 的 IPictManip 实现); ,显式接口实现,static void Main(string args) MyPicture objM = new MyPicture(); IPicture Pict = objM; /IPicture 引用 Pict.ShowImage(); IPictManip PictManip = objM; /IPictManip 引用 PictManip.ShowImage(); ,28,演示,public interface IPicture int DelImage(); pub

25、lic interface IPictManip void ApplyAlpha(); void ShowImage(); /继承多重接口 public interface IPictAll:IPicture, IPictManip void ApplyBeta(); ,public class MyPicture:IPictAll public int DelImage() Console.WriteLine(“DelImage 实现!); return(1); public void ApplyAlpha() Console.WriteLine(“ApplyAlpha 实现!); publ

26、ic void ApplyBeta() Console.WriteLine(“ApplyBeta 实现!); public void ShowImage() Console.WriteLine(“DisplayImage 实现!); ,static void Main(string args) MyPicture objM = new MyPicture(); objM.ShowImage(); int val = objM.DelImage(); Console.WriteLine(val); objM.ApplyAlpha(); objM.ApplyBeta(); ,29,自定义接口,如老

27、师和学员都可以收作业, 那么收作业的方法应该放在哪个类?,A:Teacher类,B:Student类,C:两个都放,D:Person类,E:重新定义,造成代码冗余,如果增加一个工程师类,他不会收作业,如果继承这个类,Person类怎么办?,调用收作业的方法不需要改变,自定义一个接口来实现IHomeworkCollector,30,自定义接口,public interface IHomeworkCollector void CollectHomework(); ,public class Student : Person, IHomeworkCollector,public void Colle

28、ctHomework() MessageBox.Show(报告老师!作业收集完毕!); ,1、定义一个收作业的接口,2、在有收作业功能的类实现该接口,3、不同的类收作业方式不同,31,使用自定义接口,1、接口作为参数使用,private void DoCollectHomework(IHomeworkCollector collector) collector.CollectHomework(); ,无论谁收作业这里都不需要做任何改变,2、接口作为返回值使用,DoCollectHomework(scofield);,private IHomeworkCollector CreateHomewo

29、rkCollector(string type) switch (type) case student: collector = new Student(Scofield, Genders.Male, 28, 越狱狱); break; return collector ,返回一个实现该接口的对象,collector.CollectHomework();,32,作为返回值和参数的意义,接口作为参数 传递了实现接口的对象 接口作为返回值 返回一个实现了接口的对象,33,接口和抽象类的对比,34,委托,Multiply(int,int) . Divide(int,int) . ,在运行时确定调用哪种

30、方法,委托和方法必须具有相同的签名,- public delegate Call(int n1, int n2); -,35,定义委托 2-1,访问修饰符 delegate 返回类型 委托名();,语法,class Delegates public delegate int Call(int n1, int n2); class Math public int Multiply(int n1, int n2) / 实现 public int Divide(int n1, int n2) / 实现 ,将方法与委托关联起来,class TestDelegates static void Main(

31、) Call objCall; Math objMath = new Math(); objCall = new Call(objMath.Multiply); ,36,定义委托 2-2,class Delegates / 委托定义 public delegate int Call(int n1, int n2); class Math / 乘法方法 public int Multiply(int n1, int n2) return n1*n2; / 除法方法 public int Divide(int n1, int n2) if(n2!=0) return n1/n2; ,static

32、void Main(string args) / 委托的对象 Call objCall; / Math 类的对象 Math objMath = new Math(); / 将方法与委托关联起来 objCall = new Call(objMath.Multiply); / 将委托实例化 result = objCall(4, 3); System.Console.WriteLine(结果为 0, result); ,将方法与委托关联起来,37,事件,“请听题”,集中注意力聆听,其他人,事件源,事件的发布者,事件的订阅人,不关心,未订阅该事件,定义事件 为对象订阅该事件 将发生的事件通知给订阅人

33、,38,定义事件,访问修饰符 event 委托名 事件名;,语法,public delegate void delegateMe(); private event delegateMe eventMe;,39,订阅事件,eventMe += new delegateMe(objA.Method); eventMe += new delegateMe(objB.Method);,40,通知订阅对象,if(condition) eventMe(); ,调用订阅特定事件的对象的所有委托,41,示例,class TestEvents STAThread static void Main(string

34、args) / 委托的对象 Delegate objDelegate = new Delegate(); / ClassA 的对象 ClassA objClassA = new ClassA(); / ClassB 的对象 ClassB objClassB = new ClassB(); / 订阅该事件 objDelegate.NotifyEveryOne += new Delegate.MeDelegate(objClassA.DispMethod); objDelegate.NotifyEveryOne += new Delegate.MeDelegate(objClassB.DispMe

35、thod); objDelegate.Notify(); ,class Delegate / 定义委托 public delegate void MeDelegate(); / 定义事件 public event MeDelegate NotifyEveryOne; public void Notify() / 如果事件不为 null if(NotifyEveryOne != null) Console.WriteLine(触发事件:); / 触发事件 NotifyEveryOne(); ,class ClassA public void DispMethod() Console.WriteL

36、ine(“Class A 已接到 NotifyEveryOne 事件的通知!); / 第二个类 class ClassB public void DispMethod() Console.WriteLine(“Class B 已接到 NotifyEveryOne 事件的通知! ); ,42,什么是程序集,为什么只运行这个.exe文件就能使用我们程序的所有功能?,43,什么是程序集,.NET框架应用程序的生成块 包含编译好的代码的逻辑单元 创建的每个项目文件(project)都会产生一个程序集dll MyNewsReader.exe是“新闻快客”的程序集 程序集的结构 程序集清单 类型元数据 I

37、L代码 资源,向其他应用程序公开,并由这些应用程序使用,元数据是二进制信息,程序中的图片、音乐文件,44,程序集清单,程序集清单:元数据重要部分,45,ILDasm,ILDasm 反编译的工具。 可以查看IL汇编代码 可以看到类和方法,演示: 使用ILDasm,46,Visual Studio创建程序集,Visual Studio的所有应用程序都创建程序集,AssemblyInfo.cs,用于配置程序集的属性,47,AssemblyInfo.cs介绍,AssemblyInfo.cs文件,assembly: AssemblyTitle(My News Reader) assembly: Asse

38、mblyDescription() assembly: AssemblyConfiguration() assembly: AssemblyCompany(Jade Bird) assembly: AssemblyProduct(My News Reader) assembly: AssemblyCopyright(版权所有 (C) Jade Bird 2007) assembly: AssemblyTrademark() assembly: AssemblyCulture() assembly: AssemblyVersion() assembly: AssemblyFileV

39、ersion(),48,程序集与 internal 访问修饰符,internal修饰符用于程序集级别指定访问性 internal修饰的成员不能被其它程序集访问,49,什么是序列化,Profile对象,界面语言:英语,RssFeed对象,存储 介质,存储,序列化是将对象的状态存储到特定存储介质中的过程,代理服务器,50,特性,Serializable abstract class FeedBase,标识这个类是可序列化的,可序列化就是这个类的一个特性,描述性关键字 对程序中的元素如:类、字段、方法、属性 命名时以Attribute结尾: SerializableAttribute

40、使用时省略Attribute,public sealed class SerializableAttribute,特性其实是一个类,class Program Obsolete(不要使用旧的方法, 请使用新的方法, true) static void Old() static void New() public static void Main() Old(); ,演示 MyAttribute,51,使用序列化,fileStream = new FileStream(profile.bin, FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fileStream, Profile);,Serializa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论