JAVA程序改错 (易错题)

  1 JAVA程序改错 
  2 1. 
  3     abstract class Name {    
  4       private String name; 
  5     public abstract boolean isStupidName(String name) {
  6    } 
  7 } 
  8 答案: 错。abstract method必须以分号结尾,且不带花括号。  
  9 2. 
 10 public class Something {    
 11     void doSomething () {        
 12      private String s = "";     
 13      int l = s.length();   
 14    } 
 15 } 
 16 答案: 错。局部变量前不能放置任何访问修饰符 (privatepublic,和protected)。final可以用来修饰局部变量  
 17 3.
 18    abstract class Something { 
 19    private abstract String doSomething ();
 20 
 21    } 
 22 答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract method封锁起来呢? 
(同理,abstract method前不能加final)。 23 4. 24 public class Something { 25 public int addOne(final int x) { 26 return ++x; 27 } 28 29 } 30 答案: 错。int x被修饰成final,意味着x不能在addOne method中被修改。 31 5. 32 public class Something { 33 public static void main(String[] args) { 34 Other o = new Other(); 35 new Something().addOne(o); 36 37 } 38 public void addOne(final Other o) { 39 o.i++; 40 41 wk_ad_begin({pid : 21}); 42 wk_ad_after(21, function(){ 43 $('.ad-hidden').hide(); 44 }, function(){ 45 $('.ad-hidden').show();}); 46 47 2 48 } } 49 class Other { 50 public int i; 51 } 52 和上面的很相似,都是关于final的问题,这有错吗? 53 答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference(比如: o = new Other();),那么如同上例这题也是错的。
但这里修改的是o的member vairable(成员变量),而o的reference并没有改变。
54 6. 55 class Something { 56 int i; 57 public void doSomething() { 58 System.out.println("i = " + i); 59 } 60 } 61 有什么错呢? 62 答案: 正确。输出的是"i = 0"。int i属于instant variable (实例变量,或叫成员变量)。instant variable有default value。int的default value是0。 63 7. 64 class Something { 65 final int i; 66 public void doSomething() { 67 System.out.println("i = " + i); 68 69 } 70 71 } 72 和上面一题只有一个地方不同,就是多了一个final。这难道就错了吗? 73 74 75 答案: 错。final int i是个final的instant variable (实例变量,或叫成员变量)。
final的instant variable没有default value,必须在constructor (构造器)结束之前被赋予一个明确的值。可以修改为"final int i = 0;" 76 8. 77 public class Something { 78 public static void main(String[] args) { 79 Something s = new Something(); 80 System.out.println("s.doSomething() returns " + 81 doSomething()); 82 83 } 84 public String doSomething() { 85 86 return "Do something ..."; 87 88 } 89 90 3 91 } 92 看上去很完美。 93 94 答案: 错。看上去在main里call doSomething没有什么问题,毕竟两个methods都在同一个class里。
但仔细看,main是static的。static method不能直接call non-static methods。
可改成"System.out.println("s.doSomething() returns " + s.doSomething());"。
同理,static method不能访问non-static instant variable。 95 96 9. 97 此处,Something类的文件名叫OtherThing.java class Something { 98 private static void main(String[] something_to_do) {
System.out.println("Do something ..."); 99 100 } 101 } 102 这个好像很明显。 103 答案: 正确。从来没有人说过Java的Class名字必须和其文件名相同。但public class的名字必须和文件名相同。 104 105 10106 interface A{ int x = 0; } 107 class B{ int x =1; } 108 class C extends B implements A { 109 110 public void pX(){ 111 112 System.out.println(x); 113 114 } 115 116 public static void main(String[] args) { 117 118 new C().pX(); 119 120 } 121 122 } 123 答案:错误。在编译时会发生错误(错误描述不同的JVM有不同的信息,意思就是未明确的x调用,两个x都匹配(就像在同时import java.util和java.sql两个包时直接声明Date一样)。
对于父类的变量,可以用super.x来明确,而接口的属性默认隐含为 public static final.所以可以通过A.x来明确。 124 125 11. 126 interface Playable { 127 void play(); 128 129 } 130 interface Bounceable { 131 132 4 133 void play(); 134 135 } 136 137 interface Rollable extends Playable, Bounceable { 138 139 Ball ball = new Ball("PingPang"); 140 141 } 142 class Ball implements Rollable { 143 144 private String name; 145 146 public String getName() { 147 148 return name; 149 150 } 151 public Ball(String name) { 152 153 this.name = name; 154 155 } 156 public void play() { 157 158 ball = new Ball("Football"); 159 160 System.out.println(ball.getName()); 161 162 } 163 164 } 165 这个错误不容易发现。 166 答案: 错。"interface Rollable extends Playable, Bounceable"没有问题。interface可继承多个interfaces,所以这里没错。问题出在: 167 interface Rollable里的"Ball ball = new Ball("PingPang");"168 任何在interface里声明的interface variable (接口变量,也可称成员变量),默认为public static final。也就是说: 169 Ball ball = new Ball("PingPang");实际上是 170 public static final Ball ball = new Ball("PingPang");。 171 在Ball类的Play()方法中,"ball = new Ball("Football");"
改变了ball的reference,而这里的ball来自Rollable interface,Rollable interface里的ball是public static final的,final的object是不能被改变reference的。
因此编译器将在 ball = new Ball("Football"); 这里显示有错。

 

posted @ 2016-11-27 15:26  LXQLCCC  Views(5682)  Comments(0Edit  收藏  举报