#import <Foundation/Foundation.h>
@interface Human : NSObject {
int age;
NSString *name;
}
-(void)setAge:(int)a setName:(NSString *)n;
-(int)getAge;
-(NSString *)getName;
@end
@implementation Human
-(id)init {
if (self = [super init]) {
age = 20;
name = @"holy";
}
return self;
}
-(id)initWithAge:(int)a Name:(NSString *)n {
if (self = [super init]) {
age = a;
[name release];
name=[n copy];
}
return self;
}
-(void)dealloc{
NSLog(@"this is dealloc function");
[super dealloc];
}
-(void)setAge:(int)a setName:(NSString *)n {
age = a;
[name release];
name = [n copy];
}
-(int)getAge {
return age;
}
-(NSString *)getName {
return name;
}
@end
int main(int argc, const char * argv[]) {
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
Human * human1 = [[Human alloc] init];
NSLog(@"名字%@,年龄%d",[human1 getName],[human1 getAge]);
[human1 setAge:100 setName:@"GOD"];
NSLog(@"名字%@,年龄%d",[human1 getName],[human1 getAge]);
Human * human2 = [[Human alloc] initWithAge:100 Name:@"JACK"];
NSLog(@"名字%@,年龄%d",[human2 getName],[human2 getAge]);
[human1 release];
[human2 release];
[pool release];
return 0;
}
结果:
2016-12-02 11:51:44.053 test[7391] 名字holy,年龄20
2016-12-02 11:51:44.055 test[7391] 名字GOD,年龄100
2016-12-02 11:51:44.055 test[7391] 名字JACK,年龄100
2016-12-02 11:51:44.055 test[7391] this is dealloc function
2016-12-02 11:51:44.055 test[7391] this is dealloc function