当前位置:Linux教程 - Linux文化 - 有道Scjp的模拟题搞不懂

有道Scjp的模拟题搞不懂


>>> 此贴的回复 >> 模拟器不是有给出答案的么?

>>> 此贴的回复 >> 答案的解释是这样子的 D is correct. A subclass in another package can only access protected members in the superclass via references of its own type or a subtype. In the question above the class SubclassY defines two instance variables, one of its own type (objY) and another of its superclass's type (objX). Hence there is no problem in accessing the method superclassMethodX() at line 9 but there will be a compilation error at line 11 as objX is not allowed to access any protected member of the SuperclassX.

但我的答案和测试都是会在line 9出问题啊,而不是在line11

>>> 此贴的回复 >> protected只能让同一个包或其它包的子类访问.上面的代码在编译时编译器就会检查objX的声明类型(这里objX为SuperclassX类型).因为上面两个类是在不同包的,但又不是其子类所以编译器认为objX不能访问protected int superclassVarX. 你把SuperclassX objX = new SubclassY()改成SubclassY objX = new SubclassY()后编译就通过了 line9是没问题的啊 (个人意见说的不对请原谅)

>>> 此贴的回复 >> superclassMethodX()是父类包内可见的方法,子类并不在同一个包内的类应该是不可以调用的 我同意答案是A (个人意见)

>>> 此贴的回复 >> 11.i = objX.superclassVarX 注意这行 不是在packageX包中,objX也不是子类对象,所以不能编译