acount.h
// // Acount.h // KenshinCui // // Created by hxd_mac on 15-6-9. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> @interface Acount : NSObject @property (nonatomic, assign) double balance; @end
acount.m
// // Acount.m // KenshinCui // // Created by hxd_mac on 15-6-9. // Copyright (c) 2015年 hxd198. All rights reserved. // #import "Acount.h" @implementation Acount @end
person.h
// // Person.h // KenshinCui // // Created by hxd_mac on 15-6-9. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> @class Acount; @interface Person : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) Acount *acount; - (Person *) initWithName : (NSString *) name; + (Person *) personWithName : (NSString *) name; - (void) showMessage : (NSString *) information; - (NSComparisonResult ) comparePerson : (Person *) person; @end
person.m
// // Person.m // KenshinCui // // Created by hxd_mac on 15-6-9. // Copyright (c) 2015年 hxd198. All rights reserved. // #import "Person.h" @implementation Person - (Person *) initWithName:(NSString *)name { if (self = [super init]) { self.name = name; } return (self); } + (Person *) personWithName:(NSString *)name { Person *person = [[Person alloc] initWithName:name]; return person; } - (void) showMessage:(NSString *)information { NSLog(@"my name is %@, the imformation is %@", _name, information); } - (NSComparisonResult) comparePerson:(Person *)person { return [_name compare:person.name]; } - (NSString *) description { return [NSString stringWithFormat:@"name = %@", _name]; } @end
main.m
// // main.m // KenshinCui // // Created by hxd_mac on 15-6-7. // Copyright (c) 2015年 hxd198. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { /*常用方法*/ Person *person1=[Person personWithName:@"Kenshin"]; NSLog(@"%i",[person1 isKindOfClass:[NSObject class]]); //判断一个对象是否为某种类型(如果是父类也返回YES),结果:1 NSLog(@"%i",[person1 isMemberOfClass:[NSObject class]]); //判断一个对象是否是某个类的实例化对象,结果:0 NSLog(@"%i",[person1 isMemberOfClass:[Person class]]); //结果:1 NSLog(@"%i",[person1 conformsToProtocol:@protocol(NSCopying)]);//是否实现了某个协议,结果:0 NSLog(@"%i",[person1 respondsToSelector:@selector(showMessage:)]);//是否存在某个方法,结果:1 [person1 showMessage:@"Hello,world!"];//直接调用一个方法 [person1 performSelector:@selector(showMessage:) withObject:@"Hello,world!"]; //动态调用一个方法,注意如果有参数那么参数类型只能为ObjC对象,并且最多只能有两个参数 /*反射*/ //动态生成一个类 NSString *className=@"Person"; Class myClass=NSClassFromString(className);//根据类名生成类 Person *person2=[[myClass alloc]init]; //实例化 person2.name=@"Kaoru"; NSLog(@"%@",person2);//结果:name=Kaoru //类转化为字符串 NSLog(@"%@,%@",NSStringFromClass(myClass),NSStringFromClass([Person class])); //结果:Person,Person //调用方法 NSString *methodName=@"showMessage:"; SEL mySelector=NSSelectorFromString(methodName); Person *person3=[[myClass alloc]init]; person3.name=@"Rosa"; [person3 performSelector:mySelector withObject:@"Hello,world!"]; //结果:My name is Rosa,the infomation is "Hello,world!". //方法转化为字符串 NSLog(@"%@",NSStringFromSelector(mySelector)); //结果:showMessage: } return 0; }
输出结果:
2015-06-09 21:43:20.202 KenshinCui[490:303] 1
2015-06-09 21:43:20.238 KenshinCui[490:303] 0
2015-06-09 21:43:20.240 KenshinCui[490:303] 1
2015-06-09 21:43:20.241 KenshinCui[490:303] 0
2015-06-09 21:43:20.243 KenshinCui[490:303] 1
2015-06-09 21:43:20.246 KenshinCui[490:303] my name is Kenshin, the imformation is Hello,world!
2015-06-09 21:43:20.248 KenshinCui[490:303] my name is Kenshin, the imformation is Hello,world!
2015-06-09 21:43:20.249 KenshinCui[490:303] name = Kaoru
2015-06-09 21:43:20.251 KenshinCui[490:303] Person,Person
2015-06-09 21:43:20.253 KenshinCui[490:303] my name is Rosa, the imformation is Hello,world!
2015-06-09 21:43:20.254 KenshinCui[490:303] showMessage: