小白学开发(iOS)OC_self 与 类方法 和 对象方法(2015-07-25)

 //
//  main.m
//  self与类方法和对象方法
//
//  Created by admin on 15/7/25.
//  Copyright (c) 2015年 admin. All rights reserved.
//

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

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

    
//  使用类名调用类方法
    [Iphone cameraWithFlashlighStatus:kFlashlightStatusOpean];
    
//  这里将new出一个对象,通过对象调用方法
    Iphone *ip = [Iphone new];
    [ip cameraWithFlashlighStatus:kFlashlightStatusOpean];
    
    return 0;
}

//  注意:
/*      > 无论是在对象方法还是类方法中,不能使用self来调用和当前方法同名的方法,
          否则会一直循环调用,造成死循环。
        > 在对象方法中,可以使用self->成员变量名 = 来给成员变量赋值
          这里用self直接指向对象中的成员变量,消除了 和函数内部定义同名的局部变量
          访问出现错误,只能访问函数中局部变量的情况
*/
//
//  Iphone.h
//  self与类方法和对象方法
//
//  Created by admin on 15/7/25.
//  Copyright (c) 2015年 admin. All rights reserved.
//

#import <Foundation/Foundation.h>

//  闪光灯状态
typedef enum
{
    kFlashlightStatusOpean,
    kFlashlightStatusClose
}FlashlightStatus;

@interface Iphone : NSObject
{
    int _cpu;
}

- (void)setCpu:(int)cpu;
- (int)cpu;

//  拍照方法    注意:这里都是类方法
+ (void)cameraWithFlashlighStatus:(FlashlightStatus)status;
//  打开闪光灯
+ (void)opeanFlashlight;
//  关闭闪光灯
+ (void)closeFlashlight;

//  拍照方法    注意:这里都是对象方法
- (void)cameraWithFlashlighStatus:(FlashlightStatus)status;
//  打开闪光灯
- (void)opeanFlashlight;
//  关闭闪光灯
- (void)closeFlashlight;

@end
//
//  Iphone.m
//  self与类方法和对象方法
//
//  Created by admin on 15/7/25.
//  Copyright (c) 2015年 admin. All rights reserved.
//

#import "Iphone.h"

@implementation Iphone
- (void)setCpu:(int)cpu
{
    _cpu = cpu;
}
- (int)cpu
{
    return _cpu;
}
/*
    类方法的实现
 */
+ (void)cameraWithFlashlighStatus:(FlashlightStatus)status
{
    if (kFlashlightStatusOpean == status) {
        
//  这里是打开闪光灯
//      [Iphone opeanFlashlight];
/*
      使用self来替换 Iphone
      这个self表示的意思就是,谁调用这个方法就代表谁
      这里主函数中使用类名Iphone调用
      + (void)cameraWithFlashlighStatus:(FlashlightStatus)status方法,
      所以,这里self就是Iphone, 即:self ==Iphone
 */
        [self opeanFlashlight];
    }else
    {
        //      关闭闪光灯
        [self closeFlashlight];
    }
    NSLog(@"拍照");
}

+ (void)opeanFlashlight
{
    NSLog(@"打开闪光灯");
}

+ (void)closeFlashlight
{
    NSLog(@"关闭闪光灯");
}

/*
    对象方法的实现
 */
- (void)cameraWithFlashlighStatus:(FlashlightStatus)status
{
    if (kFlashlightStatusOpean == status) {
        
//  这里是打开闪光灯
//      [Iphone opeanFlashlight];
/*
     使用self来替换 Iphone       
     这个self表示的意思就是,谁调用这个方法就代表谁
     这里主函数中使用 对象ip 调用
     + (void)cameraWithFlashlighStatus:(FlashlightStatus)status方法,
     所以,这里self就是ip这个对象, 即:ip == Iphone
 */
        [self opeanFlashlight];
    }else
    {
//      关闭闪光灯
        [self closeFlashlight];
    }
    NSLog(@"拍照");
}

- (void)opeanFlashlight
{
    NSLog(@"打开闪光灯");
}

- (void)closeFlashlight
{
    NSLog(@"关闭闪光灯");
}
@end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值