objective-c之构造函数

//
//  Student.h
//  constructor_method
//
//  Created by wu jianhua on 16-7-29.
//  Copyright (c) 2016年 wujianhua. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
    @public
    int _age;
    int _no;
    
    @private
    int _name;
}

- (void)setAge:(int)age;

- (void)setNo:(int)no;

- (int)age;


- (int)no;

//construct method
- (id)initWithAge:(int)age andNo:(int)no;


- (NSString *)description;

- (void) test;

+ (void) test2;

@end
//
//  Student.m
//  constructor_method
//
//  Created by wu jianhua on 16-7-29.
//  Copyright (c) 2016年 wujianhua. All rights reserved.
//

#import "Student.h"

@implementation Student


- (void)setAge:(int)age
{
    _age=age;
}

-(int)age
{
    return _age;
}

- (void)setNo:(int)no
{
    _no=no;
}

- (int)no
{
    return _no;
}

- (id)initWithAge:(int)age andNo:(int)no
{
    self=[super init];//invoke super method
    if (self !=nil)
    {
        //_age=age;
       // _no=no;
        self.age=age;
        self.no=no;
    }
    return self;
    
}
- (NSString *)description
{
    NSString *str=   [NSString stringWithFormat:@"description age is %i and no is %i",_age,_no];
    
    return   str;
}

- (void)test
{
     _age=1024;
}
+ (void)test2
{
    //[self alloc];
    //[Student alloc];
}



@end

//
//  GoodStudent.h
//  constructor_method
//
//  Created by wu jianhua on 16-7-31.
//  Copyright (c) 2016年 wujianhua. All rights reserved.
//

#import "Student.h"

@interface GoodStudent : Student

@end
//
//  GoodStudent.m
//  constructor_method
//
//  Created by wu jianhua on 16-7-31.
//  Copyright (c) 2016年 wujianhua. All rights reserved.
//

#import "GoodStudent.h"

@implementation GoodStudent

- (void)test
{
    _age=10;
}
@end

//
//  main.m
//  constructor_method
//
//  Created by wu jianhua on 16-7-29.
//  Copyright (c) 2016年 wujianhua. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"
#import "GoodStudent.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //Student *stu=[[Student alloc] init];
        
        /*
         id obj=[Student alloc];
        Student *stu=[obj init];
        [stu release];
         */
        Student *stu=[[Student alloc] initWithAge:1024 andNo:12];
        NSLog(@"age is %i and no is %i",stu.age,stu.no);
        NSLog(@"%@",stu);
        
        NSString *str=@"itcast";
        NSLog(str);
        
        GoodStudent* gs=[[GoodStudent alloc] init];
        [gs test];
        NSLog(@"age is %@",gs);
        
        [stu release];
        
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值