1. 程式人生 > >OC-初始化、例項方法、類方法、工廠方法

OC-初始化、例項方法、類方法、工廠方法

1.初始化

1.1 無參初始化
    1.1.1init是從父類NSObject中繼承的,所以不需要在.h檔案中宣告
SHInteger.h
#import <Foundation/Foundation.h>

@interface SHInteger : NSObject//NSObject是父類
@property int integer;
-(void)show;
@end
SHInteger.m
#import "SHInteger.h"

@implementation SHInteger
-(instancetype)init//無參初始化
{
    if (self
= [super init]) { self.integer = 50; } return self; } -(void)show { NSLog(@"%d", self.integer); } @end

main:

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        SHInteger *i1 = [[SHInteger alloc] init];
        [i1 show];
    }
    return
0; }
1.2 帶參初始化
    1.2.1方法名的格式為:-(id)initWith...
1.3 instancetype:例項(物件)的資料型別,只用在init的返回值
1.4 id:
    1.4.1萬能指標(資料型別),類似於void*
    1.4.2與void*的區別
        id定義指標時不需要加*
        id型別的指標不能指向基本資料型別(包括結構體)的變數,只能指向OC類的物件
id a;//a是一個指標       
int b = 10;
//a = &b;//id型別的指標a不能指向整型變數        
a = @"This is a string."
;//id型別的指標a只能指向OC類的物件
#import <Foundation/Foundation.h>

@interface SHInteger : NSObject
@property int integer;
-(id)initWithInteger:(int)integer;
-(void)show;
@end
#import "SHInteger.h"

@implementation SHInteger
-(instancetype)init
{
    if (self = [super init])
    {
        self.integer = 100;
    }
    return self;
}
-(id)initWithInteger:(int)integer
{
    if (self = [super init])
    {
        self.integer = integer;
    }
    return self;
}
-(void)show
{
    NSLog(@"%d", self.integer);
}
@end
1.5 self
    1.5.1 self是一個指標
    1.5.2 只能用在類中方法的函式體中
    1.5.3 指向呼叫該方法的物件
    1.5.4 可以在類中的一個方法函式體中呼叫類中其他方法,包括私有方法

1.6 super
    1.6.1 super也是一個指標
    1.6.2 該指標指向該所在類的父類資料

2.例項方法

2.1 以減號開頭的方法
2.2 用物件來呼叫
2.3 在例項方法中self指向呼叫該方法的物件

3.類方法

3.1 以加號開頭的方法
3.2 用類名來呼叫
3.3 在類方法中不能用self
#import <Foundation/Foundation.h>

@interface SHClassMethod : NSObject
-(void)show;
+(void)classMethod;
@end
#import "SHClassMethod.h"

@implementation SHClassMethod
-(void)show
{
    NSLog(@"這是一個例項方法");
}
+(void)classMethod
{
    NSLog(@"這是一個類方法");
}
@end
SHClassMethod *cm = [[SHClassMethod alloc] init];
[cm show];//例項方法

//[cm classMethod];//類方法不能用物件呼叫
[SHClassMethod classMethod];//類方法使用類名呼叫的

4.工廠方法

4.1 工廠方法是類方法的一種應用
4.2 工廠方法用於生成物件
4.3 無參工廠方法
4.4 帶參工廠方法
#import <Foundation/Foundation.h>

@interface SHPoint : NSObject
@property int x;
@property int y;
-(void)show;
+(id)point;//工廠方法的方法名就是類名,但不要字首
@end
#import "SHPoint.h"

@implementation SHPoint
-(instancetype)init
{
    if (self = [super init])
    {
        self.x = 10;
        self.y = 20;
    }
    return self;
}
-(void)show
{
    NSLog(@"(%d,%d)", self.x, self.y);
}
+(id)point
{
    SHPoint *p = [[SHPoint alloc] init];
    return p;
}
@end
帶參工廠
.h:
+(id)integerWithInteger:(int)integer;


.m:
-(id)initWithInteger:(int)integer
{
    if (self = [super init])
    {
        self.integer = integer;
    }
    return self;
}

+(id)integerWithInteger:(int)integer
{
    SHInteger *i = [[SHInteger alloc] initWithInteger:integer];
    return i;
}

5.單例模式

5.1 是一種特殊的工廠方法
5.2 特殊在只能生成一個物件
#import <Foundation/Foundation.h>

@interface SHSingleton : NSObject
+(id)sharedSingleton;
@end
#import "SHSingleton.h"

@implementation SHSingleton
+(id)sharedSingleton
{
    static SHSingleton *single = nil;
    if (single == nil)
    {
        single = [[SHSingleton alloc] init];
    }
    return single;
}
@end
#import <Foundation/Foundation.h>
#import "SHSingleton.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        SHSingleton *s1 = [SHSingleton sharedSingleton];
        NSLog(@"%p", s1);
        SHSingleton *s2 = [SHSingleton sharedSingleton];
        NSLog(@"%p", s2);
    }
    return 0;
}

執行結果:
2016-08-25 20:02:34.031 day15_14[2196:203901] 0x100200330
2016-08-25 20:02:34.032 day15_14[2196:203901] 0x100200330

思考練習

1、模擬登陸程式,實現使用者類
屬性:賬號、密碼(int)
方法:初始化(帶參、不帶參)、工廠方法(帶參、不帶參)、show、判斷賬號密碼是否正確
2、太陽類
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析

#import <Foundation/Foundation.h>

@interface SHUser : NSObject
@property int account;
@property int password;
-(id)initWithAccount:(int)account andPassword:(int)password;
+(id)user;
+(id)userWithAccount:(int)account andPassword:(int)passowrd;
-(void)show;
-(BOOL)isEuqalToUser:(int)account withPassowrd:(int)password;
@end
#import "SHUser.h"

@implementation SHUser
-(instancetype)init
{
    if (self = [super init])
    {
        self.account = 1000;
        self.password = 1234;
    }
    return self;
}
-(id)initWithAccount:(int)account andPassword:(int)password
{
    if (self = [super init])
    {
        self.account = account;
        self.password = password;
    }
    return self;
}
+(id)user
{
    SHUser *u = [[SHUser alloc] init];
    return u;
}
+(id)userWithAccount:(int)account andPassword:(int)passowrd
{
    SHUser *u = [[SHUser alloc] initWithAccount:account andPassword:passowrd];
    return u;
}
-(void)show
{
    NSLog(@"賬號:%d,密碼:%d", self.account, self.password);
}
-(BOOL)isEuqalToUser:(int)account withPassowrd:(int)password
{
    return self.account == account && self.password ==password;
}
@end

太陽類:

#import <Foundation/Foundation.h>

@interface SHSun : NSObject
+(id)sharedSun;
@end
#import "SHSun.h"

@implementation SHSun
+(id)sharedSun
{
    static SHSun *single = nil;
    if (single == nil)
    {
        single = [[SHSun alloc] init];
    }
    return single;
}
@end

main函式:

#import <Foundation/Foundation.h>
#import "SHUser.h"
#import "SHSun.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int account;
        NSLog(@"請輸入一個註冊賬號:");
        scanf("%d", &account);
        int password;
        NSLog(@"請輸入一個註冊密碼:");
        scanf("%d", &password);

        SHUser *user1 = [SHUser userWithAccount:account andPassword:password];

        NSLog(@"請輸入登入賬號:");
        scanf("%d", &account);
        NSLog(@"請輸入登入密碼:");
        scanf("%d", &password);

        if ([user1 isEuqalToUser:account withPassowrd:password] == YES)
        {
            NSLog(@"登入成功");
        }
        else
        {
            NSLog(@"登入賬號或密碼錯誤");
        }


        SHSun *sun1 = [SHSun sharedSun];
        SHSun *sun2 = [SHSun sharedSun];
        NSLog(@"%p,%p", sun1, sun2);
    }
    return 0;
}