创建父类ClassA,子类ClassB继承ClassA
ClassA.h
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
- (void)method:(int)count;
@end
ClassA.m
#import "ClassA.h"
@implementation ClassA
- (void)method:(int)count
{
NSLog(@"%d",count);
}
@end
main.m
#import <Foundation/Foundation.h>
#import "ClassB.h"
#import "ClassA.h"
int main(int argc, const char *argv[])
{
@autoreleasepool
{
/*_______________________________对象与类的关系________________________*/
//构建类名
Class className = NSClassFromString(@"ClassA");
Class className2 = [ClassA class];
//1.查看某个对象是否是某个类的对象
ClassB *b = [[ClassB alloc]init];
BOOL isYes1 = [b isMemberOfClass:[ClassB class]];
BOOL isYes2 = [b isMemberOfClass:[ClassA class]];
NSLog(@"b %@ ClassB 的对象",isYes1?@"是":@"不是");
NSLog(@"b %@ ClassA的对象",isYes2?@"是":@"不是");
//查看某个对象是否是某个类的派生对象 -> b是否是B的或B的父类的对象
BOOL isYes3 = [b isKindOfClass:[ClassB class]];
BOOL isYes4 = [b isKindOfClass:[ClassA class]];
NSLog(@"b %@ ClassB的派生对象",isYes3?@"是":@"不是");
NSLog(@"b %@ ClassA的派生对象",isYes4?@"是":@"不是");
/*+____________对象间的关系____________________________________________*/
NSObject *objc1 = [[NSObject alloc]init];
NSObject *objc2 = [[NSObject alloc]init];
NSLog(@"%p %p",string1,string2);
BOOL isEqualString = [string1 isEqualTostring:string2];
NSLog(@"两个字符串内容%@",isEqualString?@"相同":@"不同");
/*_______________________对象与方法____________________________________*/
[b method:10];
//判断某个对象是否响应某方法 --> 对象是否能调用该方法
//1.
SEL sel = @selector(method:);
BOOL isTrue = [b respondsToSelector:sel];
//2.
BOOL isTrue1 = [b respondsToSelector:@selector(method:)];
NSLog(@"对象 b %@ 方法 method",isTrue1?@"响应":@"不响应");
}
return 0;
}