问题
单项选择题
下面程序段的输出结果为
public class Test
int a,b;
Test()
a=100;
b=200;
Test(int x,int y)
a=x;
b=y;
public static void main(String args[])
Test Obj1=new Test(12,45);
System.out.println(”a=”+Obj 1.a+” b=”+Obj 1.b);
Test Obj2=new Test();
System.out.println(”a=”+Obj 2.a+” b=”+Obj 2.b);
A.a=100 b=200
a=12 b=45
B.a=12 b=45
a=100 b=200
C.a=12 b=200
a=100 b=45
D.a=100 b=45
a=12 b=200
答案
参考答案:B
解析: 本题考查构造方法及构造方法重载。Test类有两个构造方法,即使用了方法重载技术。不带参数的构造方法对类的实例变量进行特定数值的赋值,而带参数的构造方法根据参数对类的实例变量进行赋值。TestObj1=new Test(12,45)语句调用的是 Test(int x,int y),而TestObj2=new Test()调用的是Test(),注意根据参数个数来区分。