Java第三章面向对象技术的复习回顾.ppt

上传人:max****ui 文档编号:6362785 上传时间:2020-02-23 格式:PPT 页数:58 大小:289KB
返回 下载 相关 举报
Java第三章面向对象技术的复习回顾.ppt_第1页
第1页 / 共58页
Java第三章面向对象技术的复习回顾.ppt_第2页
第2页 / 共58页
Java第三章面向对象技术的复习回顾.ppt_第3页
第3页 / 共58页
点击查看更多>>
资源描述
计算机专业三年级 第三章面向对象技术回顾 南京农业大学谢忠红 计算机专业三年级 内容简介 3 1类和对象的定义和使用 3 2类的封装封装的概念 如何封装 3 3类的继承继承的概念 再论构造函数3 4类的多态方法的覆盖 方法的重载3 5Java和C 这章的主要不同点 计算机专业三年级 3 1类和对象的定义和使用 Java是 纯 面向对象的编程语言 万物皆对象 客观世界是各种对象的集合 复杂的对象由简单的对象组合而成 每个对象都是唯一的 单独分配内存 对象具有属性和行为 他们之间进行消息发送 对象都属于某个类 计算机专业三年级 类和对象的概念 什么是类 什么是对象 对象 在我们所处的客观世界中 每一个有明确的意义和边界的事物都可以看作一个对象 Object 例如 某台电脑 一辆汽车对象类 简称类Class 我们可以把具有相似特征的事物归为一类 也就是所把具有相同属性的对象看成一个对象类 例如 电视机类 类 电视机的设计图对象 各家各户的电视机Java是通过先定一个类 然后按照类的结构创造一个又一个对象 计算机专业三年级 classCar Stringtype intcolor intprice voidstart voidbrake voidaccelerate 现实世界汽车类 计算机世界描述的汽车类 MyFirstCar newcar 计算机世界描述的一辆汽车 对象 计算机专业三年级 classman Stringname intage floatweight StringID voidmove voidspeek voidwork 现实世界人类 计算机世界描述的人 类 Jack newman 计算机世界描述Jack 对象 Jack 计算机专业三年级 使用Java如何创建一个类 类声明格式 class 修饰符 public private 空类名 一个自定义的修饰符号 publicclassperson privateStringname intage 12 intweight 30 publicperson publicvoidspeak publicStringgo publicvoidwork inta 计算机专业三年级 抽象类举例 classpoint intx y 属性声明point x 0 y 0 默认的构造函数point intx1 inty1 x x1 y y1 自定义构造函数voidprint System out println x y 计算机专业三年级 publicclassCircle 抽象类举例 privateintx y privatedoubler Circle intx1 inty1 doubler1 x x1 y y1 r r1 voidsetX intx1 x x1 voidsetR intr1 r r1 intgetY retruny doublegetR returnr publicdoublegetCir returnMath PI r 2 publicdoublegetArea returnMath PI r r 计算机专业三年级 publicclassuseCircle publicstaticvoidmain String args floats 0 L 0 创建了2个圆对象 Circlec1 newCircle 20 50 15 0 Circlec2 newCircle 100 110 30 0 s c1 getArea 调用方法L c1 getCir System out println 圆c的面积是 s 圆c1的周长是 L c1 c2 对象的赋值L c1 getCir System out println 圆c1的周长是 L 计算机专业三年级 通过类创建对象 格式 类型对象名 new类型 参数列表 如 Circlec1 newCircle 20 50 15 0 创建对象比较 C Circlec1 Java Circlec1 newCircle 20 50 15 0 计算机专业三年级 执行c1 c2后呢 对象的赋值c1 c2 返回 计算机专业三年级 实例变量和局部变量 Java中的变量分为两种 类内定义的实例变量 成员变量 方法中定义的局部变量 classpoint intx y 实例变量voidinit inta intb ints 0 局部变量 x a y b 计算机专业三年级 实例变量和局部变量 类内定义的实例变量 方法中定义的局部变量 classpoint intx y 实例变量 voidinit inta intb intx 0 局部变量 x a x b 计算机专业三年级 Publicclassexp Publicvoiddisplay intx 1 intx 0 错误 注意 在同一个作用域内 不允许定义两个同名的局部变量 计算机专业三年级 实例变量和局部变量 classPoint protectedintx y 实例变量point intx1 inty1 x x1 y y1 publicvoidsetValue x 5 y 10 publicvoidprintValue intx 15 局部变量System out println x publicvoidprintInsValue x 5 x y y 5 System out println x y publicstaticvoidmain Stringargs 在类中Pointp1 newPoint 10 10 p1 setValue p1 printValue p1 printaInsValue 计算机专业三年级 ClassPoint 保存为point h文件Public Point intx1 inty1 x x1 y y1 setValue print cout x x y y protected intx y include 保存为 cppintx voidPoint setValue Point x 0 Point y 0 voidmain Pointp1 p1 setValue x 12 注意 C 支持全局变量而Java不支持 计算机专业三年级 构造函数 特殊的方法 DEF 构造函数是在创建对象的时候自动调用的一个方法 该方法与类名同名 构造函数的作用 主要是用来初始化对象的数据属性 计算机专业三年级 举例 classCellPhone privateStringtype privateStringcolor privatedoubleprice publicCellPhone Stringtype Stringcolor doubleprice this type type this Color color this Price price publicstaticvoidmain Stringargs CellPhoneMine newCellPhone Nokia Red 1500 0 计算机专业三年级 classDate intyear month day voidprint classDate intyear month day voidprint System out println dateis year month day Date 系统自动创建一个默认构造器 计算机专业三年级 一个类可以有多个构造函数 重载 根据参数的不同决定执行哪一个 计算机专业三年级 多个构造器 classPoint intx y z Point x 1 y 1 默认的构造函数Point intnew x intnew y x new x y new y Point intnew x intnew y intnew z x new x y new y z new z classUsePoint Pointpoint A newPoint Pointpoint B newPoint 5 7 pointpoint c newPoint 10 20 87 其中无参构造其实默认的构造器 计算机专业三年级 构造函数的特点 1 构造函数的函数名与类名相同2 构造函数没有返回值 但是不加void修饰符 3 构造函数可以没有参数 默认构造函数4 构造函数可以有参数 自定义构造函数 Java采用垃圾自动回收机制没有析构函数 C 计算机专业三年级 3 2类的封装 class private属性构造函数方法 封装的定义 类的设计者把类设计成一个黑匣子 使用者只能看见类定义的公共方法而看不见方法实现的细节 也不能对类的数据进行操作 计算机专业三年级 为什么要对类进行封装呢 1 隐藏类的细节 使用类的人无需知道细节 2 迫使用户通过方法接口去访问数据防止外部对类中数据的干扰 3 封装减少了程序对类中数据表达的依赖性 增强了代码的可维护性 类封装的概念可以打个比方 拿收音机做类比 计算机专业三年级 publicclassMyDate 类的封装privateintyear month day 成员变量 私有的publicMyDate inty intm intd 对私有成员赋值year y month m day d publicvoidsetYear inty year y publicvoidsetDay intd dayr d publicintgetYear returnyear 返回私有成员以便利用publicintgetMonth returnmonth publicintgetDay returnday publicvoidprint System out println dateis year month day 计算机专业三年级 classEncapsueDate publicstaticvoidmain Stringargs MyDatea newMyDate 2002 6 28 a day 13 System out println a year a month a day a print 对吗 对吗 计算机专业三年级 3 3类的继承 DEF 子类继承父类的属性和方法 以及在父类的基础上添加新的属性和方法则就叫做继承 在Java中 有一个被称为Object的特殊超类 所有的类都直接或间接地继承Object类 计算机专业三年级 父类和子类之间反映的是 一般与特殊 的关系 父类和子类之间反映的是 一般与特殊 的关系 计算机专业三年级 继承语法 classclassNameextendssuperClassName 各实例变量方法的定义 注意 没有extends 默认父类为Object只能有一个父类 即单继承子类继承父类的全部成员类继承具有传递性 计算机专业三年级 classPeople publicStringname publicintage publiccharsex People Stringname intage charsex this name name this age age this sex sex voidsetNameAgeSex Stringname intage charsex this name name this age age this sex sex StringgetName returnname 查询姓名 父类 People类的声明 计算机专业三年级 classTeacherextendsPeople Stringcourse 教授课程 子类Teacher类 Teacher Stringname intage charsex Stringcourse this name name this age age this sex sex this course course StringwhichCourse returncourse 查询所授课程 voidsetValue Stringname intage charsex booleanisTeaching setNameAgeSex name age sex this course course voidprint System out println name sex age course 计算机专业三年级 classtest publicstaticvoidmain Stringargs TeacherLiPing newTeacher LiPing 25 m Chinese LiPing print 计算机专业三年级 classtest publicstaticvoidmain Stringargs TeacherLiPing newTeacher LiPing 25 m Chinese LiPing print 计算机专业三年级 有关继承必须说明的几点 1 子类能够继承父类中声明的所有成员变量 不能直接使用private类型的成员变量 2 子类的变量 方法 会覆盖超类的同名成员变量 方法 但子类方法不能缩小父类方法的权限 3 子类的构造函数与父类构造函数的关系 计算机专业三年级 成员覆盖习练习题1classA intx 1234 voidshow System out println classA 计算机专业三年级 classBextendsA doublex 567 89 voidshow intx 45 System out println classA x super x System out println classB x x super show System out println classB publicstaticvoidmain Stringargs Bx newB System out prinln x x x show 计算机专业三年级 再论构造函数 this引用和super引用关键字thisDEF 代表一个特殊的对象即当前对象 1 指代对象 2 指代构造函数 计算机专业三年级 this代表一个特殊的对象即当前对象 例 ClassButton charc booleanequals Buttonother if this other returntrue returnfalse classtestButton publicstaticvoidmain Stringargs Buttont1 newButton t3 newButton t3 t1 System out println t3 equals t1 计算机专业三年级 this指代对象可以用于解决实例变量被局部变量屏蔽的问题 publicclassMyDate 类的封装privateintyear month day publicMyDate intyear intmonth intday this year year this month month this day day publicsetDay intday this day day date1 newMyDate 2002 5 8 构造函数中的this year就是指的是date1的实例变量year 计算机专业三年级 this指代构造函数 在构造器内部使用this 它用于指代另外一个构造函数 Classpoint intx inty Point inta intb x a y b Point this 1 1 publicclassUsePoint Publicstaticvoidmain pointp1 newpoint System out println p1 X p1 y pointp2 newpoint 10 20 System out println p2 X p2 y 计算机专业三年级 关键字super 构造函数是一种特殊的方法 子类不能继承超类的构造函数 但子类构造函数可以通过super调用超类的构造函数 classcircleextendspoint intradius circle intr intx inty super x y 调用父类的构造函数radius r System out println 子类构造函数被调用 classpoint intx y point intx inty this x x this y y System out println 父类构造函数被调用 计算机专业三年级 publicclasstestInherence publicstaticvoidmain Stringargs circlec1 c1 newcircle 1 1 1 计算机专业三年级 有关构造函数 请注意下列几个例子 计算机专业三年级 子类classcircleextendspoint intr circle intr radius r publicstaticvoidmain Stringargs circlec1 newcircle 20 System out println x c1 x y c1 y r c1 r 或 classpoint intx 1 y 1 point 此处相当于有一个super 计算机专业三年级 总结 若父类没有定义构造函数或只有一个默认构造函数那么子类先执行父类默认的构造函数 对父类数据的初始化 然后再执行自己的构造函数 计算机专业三年级 答案 子类可以在自己的构造函数中使用super调用它 计算机专业三年级 classCylinderextendsCircle privatedoubleheight publicCylinder intx1 inty1 doubler1 double height h classcircleextendspoint intradius circle intr intx inty radius r this x x this y y super circle 计算机专业三年级 这个程序能编译成功吗 classPoint intx y Point intx inty this x x this y y classCircleextendsPoint intr Circle x 10 y 10 r 10 Circle intr super 20 20 this r r super Point 计算机专业三年级 类的多态性 1 方法的覆盖 简单的多态 2 方法重载 3 构造函数的重载 计算机专业三年级 方法的重载 在Java中 同一个方法名可以被定义多次 但要求参数表不能完全一样 调用方法时 系统是通过方法名和参数确定所调用的具体方法 这种现象叫做方法或函数的重载 计算机专业三年级 classmysqure intsquare intx returnx x doublesquare doubley returny y publicclassMethodOverload publicstaticvoidmain String g mysqures newmysqure System out println Thesquareofinteger17is s square 17 System out println Thesquareofdouble17 5is s square 17 计算机专业三年级 Java和C 在这一章的不同 基本的概念相同 类 类的三大特征 语法有所不同 Java是单继承 C 是多继承 Java纯封装 无全局变量和类外的方法 C 非也 计算机专业三年级 习题 1 类Test1 Test2定义如下 1 publicclassTest1 2publicfloataMethod floata floatb 3 4 5 publicclassTest2extendsTest1 6 7 将以下哪种方法插入行6是不合法的 A publicfloataMethod floata floatb B publicintaMethod inta intb C publicintaMethod floatp floatq D publicintaMethod floata intb 计算机专业三年级 面向对象程序举例 创建一个复数类 Complex类 有两个类变量realimagin要求 两个构造函数 用方法将复数以 a b 显示出来三个方法实现复数的加减乘 a b c d a c b d a b c d a c b d a b c d a c b d a d b c 计算机专业三年级 程序设计例题创建复数类 classComplex privatefloatreal privatefloatimagin Complex real 0 imagin 0 Complex floatreal floatimagin this real real this imagin imagin floatreturnReal returnreal floatreturnImaginary returnimagin 计算机专业三年级 Complexadd Complexb intTreal real b real inTimagin imagin b imagin returnnewComplex Treal Timagin Complexsub Complexb intTreal real b real intTimagin imagin b imagin returnnewComplex Treal Timagin
展开阅读全文
相关资源
相关搜索

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


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

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


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