1.abstract class Name {
private String name;
public abstract boolean isStupidName(String name){}}
大侠们,这有何错误?
答案: 错。
abstract method 必须以分号结尾,且不带花括号。
2.public class Something {
void doSomething () {
private String s = "";
int l =s.length(); } }
有错吗?
答案: 错。
局部变量前不能放置任何访问修饰符 (private,public,和 protected)。
final 可以用来修饰局部变量(final 如同 abstract 和 strictfp,都是非访问修饰符,strictfp 只能修饰 class和 method 而非variable)。
3.abstract class Something {
private abstract String doSomething ();}
这好像没什么错吧?
答案: 错。
abstract 的 methods 不能以 private 修饰。abstract 的 methods 就是让子类implement(实现)具体细节的,
怎么可以用 private 把 abstract method 封锁起来呢? (同理,abstract method 前不能加final)。
4.public class Something {
public int addOne(final int x) { return ++x; }}
这个比较明显。
答案: 错。
int x 被修饰成 final,意味着 x 不能在 addOne method 中被修改。
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;}
和上面的很相似,都是关于 final 的问题,这有错吗?
答案: 正确。
在 addOne method 中,参数 o 被修饰成 final。如果在 addOne method 里
我们修改了 o 的 reference,(比如: o = new Other()