//
// main.m
// OC_2初始化_便利构造器
//
// Created by YIem on 15/11/25.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

import "Zombie.h"

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


// 创建对象
Zombie *z = [[Zombie alloc] init];
NSLog(@"%@ %@ %@", [z hp], [z defense], [z attack]);
Zombie *z1 = [[Zombie alloc] init];
NSLog(@"%@ %@ %@", [z1 hp], [z1 defense], [z1 attack]);
// 自定义初始化方法
Zombie *z2 = [[Zombie alloc] initWithHp:@"12" defense:@"21" attack:@"23"];
NSLog(@"%@ %@ %@", z2.hp, z2.defense, z2.attack);

// 便利构造器创建

Zombie *z3 = [Zombie zombie];
Zombie *z4 = [Zombie zombieWithHp:@"111"  defense:@"222"  attack:@"333"];





return 0;

}



//
// BiockZombie.h
// OC_2初始化_便利构造器
//
// Created by YIem on 15/11/25.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Zombie.h"

@interface BiockZombie : Zombie
{

NSString *_hat;// 帽子

}
// getter&setter

  • (void)setHat:(NSString *)hat;
  • (NSString *)hat;

// 自定义初始化

  • (instancetype)initWithHp:(NSString *)hp

                   defense:(NSString *)defense
                    attack:(NSString *)attack
                       hat:(NSString *)hat;
    

@end


//
// BiockZombie.m
// OC_2初始化_便利构造器
//
// Created by YIem on 15/11/25.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "BiockZombie.h"

@implementation BiockZombie

  • (instancetype)initWithHp:(NSString *)hp

               defense:(NSString *)defense
                attack:(NSString *)attack
                   hat:(NSString *)hat

    {
    self = [super initWithHp:hp defense:defense attack:attack];
    if (self) {
    // [self setHat:hat];

    self.hat =hat;

    }
    return self;
    }

  • (void)setHat:(NSString *)hat
    {
    _hat = hat;
    }
  • (NSString *)hat
    {
    return _hat;
    }

@end



//
// Zombie.h
// OC_2初始化_便利构造器
//
// Created by YIem on 15/11/25.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface Zombie : NSObject
{

NSString *_hp;// 血量
NSString *_defense;// 防御
NSString *_attack;//攻击力

}
// getter&setter

  • (void)setHp:(NSString *)hp;
  • (NSString *)hp;
  • (void)setDefense:(NSString *)defense;
  • (NSString *)defense;
  • (void)setAttack:(NSString *)attack;
  • (NSString *)attack;

// 自定义初始化方法

  • (instancetype)initWithHp:(NSString *)hp

                   defense:(NSString *)defense
                    attack:(NSString *)attack;
    

// 变量构造器
// 1. 类方法
// 2. 以小写的类名开头

  • (instancetype)zombie;
  • (instancetype)zombieWithHp:(NSString *)hp

                     defense:(NSString *)defense
                      attack:(NSString *)attack;
    

@end


//
// Zombie.m
// OC_2初始化_便利构造器
//
// Created by YIem on 15/11/25.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "Zombie.h"

@implementation Zombie
// 初始化方法
// instancetype/id 泛型指针(任意对象)

  • (instancetype)init
    {
    // 1. 调用父类初始化 处理从父类继承来的成员变量
    self = [super init];
    // 2. 判断初始化是否成功
    if (self) {

    // 3. 初始化当前类内容
    _hp = @"100";
    _defense = @"100";
    _attack = @"100";

    }
    // 4. 初始化结束后 返回对象自己
    return self;
    }

// 自定义初始化

  • (instancetype)initWithHp:(NSString )hp defense:(NSString )defense attack:(NSString *)attack
    {
    self = [super init];
    if (self) {

    // 初始化

    // _hp = hp;
    // _defense = defense;
    // _attack = attack;

    // 消息语法

    // [self setHp:hp];

// [self setDefense:defense];
// [self setAttack:attack];

    // 点语法
    self.hp = hp;
    self.defense = defense;
    self.attack = attack;
}
return self;

}
// 便利构造器

  • (instancetype)zombie
    {
    Zombie *z = [[Zombie alloc] init];
    return z;
    }
  • (instancetype)zombieWithHp:(NSString *)hp

                 defense:(NSString *)defense
                  attack:(NSString *)attack

    {
    Zombie *z = [[Zombie alloc]initWithHp:hp defense:defense attack:attack];
    return z;
    }

//---

  • (void)setHp:(NSString *)hp
    {
    _hp = hp;
    }
  • (NSString *)hp
    {
    return _hp;
    }
  • (void)setDefense:(NSString *)defense
    {
    _defense = defense;
    }
  • (NSString *)defense
    {
    return _defense;
    }
  • (void)setAttack:(NSString *)attack
    {
    _attack = attack;
    }
  • (NSString *)attack;
    {
    return _attack;
    }

@end