OC1

it2024-10-26  15

// // Dog.h // OC1_点语法 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> @interface Dog : NSObject //{ // NSString *_name; // NSInteger _age; //} - (id)init; //方法一 //- (NSString *)name; //- (NSInteger)age; // //- (void)setName:(NSString *)name; //- (void)setAge:(NSInteger)age; //方法二: //@property 关键字展开setter getter方法的声明 //- (NSString *)name; //- (void)setName:(NSString *)name; //@property NSString *name; //- (NSInteger)age; //- (void)setAge:(NSInteger)age; //@property NSInteger age; //方法三 //展开setter getter方法的声明及synthesize语句 //展开@synthesize name = _name; //@synthesize age = _age; @property NSString *name; @property NSInteger age; - (void)printDog; @end // // Dog.m // OC1_点语法 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "Dog.h" @implementation Dog - (id)init { if (self=[super init]) { _name = @"小黑"; _age = 12; } return self; } //方法一: //- (NSString *)name //{ // return _name; //} // //- (NSInteger)age //{ // return _age; //} // //- (void)setName:(NSString *)name //{ // _name = name; //} // //- (void)setAge:(NSInteger)age //{ // _age = age; //} //方法二 //@synthesize 关键字展开 setter getter方法的实现 //展开了 _name _age //@synthesize name=_name; //@synthesize age = _age; //方法三: 省约@synthesize - (void)printDog { NSLog(@"name = %@ , age = %li", [self name], [self age]); } @end // // main.m // OC1_点语法 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> #import "Dog.h" //点语法: 方便其他程序员转移到ios开发 int main(int argc, const char * argv[]) { @autoreleasepool { Dog *dog = [[Dog alloc] init]; [dog printDog]; [dog setName:@"小白"]; dog.name = @"小白";//<==> [dog setName:@"小白"] dog.age = 20; //<==> [dog setAge:20]; [dog printDog]; //<==> [dog name] [dog age]; NSLog(@"name = %@ age = %li", dog.name, dog.age); } return 0; }

 

转载于:https://www.cnblogs.com/0515offer/p/4581196.html

最新回复(0)