4 java
你好
,方法/构造函数参数的最终修饰符有什么用?
例如:
class someClass {
private final double some; // I understand the use of final in this context, immutability etc..
public someClass(final double some) {
// I don't understand what purpose is served by making "some" final
this.some = some;
}
public void someMethod(final double some) {
// I don't understand what purpose is served by making "some" final
}
}
Run Code Online (Sandbox Code Playgroud)
您需要时有两种主要情况:
1)你想在本地类(通常是匿名类)中使用参数,如:
public void foo(final String str) {
Printer p = new Printer() {
public void print() {
System.out.println(str);
}
};
p.print();
}
Run Code Online (Sandbox Code Playgroud)
2)你喜欢这种风格,当每个未修改的变量都标有final单词时(保持尽可能多的东西通常是一种很好的做法).
| 归档时间: |
|
| 查看次数: |
186 次 |
| 最近记录: |