第4章 面向对象编程进阶.ppt_第1页
第4章 面向对象编程进阶.ppt_第2页
第4章 面向对象编程进阶.ppt_第3页
第4章 面向对象编程进阶.ppt_第4页
第4章 面向对象编程进阶.ppt_第5页
已阅读5页,还剩135页未读 继续免费阅读

下载本文档

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

文档简介

1、第4章 面向对象编程进阶,4.1类的继承与多态 当今的面向对象编程语言都提供了继承和多态的功能,C#作为一种面向对象的高级编程语言也具有这样的特点。继承是面向对象语言的基本特征,是实现代码复用的手段。继承使得在原有的类基础之上,对原有的程序进行扩展,从而提高程序开发的速度,实现代码的复用。同一种方法作用于不同对象可以产生不同的结果,这就是多态性。它是在基类中使用虚方法,在其派生类中使用重载实现的。,4.1.1 继承,1. 使用继承 继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。使用继承而产生的类被称为派生类或者子类,而被继承的类则称为基类

2、、超类或父类。客观世界中的许多事物之间往往都是具有相同的特征,具有继承的特点。图4.1和图4.2是两个采用类的层次图表示继承的例子。,1. 使用继承,图4.1 类的层次结构1,图4.2 类的层次结构2,4.1.1 继承,【例4.1】类的继承。 using System; /定义基类Shape public class Shape protected string Color; public Shape() ; public Shape(string Color) this.Color = Color; public string GetColor() return Color; ,【例4.1】

3、,/定义Circle类,从Shape类中派生 public class Circle : Shape private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public double GetArea() return System.Math.PI * Radius * Radius; ,【例4.1】,/ 派生类Rectangular,从Shape类中派生 public class Rectangular:Shape protected

4、 double Length, Width; public Rectangular() Length=Width=0; public Rectangular(string Color,double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public double AreaIs() return Length*Width; ,【例4.1】,public double PerimeterIs()/周长 return (2*(Length+Width); / 派生类Squ

5、are,从 Rectangular类中派生 public class Square: Rectangular public Square(string Color,double Side) this.Color = Color; this.Length=this.Width = Side; public class TestInheritance public static void Main(),【例4.1】, Circle Cir= new Circle(orange,3.0); Console.WriteLine(Circle color is 0,Circle area is 1, C

6、ir.GetColor(),Cir.GetArea(); Rectangular Rect= new Rectangular(red,13.0, 2.0); Console.WriteLine(Rectangualr color is 0,Rectangualr area is 1, Rectangular perimeter is 2, Rect.GetColor(),Rect.AreaIs(), Rect.PerimeterIs(); Square Squ= new Square(green,5.0); Console.WriteLine(Square color is 0,Square

7、Area is 1, Square perimeter is 2, Squ.GetColor(),Squ.AreaIs(), Squ.PerimeterIs(); 运行结果如图4.3所示。,图4.3 运行结果,4.1.1 继承,2. base关键字 例4.1程序中的square类也可以改写为: / 派生类Square,从 Rectangular类中派生 public class Square: Rectangular public Square(string Color,double Side):base(Color,Side,Side) ; 关键字base的作用是调用Rectangular类

8、的构造函数并将Square类的变量初始化。如果将Square类改写成: / 派生类Square,从 Rectangular类中派生 public class Square: Rectangular public Square(string Color,double Side) ; ,2. base关键字,实际上这种情况调用的是父类的无参构造函数,而不是有参构造函数,等同于: / 派生类Square,从 Rectangular类中派生 public class Square: Rectangular public Square(string Color,double Side):base() ;

9、 base关键字除了能调用基类对象构造函数,还可以调用基类的方法。在下例中,Employee类的GetInfoEmployee方法使用了base关键字调用基类Person的GetInfoPerson方法。,2. base关键字,【例4.2】base调用基类的方法。 using System; public class Person protected string Phone = 444-555-666; protected string Name = 李明; public void GetInfoPerson() Console.WriteLine(Phone: 0, Phone); Con

10、sole.WriteLine(Name: 0, Name); class Employee : Person public string ID = ABC567EFG; public void GetInfoEmployee(),【例4.2】, / 调用基类Person的GetInfo方法 base.GetInfoPerson(); Console.WriteLine(Employee ID: 0,ID); class TestClass public static void Main() Employee Employees = new Employee(); Employees.GetIn

11、foEmployee(); ,4.1.1 继承,3. 继承中的构造函数与析构函数 在前面讨论过派生类的构造函数会隐式调用父类的无参构造函数。那么,如果父类也是派生于其它类,会是什么样的情形呢?C#的执行顺序是这样的:根据层次链找到最顶层的基类,先调用基类的构造函数,再依次调用各级派生类的构造函数。而析构函数的次序正好相反。 【例4.3】继承中的构造函数与析构函数。 using System; public class BaseLifeSample public static void Main() Son s1 = new Son(); public class Grandsire,【例4.3

12、】, public Grandsire() Console.WriteLine(调用Grandsire的构造函数); Grandsire() Console.WriteLine(调用Grandsire的析构函数); public class Father : Grandsire public Father() Console.WriteLine(调用Father的构造函数); Father() Console.WriteLine(调用Father的析构函数); ,【例4.3】,public class Son : Father public Son() Console.WriteLine(调用

13、Son的构造函数); Son() Console.WriteLine(调用Son的析构函数); 运行结果如图4.4所示。,图4.4 运行结果,4.1.1 继承,4. System.Object类 C#所有类都派生于System.Object类。在定义类时如果没有指定派生于哪一个类,系统就默认其派生于Object类。例4.2中Shape的定义就等同于: public class Shape :System.Object System.Object类常见的公有方法是: Equals:如果两个对象具有相同值时,方法将返回true。 GetHashCode:方法返回对象的值的散列码。 ToString

14、:通过在派生类中重写该方法,返回一个表示对象状态的字符串。,4.1.2多态,多态也是面向对象语言的基本特征之一,是指在程序执行之前无法根据函数名和参数确定调用哪一个操作,而是程序执行过程中,根据实际运行情况动态确定,从而带来编程高度的灵活性。实现多态的方法是使用虚方法。 1. 虚方法的重载 在类的方法前加上关键字virtual, 则该方法被称为虚方法。通过对虚方法的重载,实现在程序运行过程中确定调用的方法。需要注意的是这里所讲的重载与第3章所讲的通过参数类型与参数个数的不同实现的重载含义是不同的。 【例4.4】虚方法的重载。 using System; class A public void

15、F() Console.WriteLine(A.F); public virtual void G() Console.WriteLine(A.G); ,【例4.4】,class B: A new public void F() Console.WriteLine(B.F); public override void G() Console.WriteLine(B.G); class Test static void Main() B b = new B(); A a = b; a.F(); b.F(); a.G(); b.G(); ,【例4.4】,在A 类定义了提供了非虚的F和虚方法 G,

16、派生类B 则对方法F实现覆盖,对虚方法G 实现了虚方法的的重载。“A a = b”实际上a仍旧是一个b对象。输出结果为: A.F B.F B.G B.G 在例4.1中计算圆形和矩形的面积分用了两个不同的方法GetArea和AreaIs,也可以通过 虚方法实现。将Shape中增加虚方法: public virtual double GetArea() return 0.0; 在Circle类中对虚方法重载: public override double GetArea() return System.Math.PI * radius * radius; 在Rectangular类中对虚方法重载:

17、 public override double GetArea() return Length*Width; ,4.1.2多态,【例4.5】用虚方法的实现重载 using System; /定义基类Shape public class Shape protected string Color; public Shape() ; public Shape(string Color) this.Color = Color; public string GetColor() return Color; public virtual double GetArea() return 0.0; ,【例4.

18、5】,/定义Circle类,从Shape类中派生 public class Circle : Shape private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public override double GetArea() return System.Math.PI * Radius * Radius; / 派生类Rectangular,从Shape类中派生 public class Rectangular:Shape,【例4.5】

19、, protected double Length, Width; public Rectangular(string Color,double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public override double GetArea() return Length*Width; public double PerimeterIs() / 周长 return (2*(Length+Width); ,【例4.5】,/ 派生类Square,从 Rectangu

20、lar类中派生 public class Square: Rectangular public Square(string Color,double Side):base(Color,Side,Side) ; public class TestInheritance public static void Main() Circle Cir= new Circle(orange,3.0); Console.WriteLine(Circle color is 0,Circle area is 1, Cir.GetColor(),Cir.GetArea(); Rectangular Rect= ne

21、w Rectangular(red,13.0, 2.0); Console.WriteLine(Rectangualr color is 0,Rectangualr area is 1, Rectangular perimeter is,【例4.5】,2, Rect.GetColor(),Rect.GetArea(), Rect.PerimeterIs(); Square Squ= new Square(green,5.0); Console.WriteLine(Square color is 0,Square Area is 1, Square perimeter is 2, Squ.Get

22、Color(),Squ.GetArea(), Squ.PerimeterIs(); Shape Shp= Cir; Console.WriteLine(Circle area is 0, Shp.GetArea(); Shp = Rect; Console.WriteLine(Rectangualr area is 0,Shp.GetArea(); ,4.1.2多态,2. 抽象类与抽象方法 抽象类是一种特殊的基类,并不与具体的事物联系。抽象类的定义使用关键字abstract。在 图4.2 类的层次结构中,并没有“图形”这样具体事物,所以可以将“图形”定义为抽象类,派 生了“圆形”和“四边形”这

23、样一些可以产生具体实例化的普通类。需要注意的是,抽象类是不 能被实例化,它只能作为其它类的基类。将Shape类定义为抽象类: public abstract class Shape 在抽象类中也可以使用关键字abstract定义抽象方法,要求所有的派生非抽象类都要重载实 现抽象方法。引入抽象方法的原因在于抽象类本身是一种抽象的概念,有的方法并不要具 体的实现,而是留下来让派生类来重载实现。Shape类中GetArea方法本身没什么具体的 意义,而只有到了派生类Circle类和Rectangular才可以计算具体的面积。 抽象方法写法: public abstract double GetAre

24、a(); 则派生类重载实现为: public override double GetArea() ,4.1.2多态,【例4.6】抽象类和抽象方法的实现 using System; /定义基类Shape public abstract class Shape protected string Color; public Shape() ; public Shape(string Color) this.Color = Color; public string GetColor() return Color; public abstract double GetArea(); ,【例4.6】,/

25、定义Circle类,从Shape类中派生 public class Circle : Shape private double Radius; public Circle(string Color, double Radius) this.Color = Color; this.Radius = Radius; public override double GetArea() return System.Math.PI * Radius * Radius; ,【例4.6】,/ 派生类Rectangular, 从Shape类中派生 public class Rectangular:Shape p

26、rotected double Length, Width; public Rectangular(string Color,double Length, double Width) this.Color = Color; this.Length = Length; this.Width = Width; public override double GetArea() return Length*Width; public double PerimeterIs() /周长 return (2*(Length+Width); ,【例4.6】,/ 派生类Square, 从 Rectangular

27、类中派生 public class Square: Rectangular public Square(string Color,double Side):base(Color,Side,Side) ; public class TestInheritance public static void Main() Circle Cir= new Circle(orange,3.0); Console.WriteLine(Circle color is 0,Circle area is 1, Cir.GetColor(),Cir.GetArea(); Rectangular Rect= new R

28、ectangular(red,13.0, 2.0);,【例4.6】,Console.WriteLine(Rectangualr color is 0,Rectangualr area is 1, Rectangular perimeter is 2, Rect.GetColor(),Rect.GetArea(), Rect.PerimeterIs(); Square Squ= new Square(green,5.0); Console.WriteLine(Square color is 0,Square Area is 1, Square perimeter is 2, Squ.GetCol

29、or(),Squ.GetArea(), Squ.PerimeterIs(); ,4.1.2多态,3. 密封类和密封方法 抽象类是作为基类,不能被实例化,由其它类继承。相对应的还有一种不能被其它类继承 的类,叫密封类,使用sealed关键字定义。如果Rectangular类定义为密封类 : public class rectangular:Shape 这样Rectangular类的派生类Square就不再保留,否则,就会出错。 如果类的方法声明包含 sealed 修饰符,称该方法为密封方法。类的实例方法声明包含 sealed 修饰符,则必须同时使用override修饰符。使用密封方法可以防止派生

30、类进一步重 写该方法。如果将圆形Circle类的GetArea方法定义为密封类,必须先将Shape类GetArea 方法定义为: public virtual double GetArea() 然后在Circle类中实现密封方法: public sealed override double GetArea() ,4.2 操作符重载,如果有一个复数Complex类对一元操作符“+”重载,可以写成: public static Complex operator +(Complex a) 对二元操作符“+”可以写成: public static Complex operator +(Complex

31、a, Complex b) ,4.2 操作符重载,一元操作符有一个参数,二元操作符有二个参数。重载操作符开始必须以public static修饰。可以重载的操作符包括: 一元操作符:+ - ! + - true false 二元操作符:+ - * / % return x.Sidey.Side; / 重载” ”操作符 public static bool operator (Square x,Square y) Console.WriteLine(重载操作符,两个参数都为square类); return x.Sidey.Side; / 重载” = ”操作符 public static bool

32、 operator =(Square x,Square y) Console.WriteLine(重载=操作符,两个参数都为square类); return (x=(Square x,Square y) Console.WriteLine(重载=操作符,两个参数都为square类); return (xy) | (x=y); /调用重载的操作符” =”和” ” public static void Main() Square s1=new Square(10); Square s2=new Square(20); Square s3=s1+s2; / 调用operator + (Square,

33、Square) Console.WriteLine(s3); / 调用重写object类的ToString()方法 Console.WriteLine(s3+15); / 调用重写的operator + (Square,int)以及ToString() Console.WriteLine(s3+1.5); / 调用重写的operator + (Square,double)和ToString() s3=10; / 调用隐式转换public static implicit operator Square(int ) Console.WriteLine(s3); Square s4=10;,【例4.

34、8】,Console.WriteLine(s1=s4); / 调用= 操作符 Console.WriteLine(s1!=s4); / 调用 != 操作符 Console.WriteLine(s1s2); / 调用 操作符 Console.WriteLine(s1=s4); / 调用=操作符 运行结果如图4.6所示。 Square与double 和int 之间有两个隐式转换。从double以及int隐式转换过程 不会丢失数据而出现异常,所以采用了隐式转换。 Square类默认的基类是Object类,所以可以重载Object类的方法ToString。 WriteLine方法默认的参数为Strin

35、g对象,所以在输出之前都要调用ToString方 法,将参数转换为String类型。,【例4.8】,图4.6 运行结果,4.3 类型转换,实际编程中,经常用到类型的相互之间转换问题,如一个int类型要转换成一个long类型。类型转换方法分为隐式类型转换和显式类型转换,也可以采用Convert提供的方法实现类型转换。 4.3.1 隐式类型转换 隐式类型转换不需要加任何声明就可以实现的类型转换,规则简单。 1. 隐式数值转换 数值转换是指在整数类型、实数类型和字符类型之间的转换。sbyte类型向int类型转换是一种隐式数值类型转换,转换一般不会失败,也不会丢失数据。如: sbyte a = 100

36、; int b = a; 隐式数值类型转换如表4.1所示。 从int到long,long到float、double等几种类型转换时可能导致精度的下降,但不导致数量上的丢失。从表4.1中可以看出,任何的原始类型,如果值的范围完全包含在其它类型的值范围内,那么就能进行隐式转换。需要注意的是char类型可以转换为其它的整数或实数类型,但不存在其它类型转换为char类型。,1. 隐式数值转换,表4.1 隐式数值类型转换,1. 隐式数值转换,【例4.9】隐式数值转换。 using System; class ReferenceConversion static void Main() char a =

37、m; int b = a; Console.WriteLine(a equals:0,a); Console.WriteLine(b equals:0,b); 运行结果如下: a equals:m b equals:107 如果这样写: int b = 7; char a = b; Console.WriteLine(a equals:0,a); Console.WriteLine(b equals:0,b); 编译器将报错:无法将类型“int”隐式转换为“char”。,4.3.1 隐式类型转换,2. 隐式枚举转换 隐式枚举转换只允许将十进制0转换为枚举类型的变量。 【例4.10】隐式枚举转换

38、。 using System; enum Color Red,Green,Blue class EnumConversion static void Main() Color a = Color.Red; Console.WriteLine(a equals:0,a); a = 0; Console.WriteLine(a equals:0,a); 如果写a = 1或其它数值,编译器提示:无法将类型“int”隐式转换为“Color”。,4.3.1 隐式类型转换,3. 隐式引用转换 类型s向类型t隐式引用转换的条件是:s是从t派生来的,且s和t可以是接口或类。两个数组的之间的隐式转换的条件是:两

39、个数组的维数相同,元素都是引用类型,且存在数组元素的隐式引用转换。例如: class Employee / 隐含继承自System.Object class App Employee e; object o = e; 使用隐式转换的因为在于Employee是派生自Object类。,4.3.2 显式类型转换,显式类型转换只有在某些情况下实现转换,规则复杂,需要用户正确指定要转换的类型。又称强制类型转换。 1. 显式数值转换 int类型向byte类型转换就是一种显式数值类型转换。 例如: int b = 100; sbyte a =(byte)b; sbyte取值范围是0255,当int b显式转

40、换为sbyte时不会丢失信息。 int b = 1000; sbyte a =(byte)b; 则会出信息丢失,这是显式数值转换过程要注意的。 显式数值类型可转换的类型如表4.2。,1. 显式数值转换,表4.2 显式数值类型转换,4.3.2 显式类型转换,2. 显式枚举转换 显式枚举转换包括几种情况:从 sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal类型到任何枚举类型;从任何枚举类型到 sbyte, byte, short, ushort, int, uint, long, ulo

41、ng, char, float,double, decimal;从任何枚举类型到任何其它枚举类型。显式枚举转换其本质是枚举类型的元素类型与要转换的类型之间的显式转换。 【例4.11】显式枚举转换。 using System; enum Color Red, Green = 10, Blue class Test static void Main(),【例4.11】, Console.WriteLine(StringFromColor(Color.Red); Console.WriteLine(StringFromColor(Color.Green); Console.WriteLine(Str

42、ingFromColor(Color.Blue); / 枚举类型向整数类型显式转换 static string StringFromColor(Color c) switch (c) case Color.Red: / 将指定的String中的每个格式项替换为相应对象的 值的文本等效项。 return String.Format(Red = 0, (int) c); case Color.Green: return String.Format(Green = 0, (int) c); case Color.Blue: return String.Format(Blue = 0, (int) c

43、);,【例4.11】,default: return Invalid color; 运行结果如下: Red = 0 Green = 10 Blue = 11,4.3.2 显式类型转换,3. 显式引用转换 类型s向类型t显式引用转换的条件是t是从s派生来的,且s和t可以是接口或类。两个数组的之间的隐式转换的条件是两个数组的维数相同,元素都是引用类型,且存在数组元素的显式引用转换。 例如: class Employee / 隐含继承自System.Object class App object o Employee e = (Employee)o; ,4.3.3 使用Convert转换,Syste

44、m.Convert类中有一套的静态方法实现类型的转换,即使要转换的类型之间没有什么联系也可以很方便的实现类型的转换。包括的方法列在表4.3中。,表4.3 Convert类转换的方法,4.3.3 使用Convert转换,【例4.12】System.Convert实现类型转换。 using System; class TestConvert static void Main() short a; try Console.WriteLine(Enter a string:); a = Convert.ToInt16(Console.ReadLine(); Console.WriteLine(a eq

45、uals:0,a); catch (FormatException) Console.WriteLine(字符串不是由数字组成或为空.); catch (OverflowException) Console.WriteLine(字符串的数值超出了short 范围.); ,4.4 结构与接口,4.4.1 结构 C#中的结构除了包含有数据成员,还有构造函数、方法、属性、事件、索引等成员,结构也可以实现多个接口。结构与类很类似,但也有很多的区别。首先结构是值类型,而类是引用类型。 【例4.13】值类型的结构。 using System; / 定义结构 MyStruct struct MyStruct

46、 / 定义字段x,y public int x; public int y; / 定义构造函数 public MyStruct(int i, int j) x = i; y = j; ,【例4.13】,/ 定义方法 public void Sum() int sum = x + y; Console.WriteLine(The sum is 0,sum); class Class1 static void Main() MyStruct s1 = new MyStruct(1,2); MyStruct s2 = s1; s1.x = 2; s1.Sum(); s2.Sum(); ,【例4.13

47、】,运行结果如下: The sum is 4 The sum is 3 程序中的s2获得了s1的数据一份拷贝,虽然s1.x值改变了,但并没有影响到s2。当两个类 的实例相等时,则表示它们指向同一段的内存地址,试图改变一个必然要影响到另一个。 结构实例化时也可以不用new。例如,Class1也可以写成: class Class1 static void Main() MyStruct s1; s1.x = 1; s1.y = 2; MyStruct s2 = s1; s1.x = 2; s1.Sum(); s2.Sum(); ,【例4.13】,如果在Main()加入: MyStruct s3 =

48、 new MyStruct(); s3.Sum(); 虽然结构中没有默认的无参构造函数,但是却可以调用无参构造函数,并且将值类型的数据成员设置为对应值类型的缺省值,将其引用类型的数据成员设置为null。所以s3.x和s3.y都为0,s3最终的和也为0。 结构与类的区别如表4.4所示。,表4.4 结构与类的区别,4.4.2接口,1. 接口介绍 接口是用来定义一种程序的协定。接口好比一种模版,这种模版定义了实现接口的对象必须实现的方法,其目的就是让这些方法可以作为接口实例被引用。接口的定义如: public interface IPartA void SetDataA(string dataA);

49、 接口使用关键字interface定义,接口可以使用的修饰符包括new,public,protected,internal,private等。接口的命名通常是以I开头,如IPartA,IPartB。接口的成员可以是方法、属性、索引器和事件,但不可以有任何的成员变量,也不能在接口中实现接口成员。接口不能被实例化。接口的成员默认是公共的,因此不允许成员加上修饰符。,4.4.2接口,【例4.14】接口演示。 using System; / 定义接口IPartA public interface IPartA void SetDataA(string dataA); / 定义接口IPartB,继承IP

50、artA public interface IPartB:IPartA void SetDataB(string dataB); / 定义类SharedClass,继承接口IPartB public class SharedClass :IPartB private string DataA; private string DataB;,【例4.14】,/ 实现接口IPartA的方法SetDataA public void SetDataA(string dataA) DataA=dataA; Console.WriteLine(0,DataA); / 实现接口IPartB的方法SetData

51、B public void SetDataB(string dataB) DataB=dataB; Console.WriteLine(0,DataB); ,【例4.14】,class test static void Main() SharedClass a = new SharedClass(); a.SetDataA(interface IPartA); a.SetDataB(interface IPartB); 运行结果如下: interface IPartA interface IPartB 程序中一共定义两个接口和一个类。接口IPartA定义方法SetDataA,接口IPartB定

52、义方法SetDataB。接口之间也有继承关系,接口IPartB继承接口IPartA,也就继承了接口IPartA的SetDataA方法。接口只能定义方法,实现要由类或者结构来完成。SharedClass类派生于接口IPartB,因此要实现IPartB的SetDataB方法,也要实现IPartA的SetDataA方法。,4.4.2接口,接口允许多重继承: interface ID:IA,IB,IC 类可以同时有一个基类和零个以上的接口,并要将基类写在前面: class ClassB:ClassA,IA,IB ,4.4.2接口,2. 接口的实现 指出接口成员所在的接口,则称为显式接口成员。上面程序接

53、口实现可改写成: / 没有定义为public void IPartA. SetDataA(string dataA) DataA=dataA; Console.WriteLine(0,DataA); / 没有定义为public void IPartB.SetDataB(string dataB) DataB=dataB; Console.WriteLine(0,DataB); ,2. 接口的实现,显式接口成员只能通过接口来调用。 class test static void Main() SharedClass a = new SharedClass(); IPartB partb = a;

54、partb.SetDataA(interface IPartA); partb.SetDataB(interface IPartB); 方法本身并不是类SharedClass提供,a.SetDataA(interface IPartA)或a.SetDataB(interface IPartB)调用都是错误的。显式接口成员没被声明为public,这是因为这些方法都有着双重的身份。当在一个类中使用显式接口成员时,该方法被认为是私有方法,因此不能用类的实例调用它。但是,当将类的引用转型为接口引用时,接口中定义的方法就可以被调用,这时它又成为了一个公有方法。,2. 接口的实现,【例4.15】显式接口调

55、用 using System; public interface IWindow Object GetMenu(); public interface IRestaurant Object GetMenu(); / 该类型继承自system.Object,并实现了IWindow和IRestaurant 接口 public class GiuseppePizzaria : IWindow, IRestaurant / 该方法包括了IWindow接口的GetMenu方法实现 Object IWindow.GetMenu() return IWindow.GetMenu; ,【例4.15】,/ 该方法包括了IRestaurant接口的GetMenu方法实现 Object IRestaurant.GetMenu() return IRestaurant.GetMenu; / 这个GetMenu方法与接口没有任何关系 public Object GetMenu

温馨提示

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

最新文档

评论

0/150

提交评论