------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
#import <Foundation/Foundation.h>
#import "Dog.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Animal *ani = [Animal new];
//ani是Animal类的实例对象
//动态类型检测:
//1.判断类型
//1)判断某个对象是否是类的实例对象,或者子类的实例对象(对象和类)
//isKindOfClass使用格式:[对象 isKindOfClass 类对象];
BOOL isInstance = [ani isKindOfClass:[Animal class]];
Dog *dog = [Dog new];
isInstance = [dog isKindOfClass:[Animal class]];
// 1 dog是Animal子类Dog的实例对象,所以是1
// ani = [Dog new];
isInstance = [ani isKindOfClass:[Animal class]];
//2)isMemberOfClass 判断对象是否是指定类的实例对象(对象和类)
//格式: [对象 isMemberOfClass: 类对象]
isInstance = [ani isMemberOfClass:[Animal class]];
isInstance = [dog isMemberOfClass:[Animal class]];
NSLog(@"%d",isInstance);
//3)isSu判断类是否是指定类的子类(类和类之间的关系)
//格式:[类A isSubclassOfClass:类B];
//判断 类A 是不是 类B 的子类,是返回 1,不是返回 0.
BOOL isSub = [Dog isSubclassOfClass:[Animal class]];
// 1 Dog继承自Animal
isSub = [[dog class] isSubclassOfClass:[ani class]];
// 后面的类对象必须获取出来,不能直接写类名,前面的可以直接写类名或获取的方法都可以
isSub = [Person isSubclassOfClass:[Animal class]];
// 0 Person 不是Animal的子类
isSub = [Animal isSubclassOfClass:[Dog class]];
// 0 继承顺序错误
NSLog(@"isSub = %d",isSub);
}
return 0;
}