Objective-C基础 便利构造器 单例模式1-17


便利构造器 单例模式

1.在声明时指定setter或getter方法,则用点运算符方法调用时默认调用的就是自己指定的方法

2.单例:唯一性,如:主席,同一时间只能有一个

3.便利构造器和单例模式

4.-减号代表实例方法,即对象方法,只能由对象调用

  +加号代表类方法,由类调用,不依赖于具体对象,相当于由static声明的方法,空间开辟先于其他方法,程序一加载就开辟空间

5.多参函数

-(void)setName:(NSString *)newName

              andAge:(int )newAge

       andAddress:(NSString *)newAddress

          andHobby:(NSString *)newHobby;

方便赋值使用,用这样一个方法同时赋值给多个实例变量。单用这个方法不方便在主函数中输出,主函数输出还是用点方法调用属性比较方便。

6.id代表任意类型指针,好比c中的(void *)

7.-(Student *)initName:(NSString *)newName//:冒号左边是方法名,右边是形参

        andAge:(int)newAge

    andAddress:(NSString *)newAddress

      andHobby:(NSString *)newHobby

{

    self=[super init];//父类初始化,再赋值给self

if(self){   //如果self非空,即父类初始化并给子类赋值成功,进入if判断语句

    self.name=newName;//用self指向调用函数传递过来的对象,self直接指向调用对象,不用再开辟空间

    if (newAge<18) {

        newAge=18;

    }

    self.age=newAge;

    self.address=newAddress;

    self.hobby=newHobby;

   }

    return self;   //如果没有进入if判断体,会返回一个空指针nil

}

self 代表当前调用对象,即哪个对象调用该方法,self就指向哪个对象,方法返回值为Student 类的对象,返回的是指向对象的指针,这里是self,即调用该方法的对象




8.便利构造器(工厂模式)

 +(id)studentWithName:(NSString *)newName//id代表任意类型指针,这里代表Student *,类方法

              andAge:(int)newAge

          andAddress:(NSString *)newAddress

            andHobby:(NSString *)newHobby

{

    Student *stu=[[Student alloc]initName:newName andAge:newAge andAddress:newAddress andHobby:newHobby];

    return [stu autorelease];//自动释放

}



9.单例模式

用static声明对象指针  static  Singleton *st;


(+)加号方法中,判断如果对象指针为nil,则开辟空间并初始化,返回单例对象指针

@implementation Singleton


static  Singleton *st;

+(id)instance{

     @synchronized(self){  //关键字,同步,表示同一时间只能有一个线程在访问

          if(!st){

               st=[[Singleton alloc]init];

           }

        return  st;

     }

}

个人理解:单例就是一种创建对象的方法,定义静态的类对象st,通过类方法来创建对象时,返回的是定义好的那个静态指针,所以内存地址是不变的。再调用此类方法创建对象时,指向的是同一块内存,因为方法中有判断语句,只要指针st非空,就直接返回指针st。


课上代码:

**************************************************************************

//  Student.h

//  oc_class3便利构造器和单例模式

//

//  Created by stjy on 13-1-17.

//  Copyright (c) 2013年 stjy. All rights reserved.

//


#import


@interface Student : NSObject

@property(assign,nonatomic)int age;

@property(retain,nonatomic) NSString *name,*address,*hobby;

+(id)instanceofStudent;

//方法功能是:给学生对象中的属性赋指定的值

//-减号代表实例方法,即方法只能由对象调用,方法名是setName:andAge:andAddress:andHobby:

//方法返回值是void

//方法形参(NSString *)newName 表示学生姓名,(int)newAge 表示学生年龄,(NSString *)newAddress 表示学生住址,(NSString *)newHobby 表示学生爱好。

-(void)setName:(NSString *)newName//:冒号左边是方法名,右边是形参

        andAge:(int)newAge

    andAddress:(NSString *)newAddress

      andHobby:(NSString *)newHobby;

//方法功能:针对学生类的自定义初始化函数,指定初始化内容,返回值为Student *,表示返回一个可用的学生对象

-(Student *)initName:(NSString *)newName//:冒号左边是方法名,右边是形参

        andAge:(int)newAge

    andAddress:(NSString *)newAddress

      andHobby:(NSString *)newHobby;

// 针对学生类的便利构造函数,方便创建学生对象的函数,省略了alloc init,(+)加号表示该方法是类方法,即只能由类名调用,由于相关对象未生成,但是我们想调用相关方法,只能调用类方法。方法名前缀一点是类名,首字 母小写studentWithName:andAge:andAddress:andHobby:。返回值id,表示返回的是任意类型的oc指针对象(很 像c中的void *)

+(id)studentWithName:(NSString *)newName//id代表任意类型指针,这里代表Student *

              andAge:(int)newAge

          andAddress:(NSString *)newAddress

            andHobby:(NSString *)newHobby;

-(void) sayHi;

@end





//

//  Student.m

//  oc_class3便利构造器和单例模式

//

//  Created by stjy on 13-1-17.

//  Copyright (c) 2013年 stjy. All rights reserved.

//


#import "Student.h"


@implementation Student

@synthesize age=_age,name=_name,address=_address,hobby=_hobby;

static Student *instance;

+(id)instanceofStudent{

    @synchronized(self){  //关键字,同步,表示同一时间只能有一个线程在访问。这里括号里的self是指针对谁同步,相当于属性里的atomic

        if (instance==nil) {

            instance=[[Student alloc]init];

        }

        return instance;

    }

}

-(void)setName:(NSString *)newName//:冒号左边是方法名,右边是形参

        andAge:(int)newAge

    andAddress:(NSString *)newAddress

      andHobby:(NSString *)newHobby

{

    _name=newName;

    if (newAge<18) {

        newAge=18;

    }

    _age=newAge;

    _address=newAddress;

    _hobby=newHobby;

}

-(Student *)initName:(NSString *)newName//:冒号左边是方法名,右边是形参

        andAge:(int)newAge

    andAddress:(NSString *)newAddress

      andHobby:(NSString *)newHobby

{

//    Student *stu =[Student alloc];

//    stu.name=newName;

//    if (newAge<18) {

//        newAge=18;

//    }

//    stu.age=newAge;

//    stu.address=newAddress;

//    stu.hobby=newHobby;

//    return stu;

    self=[super init];//通过父类初始化,初始化self继承的相关实例变量和实例方法

    if (self) {    //如果self非空,即父类初始化并给子类赋值成功,进入if判断语句

         self.name=newName;//用self指向调用函数传递过来的对象,self直接指向调用对象,不用再开辟空间

        if (newAge<18) {

            newAge=18;

        }

        self.age=newAge;

        self.address=newAddress;

        self.hobby=newHobby;

    }

    return self; //如果没有进入if判断体,会返回一个空指针nil


}

//便利构造器

+(id)studentWithName:(NSString *)newName//id代表任意类型指针,这里代表Student *,类方法

              andAge:(int)newAge

          andAddress:(NSString *)newAddress

            andHobby:(NSString *)newHobby

{

    Student *stu=[[Student alloc]initName:newName andAge:newAge andAddress:newAddress andHobby:newHobby];

    return [stu autorelease];//自动释放

}

-(void) sayHi{

    NSLog(@"我的名字叫%@,今年%d,来自%@,爱好%@",_name,_age,_address,_hobby);

}

@end


**************************************************************************

#import "Student.h"


#import


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        Student *stu=[[Student alloc]init];

        [stu setName:@"zzh" andAge:20 andAddress:@"保定" andHobby:@"reading"];

        [stu sayHi];

        Student *stu2=[[Student alloc]initName:@"zht" andAge:21 andAddress:@"廊坊" andHobby:@"study"];//调用函数,直接开辟空间并赋值

        [stu2 sayHi];

        Student *stu3=[Student studentWithName:@"mmm" andAge:19 andAddress:@"石家庄" andHobby:@"playing"];

        [stu3 sayHi];

        //单例stu4/stu5/stu6

        Student *stu4=[Student instanceofStudent];//stu4/stu5/stu6指向的是同一块地址,赋值也相对于在给同一个值赋值。这就是单例,永远都只有一个对象

        stu4.name=@"白云";

        stu4.age=20;

        Student *stu5=[Student instanceofStudent];

        Student *stu6=[Student instanceofStudent];

        [stu5 sayHi];

        [stu6 sayHi];

    }

    return 0;

}



**************************************************************************








课件作业

**************************************************************************

父类



//

//  Men.h

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import


@interface Men : NSObject

@property(retain,nonatomic)NSString *name,*sex,*job;

@property(assign,nonatomic)int age;


-(void)sayHi;

+(id) menWithName:(NSString *) aName

           andSex:(NSString *) aSex

           andAge:(int)anAge

           andJob:(NSString *)aJob;


-(id)initWithName:(NSString *) aName

           andSex:(NSString *) aSex

           andAge:(int)anAge

           andJob:(NSString *)aJob;


@end






//

//  Men.m

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Men.h"


@implementation Men

@synthesize name,sex,age,job;



-(id)initWithName:(NSString *) aName

           andSex:(NSString *) aSex

           andAge:(int)anAge

           andJob:(NSString *)aJob

{

    if (self=[super init]) {

        self.name=aName;

        self.sex=aSex;

        self.age=anAge;

        if (self.age>100||self.age<0) {

            self.age=18;

        }

        self.job=aJob;

    }

    return self;


}//初始化方法


+(id) menWithName:(NSString *) aName

           andSex:(NSString *) aSex

           andAge:(int)anAge

           andJob:(NSString *)aJob

{

    Men *man=[[Men alloc]initWithName:aName andSex:aSex andAge:anAge andJob:aJob];

    return [man autorelease];

}





-(void)sayHi

{

    NSLog(@"我叫%@,性别是%@,年龄是%d,工作是%@。",self.name,self.sex,self.age,self.job);

}


@end

************************************************************************

//

//  King.h

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import

#import "Men.h"


@interface King : Men


+(id)instanceOfKing;

@end



//

//  King.m

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "King.h"


@implementation King


static King *instance;

+(id)instanceOfKing

{

    @synchronized(self)

    {

        if (instance==nil) {

            instance=[[King alloc]init];

        }

        return instance;

    }

}//单例模式


@end

**************************************************************************

//

//  Worker.h

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Men.h"


@interface Worker : Men


@end





//

//  Worker.m

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Worker.h"


@implementation Worker


@end

**********************************************************************


//

//  Farmer.h

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Men.h"


@interface Farmer : Men


@end




//

//  Farmer.m

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Farmer.h"


@implementation Farmer


@end

************************************************************************

//

//  Actor.h

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Men.h"


@interface Actor : Men


@end



//

//  Actor.m

//  课件作业

//

//  Created by stjy on 13-1-20.

//  Copyright (c) 2013年 huibao_er. All rights reserved.

//


#import "Actor.h"


@implementation Actor


@end

*************************************************************************