publicclassEmployee{privateStringname;publicEmployee(Stringname){this.name=name;}publicStringgetName(){returnname;}}publicclassManagerextendsEmployee{privateStringdepartment;publicManager(Stringname,Stringdepartment){this.department=department;super(name);(应于上一行掉位置)System.out.println(getName());}}Super的位置是否在方法的首行执行语句newManager(smith”,”SALES”)后程序的输出是哪项?()
A.smith
B.null
C.SALES
D.编译错误
第1题:
A.Thecodewillcompilewithoutchanges.
B.ThecodewillcompileifpublicTree(){Plant();}isaddedtotheTreeclass.
C.ThecodewillcompileifpublicPlant(){Tree();}isaddedtothePlantclass.
D.ThecodewillcompileifpublicPlant(){this(”fern”);}isaddedtothePlantclass.
E.ThecodewillcompileifpublicPlant(){Plant(”fern”);}isaddedtothePlantclass.
第2题:
A.public Person(){}
B.public Person(String name,int age) { this.name = name; this.age = age; }
C.public Person(int age,String name) { this.age = age; this.name = name; }
D.public Person(String name) { this.name = name; }
第3题:
3、下列程序的运行结果是【 】 class Demo{ private String name; Demo(String name){this.name = name;} private static void show(){ System.out.println(name) } public static void main(String[] args){ Demo d = new Demo(“lisa”); d.show(); } }
A.输出lisa
B.输出null
C.输出name
D.编译失败,无法从静态上下文中引用非静态变量name
第4题:
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
这个错误不容易发现。
错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个
interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。
任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static
final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new
Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的
reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final
的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"
这里显示有错。
第5题:
23、下列程序的运行结果是【 】 class Demo{ private String name; Demo(String name){this.name = name;} private static void show(){ System.out.println(name) } public static void main(String[] args){ Demo d = new Demo(“lisa”); d.show(); } }
A.输出lisa
B.输出null
C.输出name
D.编译失败,无法从静态上下文中引用非静态变量name