OC-初始化、实例方法、类方法、工厂方法

来源:互联网 发布:美国情景喜剧排行知乎 编辑:程序博客网 时间:2024/06/05 02:10

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;}
0 0
原创粉丝点击