初级人猿面试题02

菜鸟级

代码改错

1、
abstract class error{
private String name;
public abstract boolean isStupidName(String name) {
}
}

错。abstract method 必须以分号结尾,且不带花括号。

abstract class error{
private String name;
public abstract boolean isStupidName(String name);
}

2、
public class Something {
void doSomething () {
private String s = “”;
int l = s.length();
}
}
错。局部变量前不能放置任何访问修饰符 (private,public,和 protected)。final 可 以用来修饰局部变量
(final 如同 abstract 和 strictfp,都是非访问修饰符,strictfp 只能修饰 class 和 method 而非
variable)。

public class Something {
void doSomething () {
final String s = “”;
int l = s.length();
}
}

3、
abstract class Something {
private abstract String doSomething ();
}

错。abstract 的 methods 不能以 private 修饰。abstract 的 methods 就是让子类
implement(实现)具体细节的,怎么可以用 private 把 abstract method 封锁起来呢? (同理,abstract method 前不能加 final)。

abstract class Something {
abstract String doSomething ();
}

4、
public class Something {
public int addOne(final int x) {
return ++x;
}
}

错。int x 被修饰成 final,意味着 x 不能在 addOne method 中被修改。

public class Something {
public int addOne(int x) {
return ++x;
}
}

5、
public class Something {
public static void main(String[] args) { Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}
正确。在 addOne method 中,参数 o 被修饰成 final。如果在 addOne method 里我 们修改了 o 的 reference
(比如: o = new Other()?,那么如同上例这题也是错的。但这里修改的是 o 的 member vairable
(成员变量),而 o 的 reference 并没有改变。

6、
class Something {
int i;
public void doSomething() { System.out.println("i = “+ i);
}
}
正确。输出的是"i = 0”。int i 属於 instant variable (实例变量,或叫成员变量)。instant variable 有 default value。int 的 default value 是0。

7、
class Something {
final int i;
public void doSomething() { System.out.println("i = "+ i);
}
}

错。final int i 是个 final 的 instant variable (实例变量,或叫成员变量)。final 的 instant variable 没有 default value,必须在 constructor (构造器)结束之前被赋予一个明确的值。可 以修改为"final int i =0;"。

class Something {
int i;
public void doSomething() { System.out.println("i = "+ i);
}
}

8、
public class Something {
public static void main(String[] args) { Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public String doSomething() {
return “Do something …”;
}
}

错。看上去在 main 里 call doSomething 没有什么问题,毕竟两个 methods 都在同一 个 class 里。但仔细看,main 是 static 的。static method 不能直接 call non-staticmethods。 可改成"System.out.println(“s.doSomething()returns " + s.doSomething());”。同理,static method 不能访问 non-static instant variable。

public class Something {
public static void main(String[] args) { Something s = new Something();
System.out.println("s.doSomething() returns " + doSomething());
}
public static String doSomething() {
return “Do something …”;
}
}

9、
此处,Something 类的文件名叫 OtherThing.java class Something {
private static void main(String[] something_to_do){
System.out.println(“Dosomething …”);
}
}
正确。从来没有人说过 Java 的 Class 名字必须和其文件名相同。但 public class 的 名字必须和文件名相同。

10、
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){ System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}

错误。在编译时会发生错误(错误描述不同的 JVM 有不同的信息,意思就是未明确的 x 调用,两个 x 都匹配(就象在同时 import java.util 和 java.sql 两个包时直接声明 Date 一 样)。对于父类的变量,可以用 super.x 来明确,而接口的属性默认隐含为 public staticfinal. 所以可以通过 A.x 来明确。

interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){ System.out.println(super.x);
}
public static void main(String[] args) {
new C().pX();
}
}

11、
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 = newBall(“Football”); System.out.println(ball.getName());
}
}

错。“interfaceRollable extends Playable, Bounceable"没有问题。interface 可继承多 个 interfaces,所以这里没错。问题出在 interface Rollable 里的"Ball ball =new Ball(“PingPang”);”。任何在 interface 里声明的 interface variable (接口变量,也可称成员变 量),默认为 public static final。也就是说"Ball ball = new Ball(“PingPang”);“实际上是"public staticfinal Ball ball = new Ball(“PingPang”);”。在 Ball 类的 Play()方法中,"ball = newBall(“Football”);"改变了 ball 的 reference,而这里的 ball 来自 Rollable interface,Rollable interface 里的 ball 是 public static final 的,final 的 object 是不能被改变 reference 的。因此 编译器将在"ball = newBall(“Football”);"这里显示有错。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值