31 public class LambdaExpr15 { 32 33 static int assertionCount = 0; 34 35 static void assertTrue(boolean cond) { 36 assertionCount++; 37 if (!cond) 38 throw new AssertionError(); 39 } 40 41 interface Block<T> { 42 void apply(T t); 43 } 44 45 public static void main(String[] args) { 46 //anon class 47 Block<Object> ba1 = t -> { 48 new Object() { 49 String get() { return ""; } 50 }; 51 assertTrue(t == 1); 52 }; 53 ba1.apply(1); 54 55 //local class 56 Block<Object> ba2 = t -> { 57 class A { 58 String get() { return ""; } 59 }; 60 new A(); 61 assertTrue(t == 2); 62 }; 63 ba2.apply(2); 64 assertTrue(assertionCount == 2); 65 } 66 } | 31 public class LambdaExpr15 { 32 33 static int assertionCount = 0; 34 35 static void assertTrue(boolean cond) { 36 assertionCount++; 37 if (!cond) 38 throw new AssertionError(); 39 } 40 41 interface Block<T> { 42 void apply(T t); 43 } 44 45 public static void main(String[] args) { 46 //anon class 47 Block<Object> ba1 = t -> { 48 new Object() { 49 String get() { return ""; } 50 }; 51 assertTrue((Integer)t == 1); 52 }; 53 ba1.apply(1); 54 55 //local class 56 Block<Object> ba2 = t -> { 57 class A { 58 String get() { return ""; } 59 }; 60 new A(); 61 assertTrue((Integer)t == 2); 62 }; 63 ba2.apply(2); 64 assertTrue(assertionCount == 2); 65 } 66 } |