《JAVA语言程序设计》第3章.ppt

上传人:max****ui 文档编号:10979735 上传时间:2020-04-16 格式:PPT 页数:26 大小:286.50KB
返回 下载 相关 举报
《JAVA语言程序设计》第3章.ppt_第1页
第1页 / 共26页
《JAVA语言程序设计》第3章.ppt_第2页
第2页 / 共26页
《JAVA语言程序设计》第3章.ppt_第3页
第3页 / 共26页
点击查看更多>>
资源描述
1 对象和类 续 对象的创建对象的使用对象的释放对象的访问 2 对象的创建 对象成员 变量和方法 静态 static 成员 属于类实例成员 属于对象创建对象 实例化对象 new例1 Applea newApple 创建对象 例2 Applea 对象的说明 a newApple 实例化对象 对象的实例化通过构造方法 constructor 来实现构造方法的名字与类名相同构造方法没有返回值构造方法可以有多个 构成方法的重载 overload 3 例 对象的实例化和初始化 计算结果 102020501020 对象的创建 4 默认构造方法例classApple intcolor Applea newApple 对象实例的判断 null例Applea if a null System out println Daydream 对象的创建 运行时系统自动赋予一个空构造函数如publicApple 5 再谈构造方法 对象的创建 classMyTest MyTest booleanb publicstaticvoidmain Stringargs MyTestm1 newMyTest MyTestm2 newMyTest false classMyTest MyTest booleanb MyTest publicstaticvoidmain Stringargs MyTestm1 newMyTest MyTestm2 newMyTest false 运行时系统自动赋予一个空构造方法 仅仅当该类没定义构造方法的情况下 6 对象和类 续 对象的创建对象的使用对象的释放对象的访问 7 对象的使用 通过对象引用对象的成员变量和成员方法 classSquare inta h Square a 10 h 20 Square intx inty a x h y Square Squarer a r width h r height intwidth returna intheight returnh voidset intx inty a x h y q1 set 30 40 q1 a 30 q1 h 40 目的相同第一方式更安全 更面向对象 数据封装 避免直接操纵变量 8 对象的使用 引用对象的变量格式 对象名 变量名引用对象的方法格式 对象名 方法名例1Vectorv newVector v addElement s1 例2inta 1 2 3 4 5 intsize a length 例3System out println 9 对象和类 续 对象的创建对象的使用对象的释放对象的访问 10 对象的释放 将对象从内存中清除内存的管理 枯燥 容易出错 垃圾回收 GarbageCollection TheJavaplatformallowsyoutocreateasmanyobjectsasyouwant limited ofcourse bywhatyoursystemcanhandle andyoudon thavetoworryaboutdestroyingthem TheJavaruntimeenvironmentdeletesobjectswhenitdeterminesthattheyarenolongerbeingused Thisprocessiscalledgarbagecollection 11 对象的释放 垃圾搜集器 GarbageCollector 周期性地释放不再被引用的对象 自动完成手动完成 System gc java lang System中 publicstaticvoidgc Runsthegarbagecollector 12 对象和类 续 对象的创建对象的使用对象的释放对象的访问 13 对象的访问 访问对象的私有 private 成员通过定义一个公共方法来实现 classStudent privateStringname privateStringid Student Strings1 Strings2 name s1 id s2 StringgetName returnname voidsetName Strings name s Studentst newStudent 李忠 001 Strings st getName st setName 李晓 s st getName 14 对象的访问 对象作为方法的参数 访问权限修饰符 方法返回类型方法名 参数 throws异常名 方法体 参数 类型变量名 类型 基本数据类型 复合类型 对象 参数的传递Passbyvalue 15 例对象用作方法的参数 对象的访问 D javaTests点的坐标 2 3s点的坐标 6 8 16 例对象用作方法的参数 对象的访问 D javaTests点的坐标 2 3s点的坐标 6 8 17 例对象用作方法的参数 对象的访问 D javaTests点的坐标 2 3s点的坐标 6 8 18 对象的访问 对象的访问对象作为方法的返回值 访问权限修饰符 方法返回类型方法名 参数 throws异常名 方法体 返回类型有返回值 基本数据类型 复合类型 对象 无返回值 void 19 对象的访问 对象作为方法的返回值例 求两点坐标之间的中点坐标思路 x1 y1 和 x2 y2 x y x x1 x2 2 y y1 y2 2Spots1 newSpot 2 3 Spots2 newSpot 4 5 Spots s1 midSpot s2 20 例对象用作方法的返回值 对象的访问 D javaTests1点的坐标 3 0 5 0s2点的坐标 6 0 8 0中点的坐标 4 5 6 5 21 对象的访问 数组 类型相同的一列元素作为一个对象看待 publicclassArrayDemo publicstaticvoidmain String args int anArray newint 10 for inti 0 i anArray length i anArray i i System out print anArray i System out println 22 对象的访问 对象数组 classStudent privateStringname privateStringid Student Strings1 Strings2 name s1 id s2 StringgetName returnname voidsetName Strings name s voiddisplay System out println name id Studentst newStudent 10 for inti 0 i st length i st i newStudent for inti 0 i st length i st i display 23 对象的访问 对象作为另一个对象的成员变量一个对象中包含另一个对象 组合关系 classMobilePhone privateStringtype privateWatchw MobilePhone Strings type s voidsetWatch Watcha w a longgetTime returnw getTime classWatch longgetTime returnSystem currentTimeMillis MobilePhonemp newMobilePhone nokia Watchw newWatch mp setWatch w longl mp getTime publicstaticlongcurrentTimeMillis thedifference measuredinmilliseconds betweenthecurrenttimeandmidnight January1 1970UTC 24 对象的访问 关键词thisthis指当前对象应用1 加强程序可读性 this可有可无 classDemo1 doublex y Demo1 doublei doublej this x i this y j doubleave return x y 2 publicstaticvoidmain Stringargs Demo1d newDemo1 3 4 System out println d ave classDemo2 intx y z Demo2 inta intb x a y b this swap a b voidswap inta intb intt if x y t x x y y t 25 对象的访问 关键词thisthis指当前对象应用2 对同一个对象执行多次方法调用 classLeaf inti 0 Leafincrement i returnthis voidprint System out println i i publicstaticvoidmain Stringargs Leafx newLeaf x increment increment increment print D javaLeafi 3 26 对象的访问 关键词thisthis指当前对象应用3 在一个构造函数中调用另一个构造函数 classFlower Stringname intprice Flower this tulip 10 Flower Strings inti name s price i voidprint System out println name price publicstaticvoidmain Stringargs Flowerf newFlower f print D javaFlowertulip10
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!