资源描述:
《java继承作业》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1假设A是一个类。那么举例说明newA和Aa=newA()的区别。最好是说出原理堆里面对象的首地址给栈里的变量,如果只操作变量的话,不会导致对象的地址改动,从而可以多次使用,而匿名对象只能直接操作对象,每new一次又重新开辟一块内存空间,产生新的对象packagecom.wepull.extendS.test;publicclassTest01{publicstaticvoidmain(String[]args){System.out.println(newA());System.out.println(newA());//匿名对象只能用一次Aa=newA();System.out.p
2、rintln(a);System.out.println(a);//可以多次使用}}classA{}2完成:动物园的饲养员为猫吃鱼,狗吃肉,大象吃香蕉.packagecom.wepull.extendS.test;publicclassTest02{publicstaticvoidmain(String[]args){Persona=newPerson();Animala1=newCat("猫");Animala2=newDog("狗");Animala3=newElephant("大象");Foodf1=newFood("鱼");Foodf2=newFood("肉");Foodf3=
3、newFood("香蕉");a.feed(a1,f1);a.feed(a2,f2);a.feed(a3,f3);}}classAnimal{privateStringname;publicAnimal(Stringname){this.name=name;}voideat(Foodf){System.out.println(name+"吃"+f.getName());}}classCatextendsAnimal{publicCat(Stringname){super(name);}}classDogextendsAnimal{publicDog(Stringname){super(n
4、ame);}}classElephantextendsAnimal{publicElephant(Stringname){super(name);}}classFood{privateStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicFood(Stringname){this.name=name;}}classPerson{voidfeed(Animala,Foodd){a.eat(d);}}3有农民,教师,科学家,服务生,其中农民,服务生只有
5、基本工资.教师除基本工资外,还有课酬(元/天),科学家除基本工资外,还有年终奖,请你写出相关类,将各种类型的员工的全年工资打印出来。packagecom.wepull.extendS.test;publicclassTest03{publicstaticvoidmain(String[]args){Employeea=newPeasant("农民",3000);Employeeb=newTeacher("教师",4000,100);Employeec=newScientist("科学家",10000,50000);Employeed=newPeasant("服务生",3001);a.S
6、AL();b.SAL();c.SAL();d.SAL();}}classEmployee{publicStringname;publicdoublesal;voidSAL(){}publicEmployee(Stringname,doublesal){super();this.name=name;this.sal=sal;}}classPeasantextendsEmployee{publicPeasant(Stringname,doublesal){super(name,sal);}voidSAL(){System.out.println(name+"的年薪为:"+sal*12);}
7、}classTeacherextendsEmployee{publicdoubleforclass;publicTeacher(Stringname,doublesal,doubleforclass){super(name,sal);this.forclass=forclass;}voidSAL(){doublem=sal*12+forclass*100;System.out.println(name+"的年薪为:"+m);}}classSci