ObjectiveC基础——构造方法和自定义构造方法

一、概述

构造方法是用来初始化对象的方法,属于对象方法。

重写构造方法:在对象创建出来的时候,成员变量可以带有初始值。

自定义构造方法:带有参数,创建对象时,可以指定初始化的值。

Person *p = [Person new];
等价于
Person *p = [ [Person alloc] init];
此时发生了以下三件事:
使用alloc方法分配存储空间,返回分配的对象。
使用init方法初始化对象。
放回对象的首地址。

注意:init方法就是构造函数,默认初始化完成后,所有成员变量的值都为0.

二、重写构造方法

重写构造方法的固定形式:

-(instancetype)init {
self=[super init];
if(self){
//书写自定义代码
}
return self;
}

具体问题:

现有Person类,要求所有Person对象实例化时年龄_age的初始值为1
Person.h

#import "Animal.h"

@interface Person : NSObject
{
    NSString *_name;
    int _age;
}
@property NSString *name;
@property int age;
@end

Person.m

#import "Animal.h"

@interface Person : NSObject
{
    NSString *_name;
    int _age;
}
@property NSString *name;
@property int age;
//-(instancetype) init;
@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *p = [[Person alloc] init];
        NSLog(@"初始化后的年龄是%d",p.age);//输出:初始化后的年龄是1
    }
    return 0;
}

说明:

1.[super init]的作用:
面向对象的体现,先利用父类的init方法为子类实例化的父类部分属性初始化。

2.self为什么要赋值为[super init]
是为了防止父类的初始化释放掉了self指向的空间并重新alloc了一块空间。super init有可能alloc失败,所以用if判断一下,如果失败了,if中的语句就不需要执行了。

三、自定义构造方法

要求:
1.是对象方法,以-开头。
2.返回值类型为id或instancetype类型
3.方法名以initWith开头
4.在声明文件中声明这个方法(重写init不需要声明。)

具体问题:
Animal类两个属性_name 和_age初始化是赋值,Dog类继承Animal,并添加了新的属性_num,Dog类初始化时为这三个属性赋值。

Animal.h

#import <Foundation/Foundation.h>

@interface Animal : NSObject
{
    int _age;
    NSString *_name;
}
@property int age;
@property NSString *name;
-(instancetype)initWithAge:(int)age andName:(NSString *)name;
-(void) run;
@end

Animal.m

#import "Animal.h"

@implementation Animal
//Animal 类的自定义构造方法
-(instancetype)initWithAge:(int)age andName:(NSString *)name{
    self = [super init];
    if(self){
        _age = age;
        _name = name;
    }
    return self;
}

-(void) run{
    NSLog(@"Animal running!");
}
@end

Dog.h

#import "Animal.h"

@interface Dog : Animal
{
    int _num;
}
@property int num;
-(instancetype) initWithAge:(int)age andName:(NSString *)name andnum:(int)num;
-(void) run:(int)speed;
-(void) eat;

@end

Dog.m

#import "Dog.h"

@implementation Dog
//Dog的构造方法通过super调用了父类的initWithAge:andName:的自定义构造方法。
//利用父类的自定义构造方法为子类实例化的父类部分属性初始化
-(instancetype) initWithAge:(int)age andName:(NSString *)name andnum:(int)num{
    self = [super initWithAge:age andName:name];
    if (self) {
        _num = num;
    }
    return self;
}
-(void)run:(int)speed{
    NSLog(@"the dog run with %d km/h",speed);
}
-(void)eat{
    NSLog(@"Dog eating!");
}
@end

main.m

#import <Foundation/Foundation.h>
#import "Dog.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {        
        Animal *a1 = [[Animal alloc] initWithAge:10 andName:@"小狗"];
        NSLog(@"%@,%d",a1.name,a1.age);//输出:小狗,10

        Dog *d1 = [[Dog alloc] initWithAge:12 andName:@"大黄" andnum:101];
        NSLog(@"%@,%d,%d",d1.name,d1.age,d1.num);//输出:大黄,12,101

    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值