//
// 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
// 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