java程序设计第六章 面向对象编程的深入讨论.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《java程序设计第六章 面向对象编程的深入讨论.pdf》由会员分享,可在线阅读,更多相关《java程序设计第六章 面向对象编程的深入讨论.pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、2010/11/22010/11/21 1JAVA语言程序设计语言程序设计Java程序设计程序设计主讲教师:张思民主讲教师:张思民面向对象编程的深入讨论第六章2目标 构造函数的执行机制 屏蔽、覆盖和重载 对象间的类型转换 对象乊间的比较2010/11/22010/11/22 236.1 6.1 构造函数的执行机制 构造函数的作用:对创建的对象进行初始化 类的继承机制:子类可以使用父类的功能(即代码),那么子类对象的初始化要使用到父类的构造函数。4class class SuperClass SuperClass SuperClass()SuperClass()SystemSystem.out.
2、println(SuperClass constructor);.out.println(SuperClass constructor);publicpublic classclass SubClassSubClass extendsextends SuperClassSuperClass SubClass()SubClass()SystemSystem.outout.println(println(SubClassSubClass constructorconstructor);publicpublic staticstatic voidvoid main(main(StringString
3、 args)args)SubClassSubClass subsub=newnew SubClass()SubClass();例子例子6 61 1类在继承关系上的初始化的顺序类在继承关系上的初始化的顺序2010/11/22010/11/23 35例子例子6 61 1在子类中只实例化了一个子类对象。在子类中只实例化了一个子类对象。从输出结果上看:从输出结果上看:程序并不是一开始就运行自己的构造方法,程序并不是一开始就运行自己的构造方法,而是先运行其父类的默认构造方法。而是先运行其父类的默认构造方法。注意:程序自动调用其父类的默认构造方法。注意:程序自动调用其父类的默认构造方法。子类对象初始化子类
4、对象初始化父类父类构造函数构造函数6classclass SuperClassSuperClassSuperClass(StringSuperClass(String str)str)SystemSystem.outout.println(Superprintln(Super withwith a a stringstring.);publicpublic classclass SubClassSubClass extendsextends SuperClassSuperClassSubClass(StringSubClass(String str)str)SystemSystem.outou
5、t.println(Subprintln(Sub withwith a a stringstring.);publicpublic staticstatic voidvoid main(Stringmain(String args)args)SubClassSubClass subsub=newnew SubClass(sub)SubClass(sub);SubClass未找到其父类的默认构造方法SuperClass()SuperClass(),编译通丌过SuperClass();SuperClass();方法方法1 1:在父类中增加在父类中增加一个默认构造方法。一个默认构造方法。SuperC
6、lass();SuperClass();/SuperClass();/SuperClass();super(str);super(str);方法方法2 2:在子类的构在子类的构造方法中第一句增造方法中第一句增加加“super(str)super(str)”例6-2:2010/11/22010/11/24 47publicpublic classclass TestTest publicpublic staticstatic voidvoid main(Stringmain(String args)args)SystemSystem.outout.println(Testprintln(Test
7、 main()main();CarCar carcar=newnew Car(car)Car(car);输出结果为:输出结果为:Test main()Test main()wheelwheel-1 1 wheelwheel-2 2 wheelwheel-3 3 carcar例6-3:对象的初始化顺序问题在main()方法中初始化car类的对象时,先初始化car类的成员变量,(成员变量是wh对象则调用该对象相应的构造方法)。而后再初始化car类的对象,即调用car类的构造方法class Wheel class Wheel Wheel(String str)Wheel(String str)Sys
8、tem.out.println(str);System.out.println(str);classclass carcar WheelWheel wheel_wheel_1 1=newnew WheelWheel(wheel(wheel-1 1);WheelWheel wheel_wheel_2 2=newnew WheelWheel(wheel(wheel-2 2);WheelWheel wheel_wheel_3 3=newnew WheelWheel(wheel(wheel-3 3);Car(StringCar(String str)str)SystemSystem.outout.pr
9、intln(str)println(str);8例6-3:对象的初始化顺序 在创建对象时,对象所在类的所有数据成员会首先进行初始化 如果其中的成员变量有对象,那么它们也会按照顺序执行初始化工作。在所有类成员初始化完成后,才调用对象所在类的构造方法创建对象。构造方法作用就是初始化。在test类中创建car对象时,先初始化“car”类的成员变量成员变量wheel_1wheel_1,wheel_1wheel_1,wheel_1wheel_1等按顺序初始化最后调用carcar类的构造函数2010/11/22010/11/25 59publicpublic classclass TestTest pub
10、licpublic staticstatic voidvoid main(main(StringString args)args)SystemSystem.outout.println(Testprintln(Test main()main();CarCar car_car_1 1=newnew car(carcar(car-1 1);SystemSystem.outout.println(println(-);CarCar car_car_2 2=newnew car(carcar(car-2 2);如一个类有静态对象,那么它会在非静态对象前初始化,但只初始化一次classclass car
11、car WheelWheel wheel_wheel_1 1=newnew Wheel(Wheel(“wheelwheel-1 1”);WheelWheel wheel_wheel_2 2=newnew Wheel(wheelWheel(wheel-2 2);staticstatic WheelWheel wheel_wheel_3 3=newnew Wheel(wheelWheel(wheel-3 3);Car(StringCar(String str)str)SystemSystem.outout.println(str)println(str);classclass Wheel Whee
12、l Wheel(String str)Wheel(String str)SystemSystem.out.println(str);.out.println(str);例64:输出结果:输出结果:Test main()Test main()wheelwheel-3 3 wheelwheel-1 1 wheelwheel-2 2 carcar-1 1-wheelwheel-1 1 wheelwheel-2 2 carcar-2 2 10classclass carcar WheelWheel wheel_wheel_1 1=newnew Wheel(wheelWheel(wheel-1 1);W
13、heelWheel wheel_wheel_2 2=newnew Wheel(wheelWheel(wheel-2 2);staticstatic WheelWheel wheel_wheel_3 3=newnew Wheel(wheelWheel(wheel-3 3);Car(StringCar(String str)str)SystemSystem.outout.println(str)println(str);例6-5public class Test static Car car_3=new Car(car-3);publicpublic staticstatic voidvoid m
14、ain(Stringmain(String args)args)System.out.println(Test main();Car car_1=new Car(car-1);System.out.println(-);Car car_2=new Car(car-2);输出输出:wheelwheel-3 3wheelwheel-1 1wheelwheel-2 2carcar-3 3TestTest main()main()wheelwheel-1 1wheelwheel-2 2carcar-1 1-wheelwheel-1 1wheelwheel-2 2carcar-2 2程序中主类的静态变量
15、会在main()方法执行前初始化。classclass Wheel Wheel Wheel(String str)Wheel(String str)SystemSystem.out.println(str);.out.println(str);2010/11/22010/11/26 611classclass carcar staticstatic intint i i=0 0;WheelWheel wheel_wheel_1 1=newnew Wheel(wheelWheel(wheel-1 1);staticstatic WheelWheel wheel_wheel_2 2=newnew
16、Wheel(wheelWheel(wheel-2 2);staticstatic WheelWheel wheel_wheel_3 3=newnew Wheel(wheelWheel(wheel-3 3);car(Stringcar(String str)str)SystemSystem.outout.println(str)println(str);publicpublic classclass TestTestpublicpublic staticstatic voidvoid main(Stringmain(String args)args)SystemSystem.outout.pri
17、ntln(main()println(main();SystemSystem.outout.println(carprintln(car.i i=+carcar.i)i);输出:输出:main()main()wheelwheel-2 2 wheelwheel-3 3 car.i=0car.i=0第1次创建对象时,类中所有的静态变量要初始化第1次访问类中的静态变量(没有创建对象)时,该类中所有的静态变量也要按照它们在类中排列的顺序初始化。classclass Wheel Wheel Wheel(String str)Wheel(String str)SystemSystem.out.printl
18、n(str);.out.println(str);例6-6126.1构造函数的执行机制:总结例63:在创建对象时,对象所在类的所有数据成员(包括对象)首先按顺序进行初始化,所有类成员初始化完成后,才调用对象所在类的构造方法创建对象。构造方法作用就是初始化。例6-4,6-5:静态对象(变量)在非静态对象前初始化。静态对象(变量)只初始化一次,再次调用就丌初始化了,但非静态对象在每次调用时都要初始化。2010/11/22010/11/27 7136.1构造函数的执行机制:总结例6-5:程序中的主类的静态变量会在main()方法执行前进行初始化工作。例66:丌仅第1次创建对象(如)时,类中所有的静态
![java程序设计第六章 面向对象编程的深入讨论.pdf_第1页](https://file3.taowenge.com/FileRoot3/2023-1/13/bedf9deb-1ead-43c6-bff9-599816fc5188/bedf9deb-1ead-43c6-bff9-599816fc51881.gif)
![java程序设计第六章 面向对象编程的深入讨论.pdf_第2页](https://file3.taowenge.com/FileRoot3/2023-1/13/bedf9deb-1ead-43c6-bff9-599816fc5188/bedf9deb-1ead-43c6-bff9-599816fc51882.gif)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java程序设计第六章 面向对象编程的深入讨论 java 程序设计 第六 面向 对象 编程 深入 讨论
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内