您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 《面向对象技术与方法讲义》07、类与对象(II)
7-1LectureNotesonObject-OrientedProgramming&Design(Fall2002,MasterofSoftwareEngineering)Dr.LiWenjunlnslwj@zsu.edu.cnDepartmentofComputerScienceZhongshanUniversity,GZ5102757-2II•Java••JVM•••Java7-3•JavaC++OMGCORBA•classicalobjectmodel7.1Java7-4•C++(1)Complexc1(5,2);c1.print();//c1(2)Complex*c2=newComplex(5,2);c2-print();//c2•JavaComplexc3=newComplex(5,2);c3.print();//c37-5handle•JVM•SunJVMheapheap7-6••null•BruceEckel⇒⇒[]vs[]7-7•JamesGosling:...Thereferencevalues(oftenjustreferences)arepointerstotheseobjects,andaspecialnullreference,whichreferstonoobject.JavaC++C•⇒null⇒vs7-8•Stringname;intlength=name.length();//Stringname=Zhang3;Stringname=newString(Zhang3);7-9•intlength=Zhang3.length();•⇒Stringname1=Zhang3;Stringname2=name1.toUpperCase();newnewStringBufferStringStringBufferString7-10publicclassAge{privatestaticvoidprintAge(){Integerage=newInteger(18);System.out.println(+age);}publicstaticvoidmain(String[]args){printAge();}}vsageprintAge()newIntegerJVMageprintAge()newIntegerJVM7-117.2[]1.vs2.variable∈{primitive,reference}3.variable∈typeobject∈class7-12createdestroycleanupserviceinitialize7-13••null7-14•Stringnew•on-demand7-15•classvariablestaticstatic∼∼•instancevariablestatic∼∼∼•arraycomponent∼∼effectivelyceasestoexisteffectivelyceasestoexist7-16•methodparameterfinal∼•constructorparameterJava•exception-handlerparametertrycatch∼catch∼7-17•localvariablefinalC++Javaforfor∼∼for∼7-18classPoint{staticintnum;//numintx,y;//xyint[]w=newint[10];//w[0]intsetX(intanX){//anXintoldX=this.x;//oldXthis.x=anX;returnoldx;}}7-19•••7-20•byte(byte)0short(short)0int0long0Lfloat0.0fdouble0.0dchar'\u0000'booleanfalsenull7-21classPoint{staticintnpoints;intx,y;Pointroot;}classTest{publicstaticvoidmain(String[]args){System.out.println(npoints=+Point.npoints);Pointp=newPoint();System.out.println(p.x=+p.x+,p.y=+p.y);System.out.println(p.root=+p.root);}}npoints=0p.x=0,p.y=0p.root=nullnpoints=0p.x=0,p.y=0p.root=null7-22•final•⇒•C++C++final7-23•blankfinal⇒•final⇒7-24classPoint{privateintx,y;privateintuseCount;publicfinalstaticPointorigin=newPoint(0,0);Point(intx,inty){this.x=x;this.y=y;}}xyxyoriginfinaloriginfinal7-25•final⇒7-26•7.3JVMJVMJVM7-27JVM7-28stackJVMJavaC++7-29heapJVMJVM7-30static7-31ROMJavaC++7-32RAMserializationpersistence7-337.4createdestroycleanupserviceinitialize7-34•⇒•⇒•⇒⇒7-35•⇒•defaultcontructorpublic•initialize()create()⇒7-36publicclassPoint{intx,y;}≡publicclassPoint{intx,y;publicPoint(){super();}}throwsthrows7-37•⇒JavaC++•⇒OOP7-38•C++new•vs⇒⇒.class•⇒7-39classComputer{doubleweight=2.5;Stringseller=newString(Unknown);}classCyclic{floatf=i;staticinti=1;}classTest{inti=j;intj=1;}7-40••instanceinitializer•staticinitializerorstaticblock7-417-42classTag{Tag(intmarker){System.out.println(Tag(+marker+));}}classCard{Tagt1=newTag(1);Card(){System.out.println(Card());t3=newTag(33);}Tagt2;{t2=newTag(2);}voidf(){System.out.println(f());}Tagt3=newTag(3);}publicclassTest{publicstaticvoidmain(String[]args){Cardt=newCard();t.f();}}Tag(1)Tag(2)Tag(3)Card()Tag(33)f()Tag(1)Tag(2)Tag(3)Card()Tag(33)f()7-43classBowl{Bowl(intmarker){System.out.println(Bowl(+marker+));}voidf(intmarker){System.out.println(f(+marker+));}}classTable{staticBowlb1;static{b1=newBowl(1);}Table(){System.out.println(Table());b2.f(1);}voidf2(intmarker){System.out.println(f2(+marker+));}staticBowlb2=newBowl(2);}publicclassTest{publicstaticvoidmain(String[]args){System.out.println(Before);newTable();System.out.println(After);t.f2(1);}staticTablet=newTable();}Bowl(1)Bowl(2)Table()f(1)BeforeTable()f(1)Afterf2(1)Bowl(1)Bowl(2)Table()f(1)BeforeTable()f(1)Afterf2(1)staticstatic7-447.5createdestroycleanupserviceinitialize7-45•(1)new(2)⇒...•new⇒⇒C++:-)vsimplicitlyvsexplicitlyimplicitlyvsexplicitly7-46••JavavsC++C++Javafinalize()•finalize()finalize()callbackfinalizeJavafinalizeJava7-47•finalize()delete•finalize()JVM⇒delete•finalize()Java⇒finalize()C++delete⇒finalize()vsC++7-48•finalize()finalize()•finalize()⇒⇒C++•(1)(2)⇒C++7-49•finalize()AWTclassMyClass{AudioDevicedevice=AudioDevice.acquire();publicvoidcleanUp(){//...if(device!=null){device.release();device=null;}}publicvoidfinalize(){if(device!=null)device.release();}super.finalize();}//...}7-50•JavaJavaNativeInterfaceJNIC/C++Cmalloc()finalize()Cfree()•finalize()7-51classGarbage{staticbooleangcrun=false;staticintcreated=0;staticintfinalized=0;publicGarbage(){created++;}publicvoidfinalize(){if(!gcrun){gcrun=true;System.out.println(GCafter+created+garbages);}finalized++;}}publicclassTest{publicstaticvoidmain(String[]args){while(!Garbage.gcrun){newGarbage();newString(Totakeupspace...);}System.out.println(Created=+Garbage.created);System.out.println(Finalized=+Garbage.finalized);}}7-52javaTestGCafter5425garbagesCreated=5426Finalized=5425java-ms64m-mx64mTestGCafter30001garbagesCreated=30002Finalized=30001-mssize-mxsize-sssize-mssize-mxsize-sssizeJVMJVMJVM324GJVM324G7-53•(1)finalize()(2)System.gc()⇒⇒•JDK1.4assertref!=null;javac-source1.4...7-54classMyFile{booleanopened=false;MyFile(){opened=true;}voidclose(){opened=false;
本文标题:《面向对象技术与方法讲义》07、类与对象(II)
链接地址:https://www.777doc.com/doc-6363913 .html