标签:
objective-C五大特性:
#import <Foundation/Foundation.h> //:表示公有继承 @interface Person : NSObject { //属性,实例变量,默认权限为protected int age; //对象声明时必须使用* NSString * name; } //生成set和get方法 @property (nonatomic,assign) int age; // - 表示实例的方法(消息),+ 表示类的方法(消息)static - (void)eat;
- (void)setName:(NSString *)nameValue;
@end
#import "Person.h" @implementation Person @synthesize age; - (void)eat { NSLog(@"eat function called"); } - (void)setName:(NSString *)nameValue { if (nameValue) { //指针不能直接赋值,否则释放时会出现问题 if (name != nameValue) { name = nil; name = [NSString stringWithString:nameValue]; } } } @end
#import "Person.h" #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //alloc为+方法,init为-方法 Person * p = [[Person alloc]init]; [p eat]; //oc字符串必须以@开头 [p setName:@"yangjing"]; } return 0; }
标签:
原文地址:http://www.cnblogs.com/jenayfighting/p/5339351.html