資源描述:
《學(xué)號(hào)姓名-類繼承與接口(一)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、實(shí)驗(yàn)九類繼承與接口(一)課程名稱:面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)實(shí)驗(yàn)項(xiàng)目名稱:實(shí)驗(yàn)九類繼承與接口(一)學(xué)生姓名:許方馳專業(yè):計(jì)算1302學(xué)號(hào):31301150實(shí)驗(yàn)地點(diǎn):實(shí)驗(yàn)日期:年月【實(shí)驗(yàn)?zāi)康摹?.掌握J(rèn)ava類的繼承特點(diǎn),包括子類聲明、對(duì)象創(chuàng)建等。2.掌握域的繼承與隱藏的特點(diǎn)。3.掌握方法的繼承與重寫的特點(diǎn)?!緦?shí)驗(yàn)內(nèi)容】1.執(zhí)行類ConstructorChain,請(qǐng)寫出輸出結(jié)果,并分析出現(xiàn)該結(jié)果的原因。提示:參考構(gòu)造器鏈。classConstructorChain{publicstaticvoidmain(String[]args){newSon();}}classGrandpa{Grandpa
2、(){System.out.println("Grandpa");}}classFatherextendsGrandpa{Father(){System.out.println("father");}}classSonextendsFather{Son(){System.out.println("son");}}(1)運(yùn)行結(jié)果:分析繼承鏈上各個(gè)類的構(gòu)造器的調(diào)用次序:分析是否是超類構(gòu)造器調(diào)用在前:Grandpafatherson是的(2)在命令行上鍵入java–verboseConstructorChain,并回車,觀察各個(gè)類的加載次序.各個(gè)類的加載次序是:分析是否超類先加載:Grandp
3、afatherson是的2.程序設(shè)計(jì),把以下代碼空缺處補(bǔ)齊n(1)類的繼承n需求分析:n校務(wù)管理系統(tǒng),設(shè)計(jì)類來(lái)描述系統(tǒng)中的兩個(gè)角色:教師與學(xué)生npublicclassTeachernpublicclassStudentn抽取共性npublicclassPersonn考慮在Person中設(shè)計(jì)教師與學(xué)生的公有域已知部分代碼設(shè)計(jì)如下classPerson*classPerson*{privateStringname;*privateStringlocation;*Person(Stringname){*this.name=name;*location="hangzhou";*}*Person(
4、Stringname,Stringlocation){*this.name=name;*this.location=location;*}*publicStringinfo(){*return*"name:"+name+"location:"+location;*}*}classTeacher*classTeacherextendsPerson*{//[代碼1]增加年齡屬性*Teacher(Stringname,intage){*//[代碼2]構(gòu)造器體*}*Teacher(Stringn,Stringl,intage){*//[代碼3]構(gòu)造器體*//[代碼4]構(gòu)造器體*}*publicS
5、tringinfo(){*//[代碼5]返回姓名:住所:以及年齡的信息*}*}classPerson{privateStringname;privateStringlocation;Person(){};Person(Stringname){this.name=name;location="hangzhou";}Person(Stringname,Stringlocation){this.name=name;this.location=location;}publicStringinfo(){return"name:"+name+"location:"+location;}}classT
6、eacherextendsPerson{intage;Teacher(Stringname,intage){super(name);this.age=age;}Teacher(Stringn,Stringl,intage){super(n,l);this.age=age;}publicStringinfo(){returnsuper.info()+"age:"+age;}}classStudentextendsPerson{intage;Student(Stringname,intage){super(name);this.age=age;}Student(Stringn,Stringl
7、,intage){super(n,l);this.age=age;}publicStringinfo(){returnsuper.info()+"age:"+age;}}publicclassTest2{publicstaticvoidmain(Stringargs[]){Teacherteacher1=newTeacher("MrGreen",30);Studentstudent1=newStudent("John",18);Te