-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.m
98 lines (78 loc) · 1.91 KB
/
person.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#import "person.h"
#pragma mark Extension
@interface Person ()
@property NSString *firstName;
@property NSString *lastName;
+ (instancetype)createWithName:(NSString *)name;
@end
#pragma mark -
#pragma mark Person implementation
@implementation Person {
NSString *_nickName;
@private int _age;
}
#pragma mark Lifecycle methods
+ (void)initialize {
NSLog(@"called in initialize");
}
- (void)dealloc {
NSLog(@"===========> dealloc");
// If not using ARC, make sure to release class variable objects
[_nickName release];
// and call parent class dealloc
[super dealloc];
}
#pragma mark Constructor methods
- (id)init {
self = self = [super init];
// Check parent class init success, may return nil
if (self) {
_nickName = @"default";
_age = 0;
}
return self;
}
// The parameters are also part of the method signature!
- (id)initWithNickname:(NSString *)nickName age:(int)ageArg {
if ([super init]) {
_nickName = nickName;
_age = ageArg;
}
return self;
}
#pragma mark Career protocol
@synthesize retired = _retired;
- (void)punchIn {
NSLog(@"punch in.");
}
- (void)performWork {
NSLog(@"do some work.");
}
- (void)punchOut {
NSLog(@"punch out.");
}
#pragma mark Class methods
+ (instancetype)createWithName:(NSString *)name {
Person *p = [[self alloc] init];
p.name = name;
NSLog(@"createWithName %@", name);
return p;
}
#pragma mark Instance methods
- (void)setFirstName:(NSString *)firstName {
_firstName = firstName;
NSLog(@"setFirstName %@", firstName);
}
- (void)changeName:(NSString *)name {
[self setName: name];
NSLog(@"change name to %@", _name);
}
- (void)changeRetireStatus:(BOOL)retired {
_retired = retired;
NSLog(@"change retired status to %d", retired);
}
- (void)sayHi:(NSString *)greeting message:(NSString *)msg {
NSLog(@"%@, %@ %@ aka %@(%li)! Welcome to the world of %@!",
greeting, _name, _firstName, _nickName, _age, msg);
}
@end