主要方法和类型
Class 变量名 = [类或者对象 class];
Class 变量名 = [类或者对象 superclass];
Class 变量名 = NSClassFromString(方法名字的字符串);
NSString *变量名 = NSStringFromClass(Class 参数);
•通过类名的字符串形式实例化对象
Class class = NSClassFromString(@"Student");
id obj = [[class alloc]init];
NSLog(@"创建的对象为: %@",obj);
将类名转换为字符串
NSString *className = NSStringFromClass(class);
NSLog(@"类名:%@",className);
对象在运行时获取其类型的能力称为内省
ObjC具有很多动态特性
而在实际使用中,使用introspection来确定该对象的实际所属类:
NSObject 提供了一些简单的查询运行时系统信息的方法:
- (BOOL)isKindOfClass:(Class)aClass
判断对象是不是属于指定类型或其子类
- (BOOL)isMemberOfClass:(Class)aClass
判断对象是不是属于指定类型
+ (BOOL)isSubclassOfClass:(Class)aClass;
判断一个类型是否是另一个类型的子类型
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
判断某个类/对象是否实现某个协议
使用@protocol封装后传入
// - (BOOL)conformsToProtocol:(Protocol *)aProtocol;
[student conformsToProtocol:@protocol(MyProtocol)];
// 或者使用类方法
// + (BOOL)conformsToProtocol:(Protocol *)protocol;
[Student conformsToProtocol:@protocol(MyProtocol)];
对象复合(composition),即对象中又包含对象,表达的时“has a”的关系;
向对象中的对象发送消息:使用嵌套的中括号,如:
[tom cardWithNumber:9999]draw:1000];
访问对象中的对象的属性,使用连续的点操作符,如:
tom.car.balance
注意:中括号和点操作符不能同时使用!!
Person *p = [[Person alloc]init];
Class c = [p class];
// NSLog(@"第一个%@",c);
Teacher *t=[[Teacher alloc]init];
c = [t class];
// NSLog(@"wo shi %@",c);
Student *tom = [[Student alloc]init];
c = [tom class];
// NSLog(@"第二个%@",c);
Student *jack = [[Student alloc]init];
Class j = [jack superclass];
NSLog(@"第三个%@",j);
if ([tom isMemberOfClass:[Person class]]) {
NSLog(@"tom is a person");
}
else{
NSLog(@"tom is not a person");
}
if ([jack isMemberOfClass:[Student class]])
{
NSLog(@"jack is a student");
}
else
{
NSLog(@"jack is not a student");
}
//测试class的反射
Class class = NSClassFromString(@"Student");
id obj = [[class alloc]init];
NSLog(@"创建的对象为: %@",obj);
NSString *className = NSStringFromClass(class);
NSLog(@"类名:%@",className);
//调用方法
//Person *p = [[Person alloc]init];
if ([p respondsToSelector:@selector(speak)]) {
[p performSelector:@selector(speak)];
}
if ([p respondsToSelector:@selector(speak)]) {
NSLog(@"speak");
}
if ([p respondsToSelector:@selector(setAge:)]) {
NSLog(@"setAge");
}
if ([p respondsToSelector:@selector(speakWith:at:)]) {
Person *p2 = [[Person alloc]init];
[p speakWith:p2 at:@"KFC"];
[p performSelector:@selector(speakWith:at:) withObject:p2 withObject:@"中餐厅"];
}
//isSubclassOfClass的使用
BOOL flag = [Person isSubclassOfClass:[Student class]];
if (flag) {
NSLog(@"yes");
}
else{
NSLog(@"no");
}
BOOL flag2 = [Student isSubclassOfClass:[Person class]];
if (flag2) {
NSLog(@"yes");
}
else
{
NSLog(@"no");
}
//判断Person类是否实现了MyProtocol协议
if ([tom conformsToProtocol:@protocol(MyProtocol)]) {
NSLog(@"tom 实现了协议");
}
Class c = [p class];
// NSLog(@"第一个%@",c);
Teacher *t=[[Teacher alloc]init];
c = [t class];
// NSLog(@"wo shi %@",c);
Student *tom = [[Student alloc]init];
c = [tom class];
// NSLog(@"第二个%@",c);
Student *jack = [[Student alloc]init];
Class j = [jack superclass];
NSLog(@"第三个%@",j);
if ([tom isMemberOfClass:[Person class]]) {
NSLog(@"tom is a person");
}
else{
NSLog(@"tom is not a person");
}
if ([jack isMemberOfClass:[Student class]])
{
NSLog(@"jack is a student");
}
else
{
NSLog(@"jack is not a student");
}
//测试class的反射
Class class = NSClassFromString(@"Student");
id obj = [[class alloc]init];
NSLog(@"创建的对象为: %@",obj);
NSString *className = NSStringFromClass(class);
NSLog(@"类名:%@",className);
//调用方法
//Person *p = [[Person alloc]init];
if ([p respondsToSelector:@selector(speak)]) {
[p performSelector:@selector(speak)];
}
if ([p respondsToSelector:@selector(speak)]) {
NSLog(@"speak");
}
if ([p respondsToSelector:@selector(setAge:)]) {
NSLog(@"setAge");
}
if ([p respondsToSelector:@selector(speakWith:at:)]) {
Person *p2 = [[Person alloc]init];
[p speakWith:p2 at:@"KFC"];
[p performSelector:@selector(speakWith:at:) withObject:p2 withObject:@"中餐厅"];
}
//isSubclassOfClass的使用
BOOL flag = [Person isSubclassOfClass:[Student class]];
if (flag) {
NSLog(@"yes");
}
else{
NSLog(@"no");
}
BOOL flag2 = [Student isSubclassOfClass:[Person class]];
if (flag2) {
NSLog(@"yes");
}
else
{
NSLog(@"no");
}
//判断Person类是否实现了MyProtocol协议
if ([tom conformsToProtocol:@protocol(MyProtocol)]) {
NSLog(@"tom 实现了协议");
}