首页 > 试题广场 >

写出以下程序的运行结果,填写在每个输出语句后面的注释中。

[填空题]

写出以下程序的运行结果,填写在每个输出语句后面的注释中。

public class ATest{

public static void main(String args[]) {

Sub   sub = new Sub(  );

System. out .println(sub. x );              // 1

System. out .println(sub. n );              //2

System. out .println(sub.method1(  ));    //3

System. out .println(sub.method2( ));    //4

Super supsub= new Sub( );

System. out .println(supsub. x );          //5

System. out .println(supsub. n );          //6

System. out .println(supsub.method1( ));//7

System. out .println(supsub.method2( ));//8

Super sup= new Super( );

System. out .println(sup. x );             //9

System. out .println(sup. n );             //10

System. out .println(sup.method1( ));   //11

System. out .println(sup.method2( ));   //12

}

}

class Super{

int x =1 , y =2 ;

static int n =50;

int method1( ){

return ( x > y )? x : y ;

}

static int method2( ){

return n --;

}

}

class Sub extends Super{

int method1( ) {

return ( x < y )? x : y ;

}

static int method2( ){

return n ++;

}

}

第7个
在java编程思想的多态8.2.5中有说。
“如果某个方法是静态的,它的行为就不具有多态性”
“静态方法是与类,而并非与单个的对象相关联的”
编辑于 2017-05-22 22:23:06 回复(1)
1-4没有什么问题,分别输出sub.x=1,sub.n=50,sub.method1()=1;sub.method2()=50(n++之后,n=51)
5-8Super supsub= new Sub( );这里用了多态,其实还是创建了一个Sub对象,名字是supsub,
supsub.x=1;sup.n=51(因为n是静态数据,相当于共享的,任何Sub对象都可以对他的值进行修改);supsub.metho1()=1;supsub.method2()=51(n++,n=52)
9-12 Super sup= new Super( );这次与上边的不同了,创建的是Super类型的对象,
sup.x=1;sup.n=50;sup.method1()=2;sup.method2()=50

编辑于 2017-05-19 01:52:26 回复(3)
先返回n,在执行n++、n--
发表于 2017-06-26 10:33:02 回复(0)
父类的静态方法不能被子类继承  所以8这一行的System.   out  .println(supsub.method2( ));调用的是父类的method n是被减一的 
发表于 2017-06-11 15:58:11 回复(1)
Sub   sub = new Sub(  );没有问题调用的都是Sub的方法
Super supsub= new Sub( );向上转型,还是Sub对象
Super sup= new Super( );定义的是Super对象
发表于 2017-05-25 11:45:11 回复(0)
还解析什么哦,遇到这种题目我都是直接跳过,***哦,填这么空,你干脆直接考API不就得了么
发表于 2017-06-20 09:09:19 回复(3)
n++ n-- 没考出这道题的目的,我错了 得到整错的答案,改成 ++n --n 应该好一点
重点是4-8 调用父类的静态方法,但n是子类的 对子类n做--操作
发表于 2017-07-15 09:53:36 回复(0)
4:是++x和x++的问题
7:子类重写了父类的method1()方法,那么父类引用在调用该方法时将会调用子类中重写的 method1() 。 
8:第四题错了的基础上,又知道静态变量在内存中只分配一块空间,会接着错。
9-12:不存在多态也没有继承,按照一般类的对象来做就可以了

发表于 2017-06-21 11:28:30 回复(0)
我去,居然改题,辣么长的题谁还有心情看第二遍啊!
发表于 2017-06-10 09:51:47 回复(0)