问两道java题目,别笑话我
1.以下程序的输出是
class ValueConversion
{
public int i = 15;
}
public class BankValue
{
public static void main(String argv[])
{
BankValue tvar = new BankValue();
tvar.first();
}
public void first()
{
int i = 5;
ValueConversion v = new ValueConversion();
v.i = 25;
second(v, i);
System.out.println(v.i);//不明白这个地方为什么会输出20?
}
public void second(ValueConversion v, int i)
{
i = 0;
v.i = 20;
ValueConversion val = new ValueConversion();
v = val;
System.out.println(v.i + " " + i);
}
}
A.15 0
20
B.20 0
20
C.15 0
15
D.0 15
20
2.以下哪种说法是正确的?
I. A top-level class may be public, abstract or final.
II. A protected class can be extended by classes in other packages as well.
III. Marking a class final prevents it from being extended.
IV. A static class can be extended by another static class only.
A.I, II, and III
B.I, III, and IV
C.I and III//感觉选C,不知道对不对?
D.All of the above
3.Which of the folllowing is the disadvantage of using Serialization to achieve deep cloning?
A.More expensive
B.All objects may not be serializable
C.Not simple to implement for deep cloning
D.All of the above
------解决方案--------------------2.第一个是对的
顶层类(非内部类)只可以处于 公开 或 默认的访问级别。不能用protected和private来修饰。(修饰顶层类的修饰符包括abstract,public和final,而static,protected和private不能修饰顶层类;成员方法和成员变量可以有多种修饰符,而局部变量只能用final修饰。)
------解决方案--------------------2. A
Java code
package examples.base;
public class A {
protected static class AA {
}
}
------解决方案--------------------
1.在传递参数时,引用类型传递的其实是引用的地址,当通过这个地址改变对象中的属性值时,原对象的属性也会相应改变,所以到v.i = 20;时,此v指向的地址就是first()中的v。但当second()中为v重新new了一个ValueConversion时,v不在指向first()中的对象,虽然second()中的v.i改为了15,frist()中的v.i仍然是20。也就是说再ValueConversion val = new ValueConversion();v = val;这里,两者的联系就断开了
2.好像是选C