在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
objective-c中提供类似JAVA的反射特性,给出基本例子如下: #import <Foundation/Foundation.h> @interface ClassA : NSObject{ int _id1; int _id2; int _id3; } @property int _id1; @property int _id2; @property int _id3; -(void) setId1:(int)id1 andId2:(int)id2 andId3:(int)id3; -(void) doMethod1; -(void) doMethod2; -(void) doMethod3; @end @implementation ClassA @synthesize _id1,_id2,_id3; -(void) setId1:(int)id1 andId2:(int)id2 andId3:(int)id3{ _id1 = id1; _id2 = id2; _id3 = id3; } -(void) doMethod1{ NSLog(@"%i", self._id1);} -(void) doMethod2{ NSLog(@"%i", self._id2);} -(void) doMethod3{ NSLog(@"%i", self._id3);} @end int main(int argc, const char * argv[]) { @autoreleasepool { Class class = NSClassFromString(@"ClassA"); //通过字符串获取CLASS NSLog(@"%@", [class className]); //打印class的名称,在oc中class是一个结构体 NSObject *tmp; SEL sel1 = @selector(doMethod2); //SEL对应选择一个方法 SEL sel2 = NSSelectorFromString(@"doMethod1"); SEL sel3 = NSSelectorFromString(@"setId1:andId2:andId3:"); tmp = [[class alloc]init];
|
请发表评论