您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > java大学教程ppt08
1JAVA语言程序设计周敏彤zhoumintong@suda.edu.cn2第七讲对象和类(续)类的继承父类和子类的关系super继承中的构造方法继承与类成员的访问修饰符3小结类的继承重写:子类和父类的方法名、返回类型和参数相同,与重载(overload)的区别如果是实例方法,则称为子类重写(overriding)父类的实例方法如果是类方法,则称为子类隐藏父类的类方法(static)实例方法不能重写静态方法静态方法不能重写实例方法final/private方法不能被重写或隐藏注:实例变量可隐藏静态变量静态变量可隐藏实例变量4类的继承子类对象与父类对象Classroomc=newMMClassroom();(等价于)MMClassroomc1=newMMClassroom();Classroomc=c1;子类对象可以被视为其父类的一个对象父类对象不能当作某一个子类对象ClassroomMMClassroom5第七讲对象和类(续)抽象类抽象方法多态性静态变量的初始化类的静态变量只有一份(存储空间)单子(singleton)设计模式6第七讲对象和类(续)接口定义常量和抽象方法多态性接口的继承(多个接口合并)命名冲突多重继承class类名extends父类implements接口1,接口27第七讲对象和类(续)类的继承(extends)抽象类(abstractclass)静态变量的初始化接口(interface)包(package)8为什么需要包?使Java类更容易发现和使用防止命名冲突进行访问控制层次结构,特定的功能Apackageisacollectionofrelatedclassesandinterfacesprovidingaccessprotectionandnamespacemanagement.import包(package)9包(package)//Graphic.javafilepublicabstractclassGraphic{...}//Circle.javafilepublicclassCircleextendsGraphicimplementsDraggable{...}//Rectangle.javafilepublicclassRectangleextendsGraphicimplementsDraggable{...}//Draggable.javafilepublicinterfaceDraggable{...}容易地决定那些类和接口是相互关联的知道从哪里找到提供图形功能的类和接口由于包建立了一个新的名字空间,所以你定义的类不会同其他包中的类名有冲突可以容许在同一个包中无访问限制,同时可对在本包外的访问进行限制10包的创建packagegraphics;publicclassCircleextendsGraphicimplementsDraggable{...}包的命名层次结构对应实际的目录结构com.sun.org.w3c.org.jalpha.包(package)11包的使用仅仅公共的(public)包成员(类、接口)可以在其所定义的包外被访问三种方式利用包成员的规范名(包名+类名)引入(import)包成员名引入(import)整个包成员包(package)12例packagegraphics;publicclassCircleextendsGraphicimplementsDraggable{...}利用包成员的规范名(包名+类名)graphics.CirclemyCir=newgraphics.Circle();引入(import)包成员名importgraphics.Circle;……CirclemyCir=newCircle();引入(import)整个包成员importgraphics.*;……CirclemyCir=newCircle();包(package)13如何防止名字冲突//graphics.Circle.classpackagegraphics;publicclassCircle{...}//mygraphics.Circle.classpackagemygraphics;publicclassCircle{...}包(package)importgraphics.*;importmygraphics.*;classTest{//Circlec;}importgraphics.*;importmygraphics.*;classTest{……graphics.Circlec=newgraphics.Circle();mygraphics.Circlec=newmygraphics.Circle();……}14包与Java文件的对应关系包(package)packageorg.jalpha;publicclassHelloWorld{...}根目录d:\src源文件d:\src\org\jalpha\HelloWorld.java编译cdd:\srcjavacorg\jalpha\HelloWorld.javaclass文件d:\src\org\jalpha\HelloWorld.class执行(在根目录)cdd:\srcjavaorg.jalpha.HelloWorld执行(在其他目录)d:\java–classpathd:\srcorg.jalpha.HelloWorld15包与Java文件的对应关系包(package)packageorg.aloha;importorg.jalpha.*;importorg.w3c.*;publicclassTest{...}class文件(org.jalpha)d:\src1\org\jalpha\*.classclass文件(org.w3c)d:\src2\org\w3c\*.classTest.class文件的根目录d:\src执行(在根目录)d:\srcjava–classpathd:\src1;d:\src2;.org.aloha.Test执行(在其他目录)d:\java–classpathd:\src1;d:\src2;d:\srcorg.aloha.Test16Quiz1classSuper{static{System.out.print(Super);}}classOne{static{System.out.print(One);}}classTwoextendsSuper{static{System.out.print(Two);}}classTest1{publicstaticvoidmain(String[]args){Oneo=null;Twot=newTwo();System.out.println((Object)o==(Object)t);}}运行结果:SuperTwofalseTheclassOneisneverinitialized,becauseitnotusedactivelyandthereforeisneverlinkedto.TheclassTwoisinitializedonlyafteritssuperclassSuperhasbeeninitialized.17Quiz2classSuper{staticinttaxi=1729;}classSubextendsSuper{static{System.out.print(Sub);}}classTest2{publicstaticvoidmain(String[]args){System.out.println(Sub.taxi);}}运行结果:1729becausetheclassSubisneverinitialized;thereferencetoSub.taxiisareferencetoafieldactuallydeclaredinclassSuperanddoesnottriggerinitializationoftheclassSub.18Quiz3classSuper{Super(){printThree();}voidprintThree(){System.out.println(“three”);}}classTest4extendsSuper{intthree=(int)Math.PI;publicstaticvoidmain(String[]args){Test4t=newTest4();t.printThree();}voidprintThree(){System.out.println(three);}}运行结果:03TheinvocationofprintThreeintheconstructorforclassSuperdoesnotinvokethedefinitionofprintThreeinclassSuper,butratherinvokestheoverridingdefinitionofprintThreeinclassTest.ThismethodthereforerunsbeforethefieldinitializersofTesthavebeenexecuted,whichiswhythefirstvalueoutputis0,thedefaultvaluetowhichthefieldthreeofTestisinitialized.19软件程序肯定会发生错误/问题whatreallymattersiswhathappensaftertheerroroccurs.Howistheerrorhandled?Whohandlesit?Cantheprogramrecover,orshoulditjustdie?从外部问题(硬盘、网络故障等)到编程错误(数组越界、引用空对象等)Java语言利用异常来使程序获得处理错误的能力(error-handling)异常事件(ExceptionalEvent)Anexceptionisaneventthatoccursduringtheexecutionofaprogramthatdisruptsthenormalflowofinstructions.第八讲异常(Exception)20当一个Java程序的方法产生一个错误,该方法创造一个异常对象并将其交给运行系统InJavaterminology,creatinganexceptionobjectandhandingittotheruntimesystemiscalledthrowinganexception(抛出异常)运行系统从错误发生处开始寻找处理错误的程序段Theexceptionhandlerchosenissaidtocatchtheexception(捕获异常)捕获异常的过程可以沿方法调用的逆向顺序寻找异常(Exception)21为什么采用异常(Exception)隔绝正常代码和错误处理代码异常(Exception)readFile{openthefile;determineitssize;allocatethatmuchmemory;readthefileintomemory;closethefile;}潜在的问题:Whathappensifthefilecan'tbeopened?Whathappensifthelengthofthefilecan'tbedetermined?Whathappensifenoughmemorycan'tbeallocated?Whathappensifthereadfails?Whathappensifthefilecan'tbeclosed?readFile{try{openthefile;determineitssize;allocatethatmuchmemory;
本文标题:java大学教程ppt08
链接地址:https://www.777doc.com/doc-7014929 .html