您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 第3章面向对象技术6
第5讲继承5.1C#的继承机制5.1.1概述几个概念:基类,父类,派生类,子类,单继承,多继承。1、简单继承例:usingSystem;classVehicle//定义汽车类{intwheels;//公有成员轮子个数protectedfloatweight;//保护成员重量publicVehicle(){;}publicVehicle(intw,floatg){wheels=w;weight=g;}publicvoidSpeak(){Console.WriteLine(“thewvehicleisspeaking!”);}};classCar:Vehicle//定义轿车类从汽车类中继承{intwheels;//公有成员轮子个数protectedfloatweight;intpassengers;//私有成员乘客数publicCar(intw,floatg,intp):base(w,g){wheels=w;weight=g;passengers=p;}publicvoidSpeak(){Console.WriteLine(“thewvehicleisspeaking!”);}}C#中的继承符合下列规则:继承是可传递的如果C从B中派生,B又从A中派生。那么C不仅继承了B中声明的成员,同样也继承了A中的成员,Object类作为所有类的基类。派生类应当是对基类的扩展派生类可以添加新的成员,但不能除去已经继承的成员的定义。构造函数和析构函数不能被继承除此以外的其它成员,不论对它们定义了怎样的访问方式,都能被继承。基类中成员的访问方式只能决定派生类能否访问它们。派生类如果定义了与继承而来的成员同名的新成员,就可以覆盖已继承的成员。但这并不因为这派生类删除了这些成员,只是不能再访问这些成员。类可以定义虚方法虚属性以及虚索引指示器它的派生类能够重载这些成员,从而实现类可以展示出多态性。再看一个例子:usingSystem;usingSystem.Text;classPerson{privatestringfirstName;privatestringmiddleName;privatestringlastName;privateintage;publicPerson(){}publicPerson(stringfn,stringln){firstName=fn;lastName=ln;}publicPerson(stringfn,stringmn,stringln){firstName=fn;middleName=mn;lastName=ln;}publicPerson(stringfn,stringmn,stringln,inta){firstName=fn;middleName=mn;lastName=ln;age=a;}publicvoiddisplayAge(){Console.WriteLine(Age{0},age);}publicvoiddisplayFullName(){StringBuilderFullName=newStringBuilder();FullName.Append(firstName);FullName.Append();if(middleName!=){FullName.Append(middleName[0]);FullName.Append(.);}FullName.Append(lastName);Console.WriteLine(FullName);}}classNameApp{publicstaticvoidMain(){Personme=newPerson(Bradley,Lee,Jones);PersonmyWife=newPerson(Melissa,Anne,Jones,21);me.displayFullName();me.displayAge();myWife.displayFullName();myWife.displayAge();}}结果:BradlyL.JonesAge0MelissaA.JonesAge21继承usingSystem;usingSystem.Text;classPerson{protectedstringfirstName;protectedstringmiddleName;//不能访问基类中的私有成员protectedstringlastName;privateintage;//ToDo:AddpropertiestoaccessdatamemberspublicPerson(){}publicPerson(stringfn,stringln){firstName=fn;lastName=ln;}publicPerson(stringfn,stringmn,stringln){firstName=fn;middleName=mn;lastName=ln;}publicPerson(stringfn,stringmn,stringln,inta){firstName=fn;middleName=mn;lastName=ln;age=a;}publicvoiddisplayAge(){Console.WriteLine(Age{0},age);}publicvoiddisplayFullName(){StringBuilderFullName=newStringBuilder();FullName.Append(firstName);FullName.Append();if(middleName!=){FullName.Append(middleName[0]);FullName.Append(.);}FullName.Append(lastName);Console.WriteLine(FullName);}}classEmployee:Person{privateushorthYear;publicushorthireYear{get{return(hYear);}set{hYear=value;}}publicEmployee():base()//调用积累中的构造方法{}publicEmployee(stringfn,stringln):base(fn,ln){}publicEmployee(stringfn,stringmn,stringln,inta):base(fn,mn,ln,a){}publicEmployee(stringfn,stringln,ushorthy):base(fn,ln){hireYear=hy;}publicnewvoiddisplayFullName()//这里是覆盖{Console.WriteLine(Employee:{0}{1}{2},firstName,middleName,lastName);}}classNameApp{publicstaticvoidMain(){PersonmyWife=newPerson(Melissa,Anne,Jones,21);Employeeme=newEmployee(Bradley,L.,Jones,23);Employeeyou=newEmployee(Kyle,Rinni,2000);myWife.displayFullName();myWife.displayAge();me.displayFullName();Console.WriteLine(Yearhired:{0},me.hireYear);me.displayAge();you.displayFullName();Console.WriteLine(Yearhiredofhim:{0},you.hireYear);you.displayAge();}}2、使用基类的方法Publicnewvoiddsply(){Base.displayFullName();}实验一自己定义一个基类,派生一个子类,在子类中调用基类的成员方法;5.2多态性在面向对象的系统中,多态性是一个非常重要的概念,它允许客户对一个对象进行操作。由对象来完成一系列的动作,具体实现哪个动作,如何实现由系统负责解释。5.2.1C#中的多态性在C#中多态性的定义是:同一操作作用于不同的类的实例,不同的类将进行不同的解释,最后产生不同的执行结果。C#支持两种类型的多态性:编译时的多态性---------编译时的多态性是通过重载来实现的。运行时的多态性---------运行时的多态性就是指直到系统运行时,才根据实际情况决定实现何种操作,C#中,运行时的多态性通过虚成员实现。例:usingSystem;classPerson{protectedstringfirstName;protectedstringlastName;publicPerson(){}publicPerson(stringfn,stringln){firstName=fn;lastName=ln;}publicvoiddisplayFullName(){Console.WriteLine({0}{1},firstName,lastName);}}classEmployee:Person{publicushorthireYear;publicEmployee():base(){}publicEmployee(stringfn,stringln):base(fn,ln){}publicEmployee(stringfn,stringln,ushorthy):base(fn,ln){hireYear=hy;}publicnewvoiddisplayFullName(){Console.WriteLine(Employee:{0}{1},firstName,lastName);}}classNameApp{publicstaticvoidMain(){Employeeme=newEmployee(Bradley,Jones,1983);PersonBrad=me;me.displayFullName();Console.WriteLine(Yearhired:{0},me.hireYear);Brad.displayFullName();调用积累的方法实现}}//调用各自所属类的方法实验二测试Brad.displayFullName();是否调用了积累的方法5.2.2虚方法当类中的方法声明前加上了virtual修饰符,我们称之为虚方法,反之为非虚。使用了virtual修饰符后不允许再有static,abstract,或override修饰符。对于非虚的方法,无论被其所在类的实例调用,还是被这个类的派生类的实例调用。方法的执行方式不变,而对于虚方法,它的执行方式可以被派生类改变,这种改变是通过方法的重载来实现的。usingSystem;classA{publicvoidF(){Console.WriteLine(A.F);}publicvirtualvoidG(){Console.WriteLine(A.G);}}classB:A{newpublicvoidF(){Console.WriteLine(B.F);}publicoverridevoidG(){Console.WriteLine(B.G);}}classTest{staticvoidMain(){Bb=newB();Aa=b;a.F();b.F();a.G();b.G();//测试}}例子中A类提供了两个方法,非虚的F和虚方法G。类B则提供了一个新的非虚的方法F从而覆盖了继承的F,类B同时还重载了继承的方法G那么输出应该是A.FB.FB.GB.G实验三试写出程序的运行结果usingSystem;classPerson{protectedstringfirstName;protectedstringlastName;publicPerson(){}publicPerson(stringfn,stringln){f
本文标题:第3章面向对象技术6
链接地址:https://www.777doc.com/doc-2193839 .html