zoukankan      html  css  js  c++  java
  • oc21--super

    //
    //  Phone.h
    
    #import <Foundation/Foundation.h>
    typedef enum
    {
        kFlahlightStatusOpen,
        kFlahlightStatusClose
    } FlahlightStatus;
    
    // 被继承的这个类我们称之为父类/ 超类
    @interface Phone : NSObject
    
    + (void)carameWithFlahlightStatus:(FlahlightStatus)status;
    - (void)carameWithFlahlightStatus:(FlahlightStatus)status;
    + (void)openFlahlight;
    + (void)closeFlahlight;
    @end
    //
    //  Phone.m
    
    #import "Phone.h"
    
    @implementation Phone
    
    //可以有同名的类方法和对象方法
    - (void)carameWithFlahlightStatus:(FlahlightStatus)status;
    {
        NSLog(@"- carameWithFlahlightStatus");
    }
    
    + (void)carameWithFlahlightStatus:(FlahlightStatus)status
    {
        if (status == kFlahlightStatusOpen) {
            NSLog(@"%@",self);//Iphone,即使这个方法是子类通过super方法调过来的,self也是子类对象Iphone.
            [self openFlahlight];
        }else
        {
            NSLog(@"%@",self);
            [self closeFlahlight];
        }
        NSLog(@"拍照");
    }
    
    + (void)openFlahlight
    {
        NSLog(@"打开闪光灯");
    }
    
    + (void)closeFlahlight
    {
        NSLog(@"关闭闪光灯");
    }
    
    @end
    //
    //  Iphone.h
    
    #import <Foundation/Foundation.h>
    #import "Phone.h"
    
    @interface Iphone : Phone
    
    + (void)carameWithFlahlightStatus:(FlahlightStatus)status;
    - (void)test;
    @end
    //
    //  Iphone.m
    
    #import "Iphone.h"
    
    @implementation Iphone
    
    + (void)carameWithFlahlightStatus:(FlahlightStatus)status
    {
        // 由于以下代码和父类中的一模一样, 所以只需调用父类写好的代码即可
        /*if (status == kFlahlightStatusOpen) {
            NSLog(@"%@",self);
            [self openFlahlight];
        }else
        {
            NSLog(@"%@",self);
            [self closeFlahlight];
        }
        NSLog(@"拍照");*/
        
    //    [self carameWithFlahlightStatus:status];
        // 只需要利用super给父类的方法发送一个消息, 那么系统就会自动调用父类的方法
        // 如果以后想在子类中调用父类的方法可以使用super
        // 如果想在给父类方法进行扩展的同时保留父类的方法, 那么可以使用super调用父类同名的方法
        [super carameWithFlahlightStatus:status];
    }
    
    - (void)test
    {
        /*
         super跟self一样,super在类方法中就是代表了这个类,会调用父类的类方法。在对象方法中, 代表了这个对象,会调用父类的对象方法,
         可以利用super在任意方法中调用父类中的方法,super一般用在重写父类的方法并且想保留父类的功能。
         */
        [super carameWithFlahlightStatus:kFlahlightStatusOpen];
    }
    @end
    //
    //  main.m
    
    #import <Foundation/Foundation.h>
    #import "Iphone.h"
    
    
    int main(int argc, const char * argv[]) {
    
        Iphone *p = [Iphone new];
        [p test];
        [Iphone carameWithFlahlightStatus:kFlahlightStatusOpen];
        
        return 0;
    }
  • 相关阅读:
    组合数取模 Lucas定理
    关于上下界的二分查找
    POJ 1091 跳蚤
    Eular 函数模板
    POJ 数学(3)
    SRM 567 div2
    SRM 570 div2
    最大最小搜索,alpha beta 剪枝
    JavaScript:prototype属性使用方法
    ArcGIS网络分析之Silverlight客户端服务区分析(五)
  • 原文地址:https://www.cnblogs.com/yaowen/p/7413392.html
Copyright © 2011-2022 走看看