Declared inside the methods of the main class. Can only be used inside these methods. Unlike local classes, anonymous classes have no name. The main requirement for an anonymous class is that it must inherit an existing class or implement an existing interface. Cannot contain definition (but can inherit) static fields, methods and classes (except constants).
class OuterClass { public OuterClass() {} void methodWithLocalClass (final int interval) { // - listener // , // ActionListener ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println(" " + interval + " "); } }; Timer t = new Timer(interval, listener); // t.start(); } }
Anonymous classes are effectively used, as a rule, to implement (redefine) several methods and create their own object methods. This technique is effective when a method override is necessary, but creating a new class is not necessary because of the narrow scope (or one-time) application of the method.
id delegate =[NSObject newInstAnonClass:^{ ADD_METHOD(@selector(tableView:didSelectRowAtIndexPath:), @protocol(UITableViewDelegate), NO, ^(id selfObj,UITableView* tv,NSIndexPath* path) { NSLog(@"did select row %i",path.row); }); ADD_METHOD(@selector(tableView:willSelectRowAtIndexPath:), @protocol(UITableViewDelegate), NO, ^NSIndexPath*(id selfObj,UITableView* tv,NSIndexPath* path) { NSLog(@"will select row %i",path.row); return path; }); }]; self.tableView.delegate=delegate;
NSString *str = [@"Go" overrideMethod:@selector(description) blockImp:^NSString*(){ return @"Stop"; }]; NSLog(@"%@",str); ///log: Stop
NSMutableArray * array1 = [NSMutableArray arrayWithCapacity:10]; NSMutableArray * array2= [NSMutableArray arrayWithCapacity:10]; [array2 modifyMethods:^{ OVERRIDE(@selector(addObject:), ^(id arr,id anObject1) { NSLog(@"%@",[anObject1 description]); //[super addObject:anObject1] struct objc_super superInfo = {arr,[arr superclass]}; objc_msgSendSuper(&superInfo, @selector(addObject:),anObject1); }); OVERRIDE(@selector(insertObject:atIndex:), ^(id arr,id anObject,NSUInteger index) { NSLog(@"%@",[anObject description]); //[super insertObject:anObject atIndex:index]; struct objc_super superInfo = {arr,[arr superclass]}; objc_msgSendSuper(&superInfo, @selector(insertObject:atIndex:),anObject,index); }); }]; [array1 addObject:@"Test"]; [array2 addObject:@"One"]; [array2 addObject:@"Two"]; Log: // , addObject: insertObject:atIndex One One Two Two
UIView *tmpView = [[UIView allocAnonClass:^{ OVERRIDE(@selector(drawRect:), ^void(UIView *vie,CGRect rect){ CGContextRef context = UIGraphicsGetCurrentContext(); ... }); }] initWithFrame:CGRectMake(0, 0, 320, 480)];
// runtime newClassStr, [self class] newClass = objc_allocateClassPair([self class], [newClassStr UTF8String], 0); // newClass: IMP Method .... // objc_registerClassPair(newClass);
[[UIView allocAnonClass:^{}] initWithFrame:] // // UIView_anon_0 c / [[UIView_anon_0 alloc] initWithFrame:]
[UIView newInstAnonClass:^{}] // // UIView_anon_0 c / [UIView_anon_0 new]; //[[UIView_anon_0 alloc] init];
UIView *tmpView=[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 480)]; [tmpView overrideMethod:@selector(drawRect:) blockImp:^void(UIView* selfView,CGRect rect){ NSLog(@"draw"); }]; [self.view addSubview:tmpView]; //drawRect tmpView , .. , ; setNeedsDisplay ... [tmpView drawRect:CGRectZero]; // UIView * tmpView2 =[[[tmpView class] alloc] initWithFrame:CGRectMake(0, 100, 320, 380)]; [self.view addSubview:tmpView2]; // tmpView2 drawRect
// sel c blockIMP BOOL OVERRIDE(SEL sel,id blockIMP); // sel, p blockIMP BOOL ADD_METHOD(SEL sel,Protocol *p, BOOL isReq, id blockIMP); // sel, blockIMP BOOL ADD_METHOD_C(SEL sel,Class c,id blockIMP);
Button.OnClickListener mTakePicSOnClickListener = new Button.OnClickListener() { @Override public void onClick(View v) { //body } };
UIOnClickListener *listener =[UIOnClickListener newInstAnonClass:^{ OVERRIDE(@selector(onClick:), ^void(id selfObj,UIButton* sender){ //body }); }];
Source: https://habr.com/ru/post/170265/
All Articles