标签:person test style 指针 type 执行 blog 不能 错误
Person *p = [Person new];
这一特性是程序直到执行时才确定对象所属的类
id obj = [Person new];
//定义NSObject * 类型 NSObject* obj = [Cat new]; Cat *c = (Cat*)obj; [c eat];
/// Represents an instance of a class. struct objc_object { Class isa OBJC_ISA_AVAILABILITY; }; /// A pointer to an instance of a class. typedef struct objc_object *id;
id obj = [C at new]; [obj eat]; // 不用强制类型转换 [obj test]; //可以调用私有方法
注意:
虽然说id数据类型可以存储任何类型的对象,但是不要养成滥用这种通用类型
动态类型判断类型
Person *p = [Person new]; Student *stu = [Student new]; BOOL res = [p isKindOfClass:[Person class]]; NSLog(@"res = %i", res); // YES res = [stu isKindOfClass:[Person class]]; NSLog(@"res = %i", res); // YES
Person *p = [Person new]; Student *stu = [Student new]; BOOL res = [p isMemberOfClass:[Person class]]; NSLog(@"res = %i", res); // YES res = [stu isMemberOfClass:[Person class]]; NSLog(@"res = %i", res); // NO
BOOL res = [Person isSubclassOfClass:[Student class]]; NSLog(@"res = %i", res); // NO res = [Student isSubclassOfClass:[Person class]]; NSLog(@"res = %i", res); // YES
标签:person test style 指针 type 执行 blog 不能 错误
原文地址:http://www.cnblogs.com/xufengyuan/p/6575555.html