Theme 4: lexical act on

Description lexical scope inner classes:

    Internal class variable names (and this) is very prone to error. Inherited members (including the methods from the Object) may class members will cover the outside (Shadow) by internal class, in addition to undefined (unqualified) of this reference point to their internal rather than external class class.

lambda expressions lexical scope:

    Relative to the inner class, semantics of the lambda expression will be very simple: it does not inherit any variable name from the superclass (supertype), and will not introduce a new scope. lambda expressions based on lexical scope, i.e. lambda expression variables in the function body and its external environment variables have the same semantics (including parameters in the form of a lambda expression). Further, 'the this' key and the references cited in the internal and external lambda expressions also have the same semantics .

public class TestA {
     Runnable run = ()-> System.out.println(toString());

    @Override
     public String toString() {
         return "toString...............";
     }

    public static void main(String[] args) {
         new TestA().run.run();
     }
}

 

 

   Based on the concept of lexical scope, lambda expressions can not override any local variables in their context , its flow behavior and those control structures (e.g. for loops and catch clause) have the same parameters .

int i = 0;

int I = 0;

for (int i = 1; i <10; i + = 1) {// Here will be a compilation error, because i has been declared in the external loop for

sum += i;

}

Guess you like

Origin www.cnblogs.com/zyj-468161691/p/12213639.html