码迷,mamicode.com
首页 > 其他好文 > 详细

第二讲实例变量可见度,set/get方法,自定义初始化(代码) 及description的使用

时间:2015-08-29 15:16:12      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

一.实例变量可见度,setter和getter方法,自定义初始化的综合使用实例

   Person.h文件

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Person : NSObject
 4 {
 5     @private
 6     NSString *_name;
 7     int _age;
 8     @protected
 9     NSString *_sex;
10     NSString *_hobby;
11     @public
12     NSString *_address;
13     BOOL _isMarried;
14 }
15 -(void)initWithName:(NSString*)newName age:(int)newAge;
16 
17 -(id)initWithName:(NSString*)newName age:(int)newAge sex:(NSString*)sex;
18 
19 -(void)setName:(NSString*)name;
20 -(NSString*)name;
21 
22 -(void)setAge:(int)age;
23 -(int)age;
24 
25 -(void)setHobby:(NSString*)hobby;
26 -(NSString*)hobby;
27 
28 -(void)setAddress:(NSString*)address;
29 -(NSString*)address;
30 
31 -(void)setSex:(NSString*)sex;
32 -(NSString*)sex;
33 
34 -(void)setIsmarried:(BOOL)ismarried;
35 -(BOOL)ismarried;
 
42 @end

 

    Person.m文件

 1 #import "Person.h"
 2 
 3 @implementation Person
 4 
 5 -(void)initWithName:(NSString*)newName age:(int)newAge
 6 {
 7     _name = newName;
 8     _age = newAge;
 9 }
10 
11 -(id)initWithName:(NSString*)newName age:(int)newAge sex:(NSString*)newSex
12 {
13     _name = newName;
14     _age = newAge;
15     _sex = newSex;
16     
17     return self;
18 }
19 
20 -(void)setName:(NSString*)name
21 {
22     _name = name;
23 }
24 -(NSString*)name
25 {
26     return _name;
27 }
28 
29 -(void)setAge:(int)age
30 {
31     _age = age;
32 }
33 -(int)age
34 {
35     return _age;
36 }
37 
38 -(void)setHobby:(NSString*)hobby
39 {
40     _hobby = hobby;
41 }
42 -(NSString*)hobby
43 {
44     return _hobby;
45 }
46 
47 -(void)setAddress:(NSString*)address
48 {
49     _address = address;
50 }
51 -(NSString*)address
52 {
53     return _address;
54 }
55 
56 -(void)setSex:(NSString*)sex
57 {
58     _sex = sex;
59 }
60 -(NSString*)sex
61 {
62     return _sex;
63 }
64 
65 -(void)setIsmarried:(BOOL)ismarried
66 {
67     _isMarried = ismarried;
68 }
69 -(BOOL)ismarried
70 {
71     return _isMarried;
72 }
73 
74 //description 方法的默认实现是返回值和对象的内存地址,通过重写descripton 方法能够起到快速输出的作用
75 -(NSString*)description
76 {
77     return [NSString stringWithFormat:@"%@ %d %@",_name,_age,_sex];
78 
79 }

86 @end

 

    main.m文件

 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 #import "Person.h"
 5 
 6 
 7 int main(int argc, const char * argv[]) {
 8     @autoreleasepool {
 9         
10         
11         Person *p1 = [[Person alloc]init];
12         [p1 setName:@"刘亦菲"];
13         [p1 setAge:20];
14         [p1 setAddress:@"香港"];
15         [p1 setIsmarried:YES];
16         NSString *_name = [p1 name];
17         int _age = [p1 age];
18         NSString *_address = [p1 address];
19         BOOL _isMarried = [p1 ismarried];
20         
21         NSLog(@"%@  %d %@ %hhd" ,_name,_age,_address,_isMarried);
22         
23         NSLog(@"%@ %d %@ %hhd",[p1 name],[p1 age],[p1 address],[p1 ismarried]);
24 
25         Person *p2 = [[Person alloc]initWithName:@"小明" age:16  sex:@""];
26         //初始化标准输出格式
27         NSLog(@"%@  %d  %@",[p2 name],[p2 age],[p2 sex]);
28         
29          //通过重写descripton 方法能够起到快速输出的作用
30         NSLog(@"%@",p2);
38 
39     }
40     return 0;
41 }

 

第二讲实例变量可见度,set/get方法,自定义初始化(代码) 及description的使用

标签:

原文地址:http://www.cnblogs.com/erdeng/p/4769111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!