ava面向对象技术.ppt_第1页
ava面向对象技术.ppt_第2页
ava面向对象技术.ppt_第3页
ava面向对象技术.ppt_第4页
ava面向对象技术.ppt_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

Java语言程序设计 对象和类 抽象数据类型 基本数据类型属性操作自定义数据类型属性 数据成员 操作 函数成员 定义方法 函数和子程序voidreturn参数列表 值传递和地址传递 基本数据类型值传递其它数据类型值里存放的是地址地址传递 数据隐藏 隐藏的意义正确的访问封装 屏蔽实现的细节 便于改变实现隐藏的实现privatedefaultpublic this引用 指代当前对象获取当前对象的指针 classVariable intx 0 y 0 z 0 voidinit intx inty this x x this y y intz 5 System out println x x y y z z publicclassVariableTest publicstaticvoidmain String args Variablev newVariable System out println x v x v y v y z v z v init 20 30 System out println x v x v y v y z v z publicclassLeaf privateinti 0 Leafincrement i returnthis voidprint System out println i i publicstaticvoidmain String args Leafx newLeaf x increment increment increment print 重载方法名 方法名相同参数不同参数的个数不同参数的类型不同参数的类型次序不同不包括返回值 classMethodOverloading voidreceive inti System out println Receiveoneintdata voidreceive inti intj System out println Receivetwointdata voidreceive doubled System out println Receiveonedoubledata voidreceive Strings System out println ReceiveaString publicclassMethodOverloadingTest publicstaticvoidmain String args MethodOverloadingm newMethodOverloading m receive 1 m receive 1 2 m receive 3 14 m receive hello publicclassOverloadingOrder staticvoidprint Strings inti System out println String s int i staticvoidprint inti Strings System out println int i String s publicstaticvoidmain String args print Stringfirst 11 print 99 Intfirst 对象的构造和初始化 初始化和初始化方法构造方法 classRock Rock ThisistheconstructorSystem out println CreatingRock publicclassSimpleConstructor publicstaticvoidmain String args for inti 0 i 10 i newRock 对象的构造和初始化 重载构造方法 classRock Rock inti System out println CreatingRocknumber i publicclassSimpleConstructor publicstaticvoidmain String args for inti 0 i 10 i newRock i 对象的构造和初始化 默认构造方法如果已经定义了一个构建器 无论是否有自变量 编译程序都不会帮我们自动合成一个 finalize方法垃圾收集器 classPoint intx y Stringname apoint Point x 0 y 0 Point intx inty Stringname this x x this y y this name name intgetX returnx intgetY returny PointnewPoint Stringname PointnewP newPoint x y name returnnewP voidprint System out println name x x y y publicclassUsingObject publicstaticvoidmain String args Pointp newPoint p print Pointp1 newPoint 50 50 anewPoint p1 print System out println p1 x p1 y System out println p1 getX p1 getY p1 newPoint hello print newPoint 10 15 anothernewpoint print 子类 组合hasa继承isaextends关键字protected单重继承 classCleanser privateStrings newString Cleanser publicvoidappend Stringa s a publicvoiddilute append dilute publicvoidapply append apply publicvoidscrub append scrub publicvoidprint System out println s publicstaticvoidmain String args Cleanserx newCleanser x dilute x apply x scrub x print publicclassDetergentextendsCleanser publicvoidscrub append Detergent scrub super scrub publicvoidfoam append foam publicstaticvoidmain String args Detergentx newDetergent x dilute x apply x scrub x foam x print System out println Testingbaseclass Cleanser main args classArt Art System out println Artconstructor classDrawingextendsArt Drawing System out println Drawingconstructor publicclassCartoonextendsDrawing Cartoon System out println Cartoonconstructor publicstaticvoidmain String args Cartoonx newCartoon 多态性 向上映射instanceof运算符对象转换Shapes java 覆盖方法 继承的过程继承全部内容增加内容改变内容覆盖方法一致的声明 名称 参数 不低于基类的存取权限 java包 分割系统包package包的命名原则导入import与include环境变量 Java语言程序设计 异常处理 异常 什么是异常正常情况之外的一种 异常 在问题发生的时候 我们可能不知具体该如何解决 但肯定知道已不能不顾一切地继续下去 此时 必须坚决地停下来 并由某人 某地指出发生了什么事情 以及该采取何种对策 异常的分类 异常示例 publicclassHelloWorld publicstaticvoidmain Stringargs inti 0 Stringgreeting Helloworld No Imeanit HELLOWORD while i 4 System out println greeting i i 异常处理 try块若位于一个方法内部 并 掷 出一个违例 或在这个方法内部调用的另一个方法产生了违例 那个方法就会在违例产生过程中退出 若不想离开方法 可在那个方法内部设置一个特殊的代码块 用它捕获违例 这就叫作 try块 因为要在这个地方 尝试 各种方法调用 try块属于一种普通的作用域 用一个try关键字开头 try 可能产生违例的代码 异常处理 catch生成的违例必须在某个地方中止 这个 地方 便是违例控制器或者违例控制模块 而且针对想捕获的每种违例类型 都必须有一个相应的违例控制器 违例控制器紧接在try块后面 且用catch 捕获 关键字标记 如下所示 try Codethatmightgenerateexceptions catch Type1id1 HandleexceptionsofType1 catch Type2id2 HandleexceptionsofType2 catch Type3id3 HandleexceptionsofType3 异常处理 捕获所有违例catch Exceptione 这段代码能捕获任何违例 所以在实际使用时最好将其置于控制器列表的末尾 防止跟随在后面的任何特殊违例控制器失效 publicclassExceptionMethods publicstaticvoidmain String args try thrownewException Here smyException catch Exceptione System out println CaughtException System out println e getMessage e getMessage System out println e toString e toString System out println e printStackTrace e printStackTrace 异常处理 finally无论一个违例是否在try块中发生 我们经常都想执行一些特定的代码 为达到这个目的 可在所有违例控制器的末尾使用一个finally从句 publicclassFinallyWorks staticintcount 0 publicstaticvoidmain String args while true try post incrementiszerofirsttime if count 0 thrownewException System out println Noexception catch Exceptione System out println Exceptionthrown finally System out println infinallyclause if count 2 break outof while 常见异常 ArithmetricExceptionNullPointerExceptionNegativeArraySizeExceptionArrayIndexOutOfBoundsExceptionSecurityExceptionIOException 抛出异常 异常的处理机制违例机制的一项好处就是能够简化错误控制代码 我们再也不用检查一个特定的错误 然后在程序的多处地方对其进行控制 此外 也不需要在方法调用的时候检查错误 因为保证有人能捕获这里的错误 我们只需要在一个地方处理问题 违例控制模块 或者 违例控制器 这样可有效减少代码量 并将那些用于描述具体操作的代码与专门纠正错误的代码分隔开 抛出异常 创建自己的异常 为什么要创建异常创建异常代码为创建自己的违例类 必须从一个现有的违例类型继承 最好在含义上与新违例近似 产生异常thrownewmyExeption classMyExceptionextendsException publicMyException publicMyException Stringmsg super msg publicclassInheriting publicstaticvoidf throwsMyException System out println T

温馨提示

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

评论

0/150

提交评论