zoukankan      html  css  js  c++  java
  • OC类的继承

    类的继承

    cmd + →: 光标跳到这一行的最后面

    cmd + ←: 光标跳到这一行的最前面

    cmd + ↑: 光标跳到这一行的最上面

    cmd + ↓: 光标跳到这一行的最下面

    alt + ←: 光标跳过一个单词的最后面

    alt + →: 光标跳过一个单词的最前面

     shift + alt + k: 

     Xcode插件管理器 Alcatra

    安装命令

    Xcode插件管理器 Alcatra

    安装命令

    curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/install.sh | sh

            继承: 子类可以继承父类的特征和行为

            OC继承的特点:

            1.单根继承: 只有一个根类(基类), NSObject

            2.单向继承: 每个类只有一个父类, 但可以有多个子类

            继承的好处: 把相同的特征和行为定义在父类中, 子类只需要继承父类就可以拥有这些特征和行为, 子类只需要写自己独有的特征和行为    

            普通僵尸

    Zombie.h
    //继承关系的体现
    @interface Zombie : NSObject {
    //血量, 攻击力, 移动速度
        NSInteger _HP;
        NSInteger _attact;
        CGFloat _speed;
    }
    
    - (void)setHP:(NSInteger)HP;
    - (NSInteger)HP;
    
    - (void)setAttact:(NSInteger)attact;
    - (NSInteger)attact;
    
    - (void)setSpeed:(CGFloat)speed;
    - (CGFloat)speed;
    
    //⾏⾛ 攻击 死亡
    - (void)move;
    - (void)eat;
    - (void)die;
    @end
    Zombie.m
    #import "Zombie.h"
    @implementation Zombie
    
    - (void)setHP:(NSInteger)HP {
        _HP = HP;
    }
    - (NSInteger)HP {
        return _HP;
    }
    
    - (void)setAttact:(NSInteger)attact {
        _attact = attact;
    }
    - (NSInteger)attact {
        return _attact;
    }
    
    - (void)setSpeed:(CGFloat)speed {
        _speed = speed;
    }
    - (CGFloat)speed{
        return _speed;
    }
    
    - (void)move {
        NSLog(@"移动");
    }
    - (void)eat {
        NSLog(@"真是美味");
    }
    - (void)die {
        NSLog(@"挂了");
    }
    @end
    main.m
    普通僵尸
            Zombie *zombie = [[Zombie alloc] init];
            //实例变量
            [zombie setHP:50];
            [zombie setAttact:5];
            [zombie setSpeed:100];
            NSLog(@"%ld %ld %.2lf", [zombie HP], [zombie attact], [zombie speed]);
            //方法
            [zombie move];
            [zombie eat];
            [zombie die];

            路障僵尸

            从父类继承过来的的实例变量(特征)

    RoadZombie.h
    #import "Zombie.h"
    @interface RoadZombie : Zombie {
        //装备
        NSString *_prop;
    }
    - (void)setProp:(NSString *)prop;
    - (NSString *)prop;
    //失去装备
    - (void)lostProp;
    @end
    RoadZombie.m
    #import "RoadZombie.h"
    @implementation RoadZombie
    - (void)setProp:(NSString *)prop{
        _prop = prop;
    }
    - (NSString *)prop {
        return  _prop;
    }
    - (void)lostProp {
         NSLog(@"%@装备掉了", _prop);
        _prop = @"";
    }
    //当从父类继承的方法, 不满足子类的需求时, 重写父类的方法
    //注: 重写父类的方法, 不需要写声明部分
    - (void)move {
        NSLog(@"摩擦,摩擦!");
    }
    @end
     main.m
    RoadZombie *roadZombie = [[RoadZombie alloc] init];
            [roadZombie setHP:70];
            [roadZombie setAttact:7];
            [roadZombie setSpeed:140];       
            NSLog(@"%ld %ld %.2lf", [roadZombie HP], [roadZombie attact], [roadZombie speed]);
            //从父类继承过来的的方法(行为)
            [roadZombie move];
            [roadZombie eat];
            [roadZombie die];  
            //自己独有的
            [roadZombie setProp:@"路障"];
            NSLog(@"%@", [roadZombie prop]);
            [roadZombie lostProp];

            铁桶僵尸

            从父类继承过来的的实例变量(特征)

    DrumZombie.h
    #import "RoadZombie.h"
    @interface DrumZombie : RoadZombie {
        NSString *_weakness;
    }                  
    - (void)setWeakness:(NSString *)weakness;
    - (NSString *)weakness;
    @end
    DrumZombie.m
    #import "DrumZombie.h"
    @implementation DrumZombie
    - (void)setWeakness:(NSString *)weakness {
        _weakness = weakness;
    }
    - (NSString *)weakness {
        return _weakness;
    }
    - (void)move {
        //super: 编译指令, 用于调用父类的方法
        [super move];
        NSLog(@"在光滑的地面上");
    }
    @end
    main.m
    DrumZombie *drumZombie = [[DrumZombie alloc] init];
            [drumZombie setHP:100];
            [drumZombie setAttact:10];
            [drumZombie setSpeed:180];
            [drumZombie setProp:@"铁桶"];
            NSLog(@"%ld %ld %.2lf %@", [drumZombie HP], [drumZombie attact], [drumZombie speed], [drumZombie prop]);
            
            //从父类继承过来的的方法(行为)
            [drumZombie move];
            [drumZombie eat];
            [drumZombie die];
            
            //自己独有的
            [drumZombie setWeakness:@"怕磁铁"];
            NSLog(@"%@", [drumZombie weakness]);
            [drumZombie lostProp];

            NSObject常见的方法

            1.+alloc, 开辟内存空间

            2.-init, 初始化方法(清空操作)

            3.-class, 获取对象的类型

            4.+class, 获取类的类型

            5.isKindOfClass: 判断对像是不是属于某个类

            6.isMemberOfClass: 判断对象是不是某个类的实例(对象)

     Zombie *a = [[Zombie alloc]init];
            NSLog(@"%@", [a class]);
            NSLog(@"%@", [Zombie class]);
            BOOL result = [a isKindOfClass:[zombie class]];
            result = [drumZombie isMemberOfClass: [drumZombie class]];
            NSLog(@"%hhd", result);

            方法调用的机制: 先从自身类中找方法, 如果找到, 就调用, 没有找到, 就去父类中找, 父类没有找到, 就去父类的父类中找, 直到找到方法, 并调用

            系统的init: 清空实例变量的值

            重写init方法: 当需要为实例变量赋初始值时

            自定义初始化方法: 根据外部传入的参数, 为实例变量赋值

    自定义初始化方法

    1.-号方法

    2.返回值是id或者instancetype(id: 相当于void *, 对象类型, 可以代指任意对象类型)

    3.必须以init开头

    4.第一个参数前加with

      - (instancetype)initWithName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender; 

     

    便利构造器

    1.+号方法

    2.返回值是id或者instancetype

    3.必须以类名开头

    4.第一个参数前加with

      + (instancetype)boyWithName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender; 

    Boy.m
    - (instancetype)initWithName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender {
        self = [super init];
        if (self) {
            _name = name;
            _age = age;
            _gender = gender;
        }
        return self;
    }
    
    + (instancetype)boyWithName:(NSString *)name age:(NSInteger)age gender:(NSString *)gender {
        //1
    //    Boy *boy = [[Boy alloc] init];
    //    [boy setName:name];
    //    [boy setAge:age];
    //    [boy setGender:gender];
        
        //2
        Boy *boy = [[Boy alloc] initWithName:name age:age gender:gender];
        return boy;
        
        //3
        return  [[Boy alloc] initWithName:name age:age gender:gender];
    
        //注: 2, 3种写法建立在写了自定义初始化方法的基础上
    }
    main.m
     Boy *boy = [[Boy alloc] init];
            NSLog(@"%@", [boy gender]);
            [boy setName:@"李四"];
            [boy setAge:20];
            [boy setGender:@"未知"];
            [boy sayHi];
            
            //自定义初始化方法, 初始化 + 实例变量赋值
            Boy *boy1 = [[Boy alloc] initWithName:@"叶良辰" age:25 gender:@""];
            [boy1 sayHi];
            
            //便利构造器 = 开辟内存空间 + 初始化 + 实例变量赋值
            Boy *boy3 = [Boy boyWithName:@"福尔康" age:38 gender:@"你猜"];
            [boy3 sayHi];

     

    The one who wants to wear a crown must bear the weight!
  • 相关阅读:
    struts2文件上传下载
    struts2自定义拦截器
    JSP_Servlet 解决中文乱码登录问题
    ajax提交form表单
    sql语句大全
    spring
    struts2
    jsp_servlet
    jsp_servlet2
    数据库
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/4856292.html
Copyright © 2011-2022 走看看