Objective-C:初始化、实例方法、类方法、工厂方法、单例模式

初始化

  1. 无参初始化
    1.1 init是从父类NSObject中继承的,所以不需要在.h文件中声明
  2. 带参初始化
    2.1 方法名的格式为:-(id)initWith…
    2.2 instancetype:对象的数据类型
    2.3 id
    (2.3.1)万能指针(数据类型),类似于void*
    (2.3.2)与void*的区别
    (2.3.2.1) id定义指针时不需要加*
    (2.2.2.2) id类型的指针不能指向基本数据类型的变量(包括结构体),只能指向OC类的对象.
    2.4 self
    (2.4.1)self是一个指针
    (2.4.2)只能用在类中方法的函数体中
    (2.4.3)指向调用该方法的对象
    (2.4.4)可以在类中的衣服方法函数体中调用类中的其他方法,包括私有方法

  3. super
    3.1 super也是一个指针
    3.2 该指针指向所在类的父类

无参初始化

.h文件

@interface CZInteger : NSObject    //NSObject 是父类
@property int integer;
-(void)show;
@end

.m文件

@implementation CZInteger
-(instancetype)init           //父类NSObject继承来的方法,不用声明
{
    if (self=[super init])
    {
        self.integer=10;
       //self.**=**
    }
    return self;
}
-(void)show
{
    NSLog(@"%d",self.integer);
}
@end

main函数

#import <Foundation/Foundation.h>
#import "CZInteger.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool
    {
        CZInteger *i1=[[CZInteger alloc]init];
        [i1 show];

    }
    return 0;
}

self

.h文件

//  self是一个指针
//  只能用在类中方法的函数体中
//  指向调用该方法的对象
//  可以在类中的衣服方法函数体中调用类中的其他方法,包括私有方法

@interface CZPoint : NSObject
@property int x;
@property int y;
-(id)initWithPoint:(int)x andY:(int)y;
-(void)setWinthX:(int)x andY:(int)y;
-(void)show;
@end

.m文件

@implementation CZPoint
-(id)initWithPoint:(int)x andY:(int)y
{
    if(self = [super init])
    {
// self.x=x;
// self.y=y;
        [self setWinthX:x andY:y];
    }
    return self;
}
-(void)show
{
    NSLog(@"x=%d,y=%d",self.x,self.y);
}
-(void)setWinthX:(int)x andY:(int)y
{
    self.x=x;
    self.y=y;
}
@end

实例方法 类方法 工厂方法

  1. 实例方法 : 以减号开头的方法
    1.1. 实例方法用对象调用 (没有对象不能调用)
    1.2. 在实例方法中self指向调用该方法的对象.

  2. 类方法: 是以加号开头的方法
    2.1 类方法用类名调用 (特点是没有对象的使用也可以调用)
    2.2 一般不能在类方法中使用self. (有特例)

  3. 工厂方法
    3.1 工厂方法是累方法的一种应用
    3.2 工厂方法用于生成对象

实例方法,类方法

.h文件:

@interface CZClassMethod : NSObject
-(void)show;
+(void)classMethod;
@end

.m文件

@implementation CZClassMethod
-(void)show
{
    NSLog(@"这是一个实例方法");
}
+(void)classMethod
{
    NSLog(@"这是一个类方法");
}
@end

main函数

int main()
{
    @autoreleasepool {
        CZClassMethod *cm=[[CZClassMethod alloc]init];
        [cm show];
        //[cm classMethod]; 类方法不能用对象调用
        [CZClassMethod classMethod]; //类方法用类名调用


    }
    return 0;
}

工厂方法

.h文件

@interface CZPoint : NSObject
@property int x;
@property int y;
-(void)show;
+(id)point; //工厂方法的方法名就是类名,但不要前缀
@end

.m文件

@implementation CZPoint
-(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
{
    CZPoint *p =[[CZPoint alloc]init];
    return p;
}
@end

main函数

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CZPoint* p1=[[CZPoint alloc]init];
        [p1 show];
        CZPoint *p2=[CZPoint point]; //用工厂方法初始化
        [p2 show];
    }
    return 0;
}

单例模式

  1. 单例模式是一种特殊的工厂方法
  2. 只能生成一个对象
    .h文件
@interface CZSingleton : NSObject
+(id)sharedSingleton;
@end

.m文件

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

main函数

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

输出结果

2016-08-25 08:18:21.157 day15_14[549:11277] 0x1005001e0
2016-08-25 08:18:21.157 day15_14[549:11277] 0x1005001e0
Program ended with exit code: 0
//可以看到单例模式生成的对象是同一个地址

单例模式分析图
这里写图片述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值