点击上方“Java知音”,选择“置顶公众号”
技术文章第一时间送达!
作者:guanpj
www.jianshu.com/p/acc8d9a67d0cwww.jianshu.com/p/acc8d9a67d0c
现象描述****
原因分析****
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);
}
}
情景对比****
但是为什么对于 Kotlin 来说可以在匿名内部类中直接修改基本数据类型的值呢?查看 Kotlin 编译后反编译回来的内容:
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);
}
推荐阅读(点击即可跳转阅读)
**2. **面试题内容聚合
**3. **设计模式内容聚合
**4. **Mybatis内容聚合
**5. **多线程内容聚合
觉得不错?欢迎转发分享给更多人
我知道你 “在看”
原文始发于微信公众号(Java知音):终于明白为什么要加 final 关键字了!