1. 程式人生 > >iOS類的合理設計,面向對象思想

iOS類的合理設計,面向對象思想

open pop 打開閃光燈 hit interface 調用 else 沒有 ash

每天更新的東西可能有反復的內容。當時每一部分的知識點是不同的,須要大家認真閱讀

這裏介紹了iOS類的合理設計。面向對象思想


main.m

#import <Foundation/Foundation.h>
#import "Iphone.h"
int main(int argc, const char * argv[])
{
    Iphone * phone = [Iphone new];
    phone->_color = IphoneColorWhite;
    phone->_size = IphoneSize3point5;
    
    //phone = 0ffxxx
    //[0ffxxx cameraWithFlashLightStatus];
    [phone cameraWithFlashLightStatus:IphoneFlashLightStatusOpen];
    
    return 0;
}



iphone.h

@interface Iphone : NSObject
{
    @public
    /** 用來存儲iPhone屏幕尺寸 */
    //enum IphoneSize 與IphoneSize 等價
    IphoneSize _size;//用來存儲iPhone屏幕尺寸
    /** 用來存儲iPhone顏色 */
    IphoneColor _color;//用來存儲iPhone顏色
    
    /** 用來存儲cpu大小 */
    float _cpu;
    /** 用來存儲內部容量大小 */
    float _ram;
}

//設計方法技巧,如果方法沒有返回值,不要糾結是否有返回值,不要讓瑣碎的事兒幹擾思路
/**打開閃光燈*/
-(void)openFlashLight;
/**關閉閃光燈*/
-(void)closeFlashLight;
/**自己主動*/
-(void)flaseLightAuto;
/**拍照*/
-(void) cameraWithFlashLightStatus:(IphoneFlashLightStatus)flaseLightStatus;

@end



iphone.m

#import "Iphone.h"

@implementation Iphone
/**打開閃光燈*/
- (void)openFlashLight
{
    NSLog(@"打開閃光燈");
}
/**關閉閃光燈*/
- (void)closeFlashLight
{
    NSLog(@"關閉閃光燈");
}
/**自己主動*/
-(void)flaseLightAuto
{
    NSLog(@"自己主動模式");
}
/**拍照*/
- (void)cameraWithFlashLightStatus:(IphoneFlashLightStatus)flaseLightStatus
{
    //類的內部怎樣獲得一個對象的地址
    //self keyword
    //誰調用 self就代表誰
    if(flaseLightStatus == IphoneFlashLightStatusOpen)
    {
        //打開閃光燈
        [self openFlashLight];
    }
    else if(flaseLightStatus == IphoneFlashLightStatusClose)
    {
        [self closeFlashLight];
        //關閉閃光燈
    }
    else
    {
        [self flaseLightAuto];
        //自己主動模式
    }
    
    NSLog(@"拍照了。笑一個");
}
@end



iOS類的合理設計,面向對象思想