main.m文件标签:stu,void,OC,Person,test,Student,使用,NSObject,import From: https://blog.51cto.com/u_15907570/5925323
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Person.h"
常用方法
void
Student *stu = [[[Studentalloc] init]autorelease];
// isKindOfClass判断对象是否属于某个类或者 子类
if ( [stu isKindOfClass:[Personclass]] ) {
// NSLog(@"stu属于Person或者继承自Person");
}
// isMemberOfClass判断对象是否属于某个类(不包括子类)
BOOL result = [stu isMemberOfClass:[Student class]];
// NSLog(@"%i", result);
// 直接调用
// [stu test];
// 间接调用
performSelector:@selector(test)];
// [stu performSelector:@selector(test2:) withObject:@"abc"];
// 延迟2秒后调用test2:方法
[stu performSelector:@selector(test2:)withObject:@"abc"afterDelay:2];
}
void
// 类的反射
NSString *str = @"Person";
Class class = NSClassFromString(str);
Person *person = [[class alloc] init];
NSLog(@"%@", person);
// Class变成字符串
NSString *name = NSStringFromClass([Personclass]);
// 方法的反射
NSString *method = @"test";
SEL selector = NSSelectorFromString(method);
performSelector:selector];
// 将SEL转换为字符串
NSString *selectorName = NSStringFromSelector(selector);
release];
}
int main(int argc,const char
{
@autoreleasepool {
reflect();
}
return 0;
}
Person.h文件
#import <Foundation/Foundation.h>
@interface Person :NSObject
- (void)test;
@end
Person.m文件
#import "Person.h"
Person
- (void)test {
NSLog(@"调用了Person的test方法");
}
@end
Student.h文件
#import "Person.h"
@interface Student :Person
- (void)test;
- (void)test2:(NSString
@end
Student.m文件
#import "Student.h"
Student
- (void)test {
NSLog(@"调用了test方法");
}
- (void)test2:(NSString
NSLog(@"调用了test2方法:%@", a);
}
@end