java大学教程ppt07

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1JAVA语言程序设计周敏彤zhoumintong@suda.edu.cn2第六讲对象和类(续)对象的创建对象的使用对象的释放对象的访问3QuizclassPC{publicstaticvoidmain(String[]args){Mousem=newMouse();inti=m.click();System.out.println(i=+i);System.out.println(“Mouse‘si=”+m.i);}}publicclassMouse{inti;intclick(){returni++;}}4第七讲对象和类(续)类的继承(extends)抽象类(abstractclass)静态变量的初始化接口(interface)包(package)5类的继承父类与子类的关系(extends)基本内容1.子类可调用父类的方法和变量,子类可增加父类中没有的方法和变量2.子类可重新定义父类的静态/实例变量3.子类可重新定义父类的静态/实例方法4.继承中的构造方法5.类成员访问修饰符与继承的关系6类的继承子类可调用父类的方法和变量子类可增加父类中没有的方法和变量classVehicle{Stringbrand;voidsetB(Strings){brand=s;}voidshowB(){System.out.println(brand);}}classBusextendsVehicle{intgas;voidsetG(intg){gas=g;}voidshowG(){System.out.println(gas);}}Busb=newBus();b.setB(“audi”);//**b.setG(100);b.showB();//**b.showG();子类继承父类的方法和变量,则这些方法和变量就属于子类,则子类对象对这些方法和变量的调用是显而易见的7类的继承子类可重新定义父类中已有的变量父类中同名的变量无效(隐藏)通过“super.变量名”和父类名.变量名(static变量)引用classA{inti=256,j=64;staticintk=32;finalfloate=2.718f;}classBextendsA{publiccharj=‘x’;finaldoublek=5;staticinte=321;voidshow(){System.out.println(i+““+j+““+k+““+e);}voidshowA(){System.out.println(super.j+““+A.k+““+super.e);}}Bb=newB();b.show();b.showA();256x5.032164322.718this.变量名this.方法名this()super.变量名super.方法名super()super?当前对象/当前对象的父对象/其他8继承中的super对象类的继承父类子类实例化一个子类对象thissuper调用父类的变量和方法调用子类的变量和方法9继承中的构造方法类的继承publicclassCartoonextendsDrawing{Cartoon(){System.out.println(CartoonConstructor);}publicstaticvoidmain(Stringargs[]){Cartoonc=newCartoon();}}classArt{Art(){System.out.println(ArtConstructor);}}classDrawingextendsArt{Drawing(){System.out.println(DrawingConstructor);}}ArtConstructorDrawingConstructorCartoonConstructor子类的构造方法必须调用父类的构造方法classDrawingextendsArt{/*Drawing(){System.out.println(DrawingConstructor);}*/}ArtConstructorCartoonConstructorpublicclassCartoonextendsDrawing{Cartoon(){super();System.out.println(CartoonConstructor);}publicstaticvoidmain(Stringargs[]){Cartoonc=newCartoon();}}10再谈继承中的构造方法类的继承publicclassChessextendsBoardGame{Chess(){super();System.out.println(CartoonConstructor);}publicstaticvoidmain(Stringargs[]){Chessc=newChess();}}classGame{Game(){System.out.println(“GameConstructor);}}classBoardGameextendsGame{BoardGame(){super();System.out.println(“BoardGameConstructor);}}子类的构造方法必须要对父类的构造方法进行调用,不管以任何形式,否则编译出错super()方法必须是构造方法的第一句11再谈继承中的构造方法类的继承classA{A(inti){}}classBextendsA{B(Strings){}}B(Strings){super();}12类的继承子类可重新定义父类中已有的静态变量(static)classPoint{staticintx=2;}classTPointextendsPoint{staticdoublex=4.7;publicstaticvoidmain(String[]args){newTPoint().printX();}voidprintX(){System.out.println(x++Point.x);}}4.7213类的继承子类可重新定义父类中已有的实例变量classPoint{intx=2;}classTPointextendsPoint{doublex=4.7;voidprintBoth(){System.out.println(x++super.x);}publicstaticvoidmain(String[]args){TPointt=newTPoint();t.printBoth();System.out.println(t.x++((Point)t).x);}}4.724.7214类的继承再谈子类对父类变量的继承importpoints.Point3d;classPoint4dextendsPoint3d{intw;publicvoidmove(intdx,intdy,intdz,intdw){x+=dx;y+=dy;z+=dz;w+=dw;}}packagepoints;publicclassPoint{intx,y;publicvoidmove(intdx,intdy){x+=dx;y+=dy;}}packagepoints;publicclassPoint3dextendsPoint{intz;publicvoidmove(intdx,intdy,intdz){x+=dx;y+=dy;z+=dz;}}目录结构/points/Point.class/points/Point3d.class/Point4d.classimportpoints.Point3d;classPoint4dextendsPoint3d{intw;publicvoidmove(intdx,intdy,intdz,intdw){super.move(dx,dy,dz);w+=dw;}}编译时报错:x,y,z仅在本包中才能访问继承与类成员的访问修饰符有关!15类的继承类成员访问修饰符与继承的关系classPoint{intx,y;privatePoint(){reset();}Point(intx,inty){this.x=x;this.y=y;}privatevoidreset(){this.x=0;this.y=0;}}classColoredPointextendsPoint{intcolor;voidclear(){reset();}}classTest{publicstaticvoidmain(String[]args){ColoredPointc=newColoredPoint(0,0);c.reset();}}AnerroroccursbecauseColoredPointhasnoconstructordeclaredwithtwointegerparameters,asrequestedbytheuseinmain.ThisillustratesthefactthatColoredPointdoesnotinherittheconstructorsofitssuperclassPoint.AnothererroroccursbecauseColoredPointdeclaresnoconstructors,andthereforeadefaultconstructorforitisautomaticallycreated,andthisdefaultconstructorisequivalentto:ColoredPoint(){super();}whichinvokestheconstructor,withnoarguments,forthedirectsuperclassoftheclassColoredPoint.TheerroristhattheconstructorforPointthattakesnoargumentsisprivate,andthereforeisnotaccessibleoutsidetheclassPoint.TwomoreerrorsoccurbecausethemethodresetofclassPointisprivate,andthereforeisnotinheritedbyclassColoredPoint.16类的继承类成员访问修饰符与继承的关系私有的(private)类成员不能被子类继承公共的(public)和保护性的(protected)类成员能被子类继承,且子类和父类可以属于不同的包无修饰的父类成员,仅在本包中才能被子类继承构造函数不是类成员,所以不被继承packagepoints;publicclassPoint{publicintx,y;protectedintuseCount=0;staticprotectedinttotalUseCount=0;publicvoidmove(intdx,intdy){x+=dx;y+=dy;useCount++;totalUseCount++;}}importpoints.Point;classTestextendsPoint{publicvoidmoveBack(intdx,intdy){x-=dx;y-=dy;useCount++;totalUseCount++;}}17类的继承例classPosition{intx,y;Position(intpx,intpy){x=px;y=py;}voidshow(){System.out.println(x+”,”+y);}classSpotextendsPosition{booleandisplay;Spot(intsx,intsy,booleansf){super(sx,sy);display=sf;}voidchange(intcx,intcy){x=cx;y=cy;}}classBoxextendsSpot{intr;Box(intbx,intby,intbr,booleanbf

1 / 49
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功