第四章 面向对象高级编程_第1页
第四章 面向对象高级编程_第2页
第四章 面向对象高级编程_第3页
第四章 面向对象高级编程_第4页
第四章 面向对象高级编程_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

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

2、ved() ; dr_obj.Base_fun1(); 无需重新编写代码 派生类 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, Employe

3、e / 成员变量成员变量 / 成员函数成员函数 多重继承 允许多重接口实现 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

4、.ReadLine(); _math = uint.Parse(Console.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(C

5、onsole.ReadLine(); public void DispInfo() 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 st

6、ring _name; private uint _age; public 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

7、; private uint _mathscore; private 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 = u

8、int.Parse(Console.ReadLine(); _totscore = _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.

9、WriteLine(“not pass); 派生类 public static void Main(string args) UnderGraduate objUnderGraduate = new UnderGraduate(); objUnderGraduate.GetInfo(); objUnderGraduate.DisplayInfo(); objUnderGraduate.Check(); 8 q用于从派生类中访问基类成员 q可以使用 base 关键字调用基类的构造函数 关键字关键字 base 9 调用调用 base 构造函数构造函数 public class Student:Pe

10、rson private uint id; /调用 Person 构造函数 public 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; C

11、onsole.WriteLine(_name); Console.WriteLine(_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

12、); 11 关键字关键字 override Class Base / 成员变量 int basevar; / 成员函数 GetInfo() / 定义 . . Class Derived : Base / 成员变量成员变量 int derivedvars; / 成员函数成员函数 override GetInfo() / 定义定义 . . 基类派生类 base 方法的新实现 12 关键字关键字 virtual 访问修饰符访问修饰符 virtual 返回类型返回类型 方法名方法名 ( 参数列表参数列表 ) . / Virtual 方法实现 . public virtual void Func() C

13、onsole.WriteLine(“这是 virtual 方法,可以被重写); 13 /将执行派生类的变量 /要求 new 访问修饰符 将输出派生类中的 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

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

15、ers); / 无法实现无法实现 . 抽象方法抽象方法 class Derived : Base / 成员变量成员变量 int derivedvars; / 成员函数成员函数 override void Base_fun1(parameters) / 实际实现实际实现 . 抽象类 派生类 提供提供 重写方法 原型原型 必须重写重写 抽象类和抽象方法抽象类和抽象方法 2-2 17 演示 using System; namespace Example_1 abstract class ABC public abstract void AFunc(); public void BFunc() Con

16、sole.WriteLine(“这是一个非抽象方法!); class Derv : ABC public override void AFunc() Console.WriteLine(“这是一个抽象方法! ); 抽象类 不能实例 化 派生类 重写方法 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

17、override void AbMethod() Console.WriteLine(“在 MyClass 中实现的抽象方法); /派生自 MyClass 的子类 class SubMyClass:MyClass public void General() /未实现 AbMethod 抽象方法 Console.WriteLine(在 SubMyClass 中未实现的抽象方法 ); static void Main(string args) SubMyClass objSubClass = new SubMyClass(); objSubClass.General(); 19 接口接口 3-1

18、OFF ON 请按开关按钮:ON/OFF 两种方法两种方法 ON OFF 20 接口接口 3-2 class IBase void method1(); int method2(); /没有实现没有实现 . 接口接口 interface 只有方法声明 没有实现 21 public interface IPicture int DelImage(); void ShowImage(); 隐式声明为 public 无访问修饰符 示例中的 IPicture接口用于演示接口 接口接口 3-3 22 演示 public class MyPicture : IPicture /第一个方法的实现 publi

19、c int DelImage() Console.WriteLine(“DelImage 实现!); return(1); /第二个方法的实现 public void ShowImage() Console.WriteLine(“ShowImage 实现!); static void Main(string args) MyPicture objM = new MyPicture(); objM.ShowImage(); int t = objM.DelImage(); Console.WriteLine(t); 派生自 IPicture 接口 23 演示 public interface I

20、Picture int DelImage(); void ShowImage(); public class MyPicture : BaseIO, IPicture public int DelImage() Console.WriteLine(“DelImage 实现!); return(1); public void ShowImage() Console.WriteLine(“ShowImage 实现!); public class BaseIO public void Open() Console.WriteLine(BaseIO 的 Open 方法); static void Ma

21、in(string args) MyPicture objM = new MyPicture(); objM.ShowImage(); int val = objM.DelImage(); Console.WriteLine(val); objM.Open(); 24 多重接口实现多重接口实现 vC# 不允许多重类继承 v但C#允许多重接口实现 v这意味着一个类可以实现多个接口 25 演示 public interface IPictManip void ApplyAlpha(); /第二个接口 public interface IPicture int DelImage(); void Sh

22、owImage(); public class BaseIO public void Open() Console.WriteLine(“BaseIO 的 Open 方法); static void Main(string args) MyPicture objM = new MyPiture(); objM.ShowImage(); objM.DelImage(); objM.ApplyAlpha(); objM.Open(); 26 显式接口实现显式接口实现 在 C# 中,只要不发生命名冲突,就完全可以允许多重接口实现 public interface IPictManip void Ap

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

24、Console.WriteLine(“ApplyAlpha 实现!); void IPicture.ShowImage() Console.WriteLine(“ShowImage 的 IPicture 实现); void IPictManip.ShowImage() Console.WriteLine(“ShowImage 的 IPictManip 实现); 显式接口实现 static void Main(string args) MyPicture objM = new MyPicture(); IPicture Pict = objM; /IPicture 引用 Pict.ShowIma

25、ge(); IPictManip PictManip = objM; /IPictManip 引用 PictManip.ShowImage(); 28 演示 public interface IPicture int DelImage(); public interface IPictManip void ApplyAlpha(); void ShowImage(); /继承多重接口 public interface IPictAll:IPicture, IPictManip void ApplyBeta(); public class MyPicture:IPictAll public in

26、t DelImage() Console.WriteLine(“DelImage 实现!); return(1); public void ApplyAlpha() Console.WriteLine(“ApplyAlpha 实现!); public void ApplyBeta() Console.WriteLine(“ApplyBeta 实现!); public void ShowImage() Console.WriteLine(“DisplayImage 实现!); static void Main(string args) MyPicture objM = new MyPicture

27、(); objM.ShowImage(); int val = objM.DelImage(); Console.WriteLine(val); objM.ApplyAlpha(); objM.ApplyBeta(); 29 自定义接口自定义接口 如老师和学员都可以收作业,如老师和学员都可以收作业, 那么收作业的方法应该放在哪个类?那么收作业的方法应该放在哪个类? A:Teacher类类 B:Student类类 C:两个都放两个都放 D:Person类类 E:重新定义重新定义 造成代码冗余造成代码冗余 如果增加一个工程师类,他不会收作业如果增加一个工程师类,他不会收作业 如果继承这个类,如果继

28、承这个类,Person类怎么办类怎么办? 调用收作业的方法不调用收作业的方法不 需要改变需要改变 自定义一个接口来实现自定义一个接口来实现IHomeworkCollector 30 自定义接口自定义接口 public interface IHomeworkCollector void CollectHomework(); public class Student : Person, IHomeworkCollector public void CollectHomework() MessageBox.Show(报告老师!作业收集完毕!报告老师!作业收集完毕!); 1、定义一个收作业的接口、定义

29、一个收作业的接口 2、在有收作业功能的类实现该接口、在有收作业功能的类实现该接口 3、不同的类收作业方式不同、不同的类收作业方式不同 31 使用自定义接口使用自定义接口 1、接口作为参数使用、接口作为参数使用 private void DoCollectHomework(IHomeworkCollector collector) collector.CollectHomework(); 无论谁收作业这里都不无论谁收作业这里都不 需要做任何改变需要做任何改变 2、接口作为返回值使用、接口作为返回值使用 DoCollectHomework(scofield); private IHomeworkC

30、ollector CreateHomeworkCollector(string type) switch (type) case student: collector = new Student(Scofield, Genders.Male, 28, 越狱狱越狱狱); break; return collector 返回一个实现该返回一个实现该 接口的对象接口的对象 collector.CollectHomework(); 32 作为返回值和参数的意义作为返回值和参数的意义 v接口作为参数 传递了实现接口的对象 v接口作为返回值 返回一个实现了接口的对象 33 接口和抽象类的对比接口和抽象类的

31、对比 抽象类接口 不 同 点 用 abstract 定义用 interface 定义 只能继承一个类可以实现多个接口 非抽象派生类必须实现抽象方法 实现接口的类必须实现 所有成员 需要override实现抽象方法直接实现 相 同 点 不能实例化 包含未实现的方法 派生类必须实现未实现的方法 34 委托委托 Multiply(int,int) . Divide(int,int) . 在运行时确定调用哪种方法 委托和方法必须具有相同的签名 - public delegate Call(int n1, int n2); - 35 定义委托定义委托 2-1 访问修饰符 delegate 返回类型 委托

32、名(); 语法 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() Call objCall; Math objMath = new Math(); objCall = new Call(objMath.Multiply); 36 定义委托定义委托 2-

33、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 void Main(string args) / 委托的对象 Call objCall; / Math 类的对象 Math objMath = new Math(); / 将方法与委托关

34、联起来 objCall = new Call(objMath.Multiply); / 将委托实例化 result = objCall(4, 3); System.Console.WriteLine(结果为 0, result); 将方法与委托关联起来 37 事件事件 抢答者 宣布人宣布人 抢答者 “请听题” 集中注意力聆听 其他人 事件源 事件的发布者 事件的订阅人 未订阅该事件 定义事件 为对象订阅该事件 将发生的事件通知给订阅人 38 定义事件定义事件 访问修饰符 event 委托名 事件名; 语法 public delegate void delegateMe(); private event delegateMe eventMe; 39 订阅事件订阅事件 eventMe += new delegateMe(objA.Method); eventMe += new delegateMe(objB.Method); 40 通知订阅对象通知订阅对象 if(con

温馨提示

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

最新文档

评论

0/150

提交评论