第5、6章面向对象高级应用.ppt_第1页
第5、6章面向对象高级应用.ppt_第2页
第5、6章面向对象高级应用.ppt_第3页
第5、6章面向对象高级应用.ppt_第4页
第5、6章面向对象高级应用.ppt_第5页
已阅读5页,还剩104页未读, 继续免费阅读

下载本文档

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

文档简介

1、第五章 面向对象高级应用,本章目标,加深理解面向对象编程的概念 掌握类的继承和多态以及如何实现 理解接口、委托和事件以及泛型的相关概念 能够综合面向对象的知识进行相关内容的设计,1 继承,“继承”这个词的字面含义是: 按照法律或遵照遗嘱接受死者的财产、职务、头衔、地位等。 面向对象方法中的继承与现实世界中的继承的意义基本相同,是指一个类完全继承了另一个类的所有内容,如字段、成员函数等。 继承是一种父母与孩子的关系,孩子从父母那继承了一些父母的特点,并也有着与父母的不同之处。,面向对象的关键特性继承,继承示例 灯与台灯、壁灯、吊灯之间的继承关系,父类、超类、基类,子类、派生类,面向对象的关键特性

2、继承,继承示例 人与学生、教师、士兵之间的继承关系,父类、超类、基类,子类、派生类,1.1 继承语法,创建派生类语法: 访问修饰符 class 类名 :基类名称 类主体,public class Animal protected bool IsSleeping; public void Sleep() public void Eat() public class Lion : Animal public void Prey() . Lion adoptedLion = new Lion(); adoptedLion.Prey(); adoptedLion.Eat();,继承描述了“是什么种类”

3、 ( is a kind of)的关系 派生类从基类继承属性和 方法,实现了代码重用 派生类变得更专门化,父类,子类,动物,class Program static void Main(string args) B b = new B(); b.Display(); Console.ReadLine(); ,class A public void Display() Console.WriteLine(display in a); class B : A ,class Program static void Main(string args) B b = new B(); b.Display(

4、); b.T = hello; b.Dispaly11(); Console.ReadLine(); ,class A public void Display() Console.WriteLine(display in a); ,class B : A private string t; public string T get return t; set t = value; public void Dispaly11() Console.WriteLine(值为0, t); ,1.2 在派生类中调用基类的方法,在派生类中可以使用base关键字调用基类的方法。 base关键字使用对象实例,所

5、以在静态成员中使用它会产生错误。,class Program static void Main(string args) Employee e = new Employee(); e.GetEmployeeInfo(); Console.ReadLine(); ,class Person protected string SchoolName = 沈阳理工; protected string Dep = 信息系; public void GetInfo() Console.WriteLine(学校: + SchoolName); Console.WriteLine(部门: + Dep); cl

6、ass Employee : Person public string EmployeeName = 小红; public string zhicheng = 教授; public void GetEmployeeInfo() base.GetInfo(); Console.WriteLine(员工姓名: + EmployeeName); Console.WriteLine(员工职称: + zhicheng); ,1.3 在派生类中调用基类构造函数,在派生类中同样要以使用关键字 base 以调用基类的构造函数,public class Animal public Animal(GenderTy

7、pe gender) / . . . Console.WriteLine(Constructing Animal); public class Elephant : Animal public Elephant(GenderType gender): base(gender) /Elephant code Console.WriteLine(Constructing Elephant); ,public class Person int age; public Person(int age) this.age=age; public class Student : Person int age

8、,string sex; public Student(int age,string sex): base(age) this.sex=sex; ,1 .4 基类、派生类构造函数执行顺序,在C#中,构造函数的执行顺序是:先调用基类的构造函数,再依次调用各派生类的构造函数。,class Program static void Main(string args) Console.WriteLine(第一次实例化,无参:); Child c = new Child(); Console.WriteLine(); Console.WriteLine(第二次实例化,有参:); Child c1 = ne

9、w Child(8); Console.ReadLine(); ,class Parent public Parent() Console.WriteLine(基类的构造函数); class Child : Parent int a; public Child() Console.WriteLine(子类构造函数); public Child(int k) a = k; Console.WriteLine(子类带参构造函数); ,class Program static void Main(string args) Console.WriteLine(第一次实例化,无参:); Child c

10、= new Child(); Console.WriteLine(); Console.WriteLine(第二次实例化,有参:); Child c1 = new Child(8); Console.ReadLine(); Child c2 = new Child(3,4,5); ,class Parent public Parent() Console.WriteLine(基类的构造函数); class Child : Parent int a; public Child() Console.WriteLine(子类构造函数); public Child(int k) a = k; Cons

11、ole.WriteLine(子类带参构造函数); public Child( int a, int b, inc) this.a=a; MyClass tt=new MyClass(b,c); Console.Write(“子类带三参构造函数”); ,class MyClass int x,y; public MyClass( int x, int y) this.x=x; this.y=y; ,1.5 隐藏基类方法,当从基类继承一个成员时,也就继承了其执行代码。 在派生类中,通过定义与基类同名的新成员可以隐藏基类成员,从而达到修改基类成员的目的。 使用new关键字显示地说明所要隐藏的成员。

12、基类: public void Speak() 派生类 new public void Speak(),public class Person int age; public Person(int age) this.age=age; public void Speak() Console.writeLine(“speak language”); ,public class Student : Person int age,string sex; public Student(int age,string sex): base(age) this.sex=sex; new public voi

13、d Speak() Console.writeLine(“speak chinese”); ,1.6 继承特征,继承是可传递的。 C从B派生, B从A派生,那么C不仅继承了B中声明的成员, 也继承了A中的成员。 C#中的派生类只能从一个类继承。 派生类应当是对基类的扩展。 派生类可以添加新的成员,但不能除去已经继承的成员定义 类可以定义虚方法、虚属性。 它的派生类能够重载这些成员,从而实现类的多态性。,2 多态性,多态性允许外界以相似的处理方式对待所有的派生类.,2.1 多态性定义,C#中的多态包括两类:编译时的多态性和运行时的多态性。 编译时的多态性是通过我们前面所介绍的方法重载来实现的。

14、运行时的多态是指直到系统运行时,才根据实际情况决定要实现何种操作。 C#中运行时的多态性是通过虚方法来实现的(虚方法重载)。,2.2 虚方法重载格式,public class Animal public virtual void Eat() Console.WriteLine(Eat something); public class Cat : Animal public override void Eat() Console.WriteLine(Eat small animals); ,static void Main(string args) Bicycle b = new Bicycle

15、(); b.Wheel(); Tricycle t = new Tricycle(); t.Wheel(); Jeep j = new Jeep(); j.Wheel(); ,class Vehicles/车辆 public virtual void Wheel() Console.WriteLine(I have 4 wheels); class Bicycle : Vehicles/自行车 public override void Wheel() Console.WriteLine(I have 2 wheels); class Tricycle : Vehicles/三轮车 public

16、 override void Wheel() Console.WriteLine(I have 3 wheels); class Jeep : Vehicles/吉普车 ,2.3 重写(虚方法重载)与方法重载的区别,重写(虚方法重载)发生在派生类和基类之间,要求派生类重载方法的方法名称、方法参数类型、方法参数数量、方法参数顺序和方法返回值都必须与基类中的虚方法完全一样。 重载是发生在一个类内部,要求方法名称相同,但参数类型和参数个数不同。,2.4 虚方法总结,虚方法重载是函数重载的一种特殊形式,在派生类重新定义虚函数时,要求方法名称,返回类型,参数个数、类型和顺序必须与基类中的虚函数完全一致。

17、 基类中若要定义虚方法,使用virtual关键字,派生类中的虚方法重载,要求声明中加上override关键字。 重写基类方法必须是虚拟的、抽象的或重写的(virtual, abstract,override)。,思考:,使用多态性实现:,2.5 隐藏基类成员和重写方法的区别,重写方法是将代码重写,基类的代码将删除; 而隐藏基类成员并不会把基类代码删除,只是隐藏起来。 当把派生类对象转换成基类对象后,调用隐藏和重写方法时,就可以看出他们的区别: 后者是调用子类中的重写方法,而前者将还是调用基类中的方法,static void Main(string args) MyDerivedClass my

18、obj = new MyDerivedClass(); MyBaseClass mybaseobj; mybaseobj = myobj; mybaseobj.DoSomething(); ,class MyBaseClass public virtual void DoSomething() Console.WriteLine(base imp); class MyDerivedClass : MyBaseClass public override void DoSomething() Console.WriteLine(derived imp); ,static void Main(str

19、ing args) MyDerivedClass myobj = new MyDerivedClass(); MyBaseClass mybaseobj; mybaseobj = myobj; mybaseobj.DoSomething(); ,class MyBaseClass public virtual void DoSomething() Console.WriteLine(base imp); class MyDerivedClass : MyBaseClass new public void DoSomething() Console.WriteLine(derived imp);

20、 ,2.6 隐藏基类成员和重写方法的区别。,在派生类中隐藏基类方法时,基类的此方法既可以定义为虚拟方法,也可以不定义。 此时在子类中使用new来隐藏. 在派生类中重写基类方法时,基类的此方法一定要定义成虚方法。 此时在子类中使用override来重写。,3 抽象方法和抽象类,我们在分析问题时,需要用到抽象。 比如,动物的共同特性可以定义一个动物类,但是该类并不能对应一个具体的事物,只能定义一个抽象的名称,这就是抽象类。 抽象类的定义:abstract修饰类的定义就表示当前类为一个抽象类。 它只能作为其它类的基类,不能实例化,所以它是一种抽象的概念,用来为派生类提供一个公共接口。,3 抽象方法和

21、抽象类,此外,各种动物都有“吃”的行为,我们无法给出一个统一的方法。此时我们可以在他们的基类中定义一个抽象方法,该方法什么事也不做,只是定义一个抽象的名字。 抽象方法: 当创建一个类时,有时需要该类包含一些特殊的方法,该类对这些方法不提供实现,但是该类的非抽象派生类必须实现这些方法,这些方法称为抽象方法。,3.1 抽象方法和抽象类,抽象方法的声明格式: 访问修饰符 abstract 返回值类型 方法名(参数列表) 在派生类中必须实现抽象方法。格式为: 访问修饰符 override 返回值类型 方法名(参数列表) 方法体 ,3.1 抽象方法和抽象类,抽象类的定义方式,abstract class

22、 访问级别 () / . 访问级别 abstract (); / 抽象成员 ,3.1 抽象方法和抽象类,Public abstract class Animal public abstract void Eat(); ,Public class Mouse:Animal public override void Eat() Console.WriteLine(“Eat cheese”); ,static void Main(string args) Circle c = new Circle(3); Console.WriteLine(圆的面积是: + c.GetArea(); Rectang

23、le r = new Rectangle(5, 3); Console.WriteLine(矩形的面积是: + r.GetArea(); Console.ReadLine(); ,abstract class Shape public abstract double GetArea(); class Circle : Shape private double r; public Circle (double r) this.r = r; public override double GetArea() return Math.PI * r * r; ,class Rectangle : Sha

24、pe private double l, w; public Rectangle(double l, double w) this.l = l; this.w = w; public override double GetArea() return l * w; ,3.2 抽象类、抽象方法特点,抽象方法是没有实现的,就连也不能有,在继承于抽象类的类中,必须使用orverride来重写抽象方法。 抽象类用abstract修饰符,只能作为其他类的基类,不能被实例化,不能被密封,不能是private的,也不能使用new操作符。 包含抽象方法的类一定是抽象类,抽象方法只能位于抽象类中。 抽象类可以包含

25、抽象成员,也可以包含非抽象成员。,4 密封类,sealed class表示类没有继承的必要。 密封类不能作为其他类的基类。 抽象类不能同时是密封的,因为抽象类总是希望被继承的。,思考,编写代码,定义一个基类MyClass,其中包含虚拟方法GetString().这个方法应返回存储在受保护字段myString中的字符串,该字段可以通过只写公共属性ContainedString来访问。 从类MyClass中派生一个类MyDerivedClass.重写GetString()方法,使用该方法的基类执行代码从基类中返回一个字符串,但在返回的字符串中添加文本”output from derived cla

26、ss”.,class MyClass protected string myString; public string ContainedString set myString = value; public virtual string GetString() return myString; ,class MyDerivedClass : MyClass public override string GetString() return base.GetString() + (output from derived class); ,class Program static void Ma

27、in(string args) MyDerivedClass d = new MyDerivedClass(); MyClass c = d; c.ContainedString = Console.ReadLine(); Console.WriteLine(基类返回的字符串是 + c.GetString(); ,5 接口,接口是引用类型,一个接口的定义相当于一个约定。实现某接口的类或结构必须遵循该接口定义的约定。 在某种程度上,我们可以说接口是抽象类的一种极端形式,所有的成员都不提供实现, 仅指定实现该接口的类或结构所必须提供的成员。注意继承接口的类必须提供接口成员的实现。 接口成员包括:方

28、法、属性 、索引器、事件。 C#中不支持多重继承,因此当某个类需要继承几个类的行为时单纯用类继承的方法不能解决,这时就要用到接口。(C#中允许一个接口继承多个接口),5.1 声明接口,声明接口: 访问修饰符 interface 接口名:基接口 /接口主体 如: interface IShape bool IsHungry get; set; void Eat( ); ,5.2 实现接口,要实现一个接口,必须要有类继承该接口。 接口的实现类可以是派生类,并且这些派生类可以包括一些自己特有的类成员。,5.3 接口特征,接口的访问修饰符有public、protected 、internal和 pri

29、vate。 所有接口成员都隐式的具有public访问属性。 在接口成员声明中不能包含任何修饰符。 接口名通常以I开头。(要实现该接口,必须有类继承该接口) 接口的成员不能包含常数、字段、运算符、实例构造函数、析构函数或类型,也不能包含任何种类的静态成员。,45,public interface IFile int delFile(); void disFile(); class Pragrom static void Main() IFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue =objMyFile.delFi

30、le(); ,public class MyFile : IFile public int delFile() Console.WriteLine(DelFile 实现!); return 0; public void disFile() Console.WriteLine(DisFile 实现!); ,运行结果: DisFile 实现! DelFile 实现!,建议:在使用接口成员时,坚持将对象转换为所需的接口。,46,5.4 接口与继承,类无法实现多继承,即一个类只能继承于一个基类而不能同时继承于多个基类。在C#中允许一个接口继承于多个接口,当继承于多个接口时,各个接口之间用“,”号隔开。

31、,47,public class MyFile : BaseforInterface, IFile public int delFile() Console.WriteLine(DelFile 实现!); return 0; public void disFile() Console.WriteLine(DisFile 实现!); class Program static void Main() IFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.o

32、pen(); ,运行结果: DisFile 实现! DelFile 实现! 这是 BaseforInterface 的 open 方法,定义一个类: public class BaseforInterface public void open() Console.WriteLine(“这是 BaseforInterface 的open 方法); ,练习,声明一个接口ICDPlayer,包含4个接口方法: Play:输出“play CD” Stop:输出“stop play CD” NextTrack:输出下一首,且私有字段currentTrack增1 PreviousTrack :输出上一首,

33、且私有字段currentTrack减1 (currentTrack0) 只读属性CurrentTrack:返回当前是第几首歌 创建CDPlayer类实现该接口,模拟CD的播放、停止、下一首、上一首的操作。,public interface ICDPlayer void Play(); void Stop(); void PreviousTrack(); void NextTrack(); int CurrentTrack get; ,public class CDPlayer:ICDPlayer private int currentTrack=0; public void Play() Co

34、nsole.WriteLine(“启动 CDPlayer”); public void Stop() Console.WriteLine(“停止 CDPlayer”); public void PreviousTrack() Console.WriteLine(“向前一首”); if(currentTrack=1) currentTrack-; public void NextTrack() Console.WriteLine(“向后一首”); currentTrack+; public int CurrentTrack getreturn currentTrack; ,class Progr

35、am static void Main() ICDPlayer cd=new CDPlayer() cd.Play(); Console.WriteLine(“当前为:”+ cd.CurrentTrack); cd.NextTrack(); Console.WriteLine(“前进后为:”+ cd.CurrentTrack); cd.PreviousTrack(); Console.WriteLine(“后退后为:”+cd.CurrentTrack); cd.Stop(); ,51,5.5 多接口,C# 允许多接口实现 在上面的例子中,再定义一个接口,仅包含一个方法 public inter

36、face IFileTwo void applySecondInterface(); ,52,public class MyFile : BaseforInterface, IFile, IFileTwo public int delFile() Console.WriteLine(DelFile 实现!); return 0; public void disFile() Console.WriteLine(DisFile 实现!); public void applySecondInterface() Console.WriteLine(ApplySecondInterface 实现!);

37、class Program static void Main() MyFile objMyFile = new MyFile(); IFile file =objMyFile; file.disFile(); int retValue = file.delFile(); objMyFile.open(); IFileTwo two=objMyFile; two.applySecondInterface(); ,运行结果: DisFile 实现! DelFile 实现! 这是 BaseforInterface 的 open 方法 ApplySecondInterface 实现!,5.6 显示接口

38、,如果类实现两个接口,并且这两个接口包含具有相同签名的成员,那么在类中实现该成员将导致两个接口都使用该成员作为它们的实现。 interface IControl void Paint(); interface ISurface void Paint(); class SampleClass : IControl, ISurface public void Paint() 这样的话,如果两个接口成员执行不同的函数,那么这可能会导致其中一个接口的实现不正确或两个接口的实现都不正确。,这时可以显式地实现接口成员 - 即创建一个仅通过该接口调用并且特定于该接口的类成员。这是使用接口名称和一个句点命名该

39、类成员来实现的。 public class SampleClass : IControl, ISurface void IControl.Paint() System.Console.WriteLine(IControl.Paint); void ISurface.Paint() System.Console.WriteLine(ISurface.Paint); 类成员 IControl.Paint 只能通过 IControl 接口使用,ISurface.Paint 只能通过 ISurface 使用。两个方法实现都是分离的,都不可以直接在类中使用。,显式实现还用于解决两个接口分别声明具有相同名

40、称不同成员的情况: interface ILeft int P get; interface IRight int P(); 为了同时实现两个接口,类必须对属性 P 和/或方法 P 使用显式实现以避免编译器错误。如: class Middle : ILeft, IRight int ILeft.P get return 0; int IRight.P() return 0; ,56,public interface IFile int delFile(); void disFile(); public interface IFileTwo void applySecondInterface()

41、; void disFile(); public class MyFile : BaseforInterface, IFile, IFileTwo . void IFile.disFile() System.Console.WriteLine(DisFile 的 IFile 实现); void IFileTwo.disFile() System.Console.WriteLine(DisFile 的 IFileTwo 实现); . ,当两个接口中具有同名的方法时,可以使用显式接口实现,不能直接调用MyFile的disFile()方法。,当在具体的实现类中指明接口成员所在的接口时,这种称为显式接

42、口成员。 显式接口成员没有public修饰符。,interface IPartA void SetData(string data); interface IPartB : IPartA void SetData(string data); ,class Test : IPartB private string dataA; private string dataB; void IPartA.SetData(string data) dataA = data; Console.WriteLine(dataA+这是调用了接口A); void IPartB.SetData(string data)

43、 dataB = data; Console.WriteLine(dataB+这是调用了接口B); ,class Program static void Main(string args) IPartB b = new Test(); IPartA a = new Test(); b.SetData(interface A); a.SetData(interface A); a.SetData(interface B); Console.ReadKey(); ,5.7 接口的好处和意义,程序中的接口和计算机中的接口极为神似! 好处是显而易见的: 首先,系统扩展性大有好处。当下层派生类需要改变时

44、,只要接口及接口功能不变,则上层基类不用做任何修改,其他派生类也不需要修改。要新增一个类,只需定义一个符合接口的类即可。 另一个好处就是不同部件或层次的开发人员可以并行开工,就像制造计算机时,生产硬盘、CPU、显示器的,只要接口一致,设计合理,完全可以并行进行开发,从而提高效率。,5.8 抽象类与接口的区别,什么时候使用抽象类,什么时候使用接口,是很容易模糊的概念。 甚至初学者会觉得接口就是多余的,因为单从具体功能来看,除多重继承外,抽象类似乎完全能取代接口。但是,难道接口的存在仅是为了实现多重继承?,抽象类和接口的区别在于使用动机。 使用抽象类是为了代码的复用,而使用接口的动机是为了实现多态

45、性。如果你犹豫不决时,那么可以想想你的动机是什么。 抽象类和它的子类之间应该是一般和特殊的关系,而接口仅仅是它的子类应该实现的一组规则。 例如,交通工具定义成抽象类,汽车、飞机、轮船定义成子类,可以接受的,因为汽车、飞机、轮船都是一种特殊的交通工具。,当准备为组件提供多态性时,表中的建议将有助于在抽象类和接口之间做出正确的选择。,任务分析: 程序功能:模拟U盘、移动硬盘、MP3三种USB存储设备的读、写、播放音乐等操作过程,要求有很强的扩展性,可以随意增加新的设备。 首先用类的方式实现U盘、移动硬盘、MP3三种USB存储设备,完成computer程序功能,然后增加一个新的数码相机存储设备,看看

46、这种方式是否能实现只增加一个新的数码相机类,而不用修改computer程序? 接着用抽象类的方式实现U盘、移动硬盘、MP3三种USB存储设备,然后也增加一个新的数码相机存储设备,看看这种方式是否能实现只增加一个新的数码相机类,而不用修改computer程序? 最后用接口的方式实现U盘、移动硬盘、MP3三种USB存储设备,然后也增加一个新的数码相机存储设备,看看这种方式是否能实现只增加一个新的数码相机类,而不用修改computer程序? 思考上述三种方法的优劣。,学习任务:“USB存储设备”案例,实现方案一:面向类的编程,示例文件,(1)将来新增移动存储设备时,必须对Computer进行修改。

47、(2)如果有100种移动存储,Computer中要写200个方法。,缺点:可扩展性差,实现方案二:通过抽象类实现,示例文件,方案二分析,(1)由于可以运行时动态替换,而不必将移动存储类硬编码在Computer中,所以有了新的第三方设备,完全可以替换进去运行。这就是所谓的“依赖接口,而不是依赖于具体类” (2)它解决了代码冗余的问题,因为可以动态替换移动设备,并且都实现了共同的接口,所以不管有多少种移动设备,只要一个Read方法和一个Write方法,多态性就帮我们解决问题了。,实现方案三:通过接口实现,示例文件,选抽象类还是接口呢?,方案二和方案三,基本是一个方案(从思想层面上来说),只不过实现

48、手段不同,一个是使用了抽象类,一个是使用了接口,而且最终达到的目的应该是一样的。 记得前面我们对抽象类和接口选择的建议吗? 看动机。这里,我们的动机显然是实现多态性而不是为了代码复用,所以当然要用接口。 所以,最优的方案是方案三。,开闭原则:拥抱变化,这就是所谓面向对象设计的重要基石:开闭原则-“对扩展开放,对修改关闭”。 就是说我们开发了一个软件,应该可以对它进行功能扩展(开放),而在进行这些扩展的时候,不需要对原来的程序进行修改(关闭)! 面向接口的编程是实现开闭原则的重要方法,能够灵活运用则 “任督二脉打通”。,故事还没有结束,有人通知我说又有一个叫SuperStorage的移动设备要接

49、到我们的Computer上。 设备送来后,我傻眼了,开发这个新设备的团队没有拿到我们的IMobileStorage接口,自然也没有遵照这个约定。这个设备的读、写方法不叫Read和Write,而是叫rd和wt,这下完了不符合接口啊,插不上。,“适配器”设计模式插上一个插座,示例文件,总结,面向类的编程,需要修改各个涉及的类,因此用户不管是否使用到新的功能,所有用户都需要升级,工作量大。 而面向接口的编程,不需要修改各个涉及的类,只有需要新功能的用户需要升级,大大节约工作量。 只有可能存在扩展变化的情况,才使用接口,注意也不要设计过度。,练习,以下程序的运行结果是 class A publicA(

50、) Console.WriteLine(“Constructing base.”); A() Console.WriteLine(“Destruing base.”); class B:A publicB() Console.WriteLine(“Constructing derived.”); B() Console.WriteLine(“Destruing derived.”); ,class Program static void Main(string args) B b=new B(); ,练习,以下程序的输出结果为N,请在空白处填入适当内容,把程序补充完整。 class BaseC

51、lass protected char s; public BaseClass(char c) s=c; class Derived : BaseClass _ public void Print() Console.WriteLine(s); ,class Test public static void Main(string args) Derived d = new Derived(M); d.Print(); ,public Derived(char c):base(char)(c+1) ,练习,在基类中定义一个私有字段s,找出以下程序中的错误,并更改错误使得程序能正确输出结果为N。

52、class BaseClass private char s; public BaseClass(char c) s=c; class Derived : BaseClass public Derived (char c):base(char) (c+1) ; public void Print() Console.WriteLine(s); ,class Test public static void Main(string args) Derived d = new Derived(M); d.Print(); ,谢谢!,泛型与集合,继承提供了代码重用机制,而泛型提供了另外一种代码重用机制

53、,它允许使用同样的代码处理不同类型的数据。 在C#中,泛型常与集合一起使用,创建能够存储不同类型数据的集合。,5 泛型的引入(1),在讲到泛型之前,我们先看下面这段代码,它是实现一个栈,这个栈只能处理int数据类型: public class Stack private int m_item; public int Pop(). public void Push(int item). public Stack(int i) this.m_item = new inti; 当需要一个栈来保存string类型时,该怎么办呢?,5 泛型的引入(2),优秀的程序员会想到用一个通用的数据类型object

54、来实现这个栈: public class Stack private object m_item; public object Pop(). public void Push(object item). public Stack(int i) this.m_item = newi; 这样写很好,非常灵活且可以接收任何数据类型,可以说是一劳永逸。但会存在一些缺陷,主要表现在:,5 泛型的引入(3),当Stack处理值类型时会出现装箱、折箱操作,这将在托管堆上分配和回收大量的变量,若数据量大,则性能损失非常严重。 在处理引用类型时,虽然没有装箱和折箱操作,但将用到数据类型的强制转换操作,增加处理器

55、的负担。 在数据类型的强制转换上还有更严重的问题 。如: Stack stack=new Stack(); Node1 x = new Node1(); stack.Push(x); Node2 y = (Node2)stack.Pop();,5 泛型,泛型是用一个通用的数据类型T来代替object,在类实例化时指定T的类型,运行时自动编译为本地代码,运行效率和代码质量都有很大提高,并且保证数据类型安全。 用泛型来重写上面的栈: public class Stack private T m_item; public T Pop(). public void Push(T item). publ

56、ic Stack(int i) this.m_item = new Ti; ,5 泛型,泛型的定义 修饰符 class 类名 类体 泛型类型参数只是特定类型的一个点位符,实例化时,要用具体的类型来替换泛型类型参数。 实例化泛型类 类名 实例名 = new 类名 (构造函数的实参),5 泛型的调用,Stack a = new Stack(100); a.Push(10); a.Push(“8888”); int x = a.Pop(); Stack b = new Stack(100); b.Push(10); b.Push(8888); string y = b.Pop();,这一行编译不通过,因为类a只接收int类型的数据,这一行编译不通过,因为类b只接收string类型的数据,5 泛型的优点,它是类型安全的。实例化了int类型的栈,就不能处理string类型的数据,其他数据类型也一样。

温馨提示

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

评论

0/150

提交评论