objective-c的构造函数测试

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值