其他
8年开发,一直不知道 Java为什么要加 final 关键字!
现象描述
原因分析
class TestInnerClass$1 extends InnerClass {
TestInnerClass$1(TestInnerClass var1, int var2, DataBean var3) {
super(var1);
this.this$0 = var1;
this.val$num = var2;
this.val$bean = var3;
}
void doSomething() {
super.doSomething();
System.out.println("num = " + this.val$num);
System.out.println("bean name is: " + this.val$bean.name);
}
}
情景对比
public final void useNestedClass(@NotNull final TestNestedClass.DataBean bean) {
Intrinsics.checkParameterIsNotNull(bean, "bean");
final IntRef num = new IntRef();//---1
num.element = 1;//---2
String var3 = "before action, num = " + num.element;
System.out.println(var3);
<undefinedtype> nestedClass = new TestNestedClass.NestedClass() {
public void doSomething() {
num.element = 678;//---3
bean.setName("xyz");
String var1 = "num = " + num.element;
System.out.println(var1);
var1 = "bean name is: " + bean.getName();
System.out.println(var1);
}
};
nestedClass.doSomething();
String var4 = "after action, num = " + num.element;//---4
System.out.println(var4);
}
public final void useNestedClass(@NotNull TestNestedClass.DataBean bean) {
Intrinsics.checkParameterIsNotNull(bean, "bean");
int num = 1;
String var3 = "before action, num = " + num;
System.out.println(var3);
int num = 678;
var3 = "after action, num = " + num;
System.out.println(var3);
}
往期推荐