您好,欢迎访问三七文档
1.写一个Java应用程序,主要是体现父类子类间的继承关系。父类:鸟,子类:麻雀、鸵鸟、鹰。子类继承父类的一些特点,如都是鸟的话就都会有翅膀、两条腿等,但它们各自又有各自的特点,如麻雀的年龄、体重;鸵鸟的身高、奔跑速度;鹰的捕食、飞翔高度等。鸟packagesudy;publicclassBird{publicvoidniaoxinxi(){System.out.println(我能飞!);System.out.println(我有两条腿!);}}麻雀packagesudy;publicclassMaQueextendsBird{privateintnianling;privateinttizhong;publicMaQue(intnianling,inttizhong){this.nianling=nianling;this.tizhong=tizhong;}publicvoidname(){System.out.println(我是麻雀!);}publicvoidshuchu(){System.out.println(我的年龄是:+nianling+岁);System.out.println(我的体重是:+tizhong+斤);}}鸵鸟packagesudy;publicclassTuoNiaoextendsBird{privateintshengao;privateintsudu;publicTuoNiao(intshengao,intsudu){this.shengao=shengao;this.sudu=sudu;}publicvoidname(){System.out.println(我是鸵鸟!);}publicvoidshuchu(){System.out.println(我的身高是:+shengao+m);System.out.println(我的速度是:+sudu+m/s);}}鹰packagesudy;publicclassYingextendsBird{privateStringpushi;privateintflygaodu;publicYing(Stringpushi,intflygaodu){this.pushi=pushi;this.flygaodu=flygaodu;}publicvoidname(){System.out.println(我是鹰!);}publicvoidshuchu(){System.out.println(我的扑食方式是:+pushi);System.out.println(我的飞行高度是:+flygaodu+m);}}测试packagesudy;publicclassBirdTxet{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubMaQuemq=newMaQue(2,12);Yingy=newYing(爪子抓,300);TuoNiaotn=newTuoNiao(11,23);mq.name();mq.niaoxinxi();mq.shuchu();y.name();mq.niaoxinxi();y.shuchu();tn.name();mq.niaoxinxi();tn.shuchu();}}2.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff。具体要求如下:(1)Person类中的属性有:姓名name(String类型)、地址address(String类型)、电话号码telephone(String类型)和电子邮件地址email(String类型)。packagesudy;publicclassPerson{privateStringname;privateStringaddress;privateStringtelephone;privateStringemail;publicPerson(){}publicPerson(Stringname,Stringaddress,Stringtelephone,Stringemail){this.name=name;this.address=address;this.telephone=telephone;this.email=email;}}(2)Employee类中的属性有:办公室office(String类型)、工资wage(double类型)和受雇日期hiredate(String类型)。packagesudy;publicclassEmployeeextendsPerson{privateStringoffice;privatedoublewage;privateStringhiredate;}(3)Faculty类中的属性有:学位degree(String类型)、级别level(String类型)。packagesudy;publicclassFacultyextendsEmployee{privateStringdegree;privateStringlevel;}(4)Staff类中的属性有:职务称号duty(String类型)。packagesudy;publicclassStaffextendsEmployee{privateStringduty;}3.在包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有);具有功能:工作(公有)、开车(公有)。在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名;具有功能:玩(私有)、学习(公有)。最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。类Fatherpackagea;publicclassFather{privateintnianling;publicStringname;publicFather(Stringname,intnianling){this.name=name;this.setNianling(nianling);}publicvoidgongneng(){System.out.println(name+能开车和工作);}publicintgetNianling(){returnnianling;}publicvoidsetNianling(intnianling){this.nianling=nianling;}}类Sonpackagea;publicclassSonextendsFather{protectedintnianling;privateStringname;publicSon(Stringname,intnianling){super(name,nianling);}@SuppressWarnings(unused)privatevoidwan(){System.out.println(name+能玩!);}publicvoidxuexi(){System.out.println(name+能学习!);}}Textpackagea;publicclassText{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubSons=newSon(小张三,19);s.gongneng();s.xuexi();Fatherf=newFather(张三,44);f.gongneng();}}4.编写一个Car类,具有final类型的属性品牌,具有功能drive;定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;定义主类E,在其main方法中分别创建Aodi和Benchi的对象,并测试对象的特性。Car类packagefour;publicclassCar{privatefinalStringpinpai=;publicvoiddrive(){System.out.println(正在开车);}publicStringgetPinpai(){returnpinpai;}}Aodi类packagefour;publicclassAodiextendsCar{privateintjiage;privateStringxinghao;publicvoidgongneng(){System.out.println(变速);}publicintgetJiage(){returnjiage;}publicvoidsetJiage(intjiage){this.jiage=jiage;}publicStringgetXinghao(){returnxinghao;}publicvoidsetXinghao(Stringxinghao){this.xinghao=xinghao;}}Benchi类packagefour;publicclassBenchiextendsCar{privateintjiage;privateStringxinghao;publicvoidgongneng(){System.out.println(变速);}publicintgetJiage(){returnjiage;}publicvoidsetJiage(intjiage){this.jiage=jiage;}publicStringgetXinghao(){returnxinghao;}publicvoidsetXinghao(Stringxinghao){this.xinghao=xinghao;}}E类packagefour;publicclassE{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubCarc=newCar();c.drive();Aodia=newAodi();a.setJiage(999990);a.setXinghao(X5);System.out.println(Aodi的价格为:+a.getJiage()+型号为:+a.getXinghao());a.gongneng();Benchib=newBenchi();b.setJiage(845000);b.setXinghao(B200);System.out.println(Benchi的价格为:+b.getJiage()+型号为:+b.getXinghao());b.gongneng();}}5.按要求编写一个Java应用程序:(1)定义一个矩形类Rect,包含两个protected属性:矩形的宽width和高height。一个带有两个参数的构造方法,用于将width和height属性初始化。一个不带参数的构造方法,将矩形的宽和高都初始化为10。还有两个一般方法:求矩形面积的方法getArea()和求矩形周长的方法getPerimeter()。publicclassRect{//属性protecteddoublewidth;protecteddoubleheight;//方法//不带参数构造函数publicRect(){this.width=10;this.height=10;}//带2个参数构造函数publicRect(doublex,doubley){this.width=x;this.height=y;}//球面积方法publicdoublegetArea(){returnwidth*height;}//求周长方法publicdoublegetPerimeter(){return2*(width+height);}publicdoublegetWidth(){returnwidth;}publicvoidsetWidth(doublewidth){this.width=width;}publicdoublegetHeight(){returnheight;}publicvoidsetHeight(do
本文标题:java继承题
链接地址:https://www.777doc.com/doc-1838738 .html