java study notes 2019.6.13

Q: java anonymous class calls the principle of local variables
for example as follows:
Package Penalty for com.rupeng.ooptest10;

public class Test3 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    final int i = Integer.parseInt("123");
    final String s = Integer.toString(4433);
    DiQiuRen d1 = new DiQiuRen() {

        @Override
        public void speak() {
            // TODO Auto-generated method stub
            System.out.println("我是匿名类" + i + s);
        }
    };

    d1.welcome();
    d1.speak();
}

}

// decompile, we learned that the principle is not anonymous like local variables, but the anonymous class defines member variables and constructors have parameters, local variables are invoked argument constructor has the form of transfer
// class Test3 the extends DiQiuRen. 1 {$
// $ I int Val;
// String Val $ S;
// the Test3 $. 1 (int paramInt, o paramString String) {
// this.val I $ = paramInt;
// this.val $ S = o paramString;
} //
// @Override
// public void Speak () {
// // TODO Auto-Generated Method, Stub
// System.out.println ( "I am the anonymous class" + this.val $ i + this.val $ S);
//}
//}

Guess you like

Origin blog.51cto.com/14394144/2408185