(7-1)一个父类一般代表的对象数量要少于其子类代表的对象数量。
查看答案
(7-1)请阅读下面程序,并写出运行结果。classBase{publicintgetX(){return1;}publicstaticintgetY(){return2;}}classSubextendsBase{publicintgetX(){return3;}publicstaticintgetY(){return4;}}publicclassDemo{publicstaticvoidmain(String[]args){Subsub=newSub();Basebase=(Base)sub;inta=sub.getX()*base.getX();intb=sub.getY()+base.getY();System.out.println(a+b);}}
(7-7)请阅读程序,写出程序运行结果。//写出程序运行结果classEye{//猫的眼睛类privateStringcolor;publicEye(Stringcolor){this.color=color;}}classCat{//猫类privateStringname;privateEyeeye;publicCat(Stringname,Eyeeye){this.name=name;this.eye=eye;}publicbooleanequals(Objectobj){Catcat=(Cat)obj;if(this.name.equals(cat.name)&&this.eye==cat.eye)returntrue;returnfalse;}}publicclassCatDemo{publicstaticvoidmain(String[]args){Eyee1=newEye("蓝色");Eyee2=newEye("蓝色");Cattom=newCat("Tome",e1);Catjack=newCat("Jack",e2);System.out.println(tom==jack);System.out.println(tom.equals(jack));}}
(7-1)请阅读程序,并写出程序运行结果。classX{intgetX(){return5;}}classYextendsX{intgetX(){return6;}}classTextendsX{intgetX(){return7;}}publicclassDemo10{publicstaticvoidmain(String[]args){Xx=newX();Yy=newY();Xt=newT();intsum=x.getX()+y.getX()+t.getX();System.out.println(sum);}}
(7-1)阅读如下程序,写出①、②处的运行结果。classDog{Stringname;publicDog(Stringname){super();this.name=name;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+((name==null)?0:name.hashCode());returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(obj==null)returnfalse;if(getClass()!=obj.getClass())returnfalse;Dogother=(Dog)obj;if(name==null){if(other.name!=null)returnfalse;}elseif(!name.equals(other.name))returnfalse;returntrue;}}publicclassDemo7{publicstaticvoidmain(String[]args){Dogd1=newDog("泰迪犬");Dogd2=newDog("泰迪犬");System.out.println(d1==d2);//输出结果①System.out.println(d1.equals(d2));//输出结果②}}