您的位置:首页 > 移动开发 > Objective-C

objective-c中的构造函数(对象初始化)(七)

2012-12-20 09:13 549 查看
以前我们创建对象时都是用new,从java过来的同学也都有这种习惯,其实objective-c中还有一种地道的创建对象的方法:[ [类名 alloc ] init].这种方法才是OC中创建对象的正统,不过效果和new是一样的,但是这种方法更能表示创建对象的实质,那就是分配内存,初始化对象。

alloc是在内存中划分一片空间,这片空间是一片处女地,然后呢,init初始化,我们可以在初始化的时候设置一些基本属性的值,这样就不用在创建对象后再调用方法来赋值。看代码:

human.h:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

  

@interface Human : NSObject  

{  

    int age;  

    NSString *name;  

}  

-(void)setAge:(int)a setName:(NSString *)n;  

-(int)age;  

-(NSString *)name;  

@end  

human.m:

[plain] view
plaincopy

#import "Human.h"  

  

@implementation Human  

-(id)init  

{  

    if(self=[super init])  

    {  

        age=20;  

        name=@"holy";  

    }  

    return self;  

}  

-(void)setAge:(int)a setName:(NSString *)n  

{  

    age=a;  

    [name release];  

      

    name=[n copy];  

}  

-(int)age  

{  

    return age;  

}  

-(NSString *)name  

{  

    return name;  

}  

@end  

main.m:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

#import "Human.h"  

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

{  

    NSAutoreleasePool *pool;  

    pool =[[NSAutoreleasePool alloc] init];  

      

    Human * human = [[Human alloc] init];  

    NSLog(@"名字%@,年龄%d",[human name],[human age]);  

      

    [human setAge:100 setName:@"GOD"];  

    NSLog(@"名字%@,年龄%d",[human name],[human age]);  

  

    [human release];  

    [pool release];//相当于对池中每个对象执行了一次release;  

      

  

}  

上面的代码,在human类中有一个init方法,继承于NSObject类中,不用在human.h文件中声明,也可以不写,如果要设置默认的属性,就可以重写init方法,在里面进行对象的初始化,然后呢生成出来的对象就会有自己的默认属性值了。

其中有一行 if(self=[super init]) 这行代码主要是为了防止父类初始化失败,如果父类初始化失败了,会返回一个nil值,虽然可能性很小

这种初始化方法很好理解,但是有个缺点就是生成对象的默认属性值是固定的,如果想要修改的话需要再调用set方法,那么有没有可以在生成对象的时候自定义属性值的方法呢?既然这样说了,肯定是有的嘛,吼吼,请看代码:
Human.h:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

  

@interface Human : NSObject  

{  

    int age;  

    NSString *name;  

}  

-(id)initWithAge:(int)a Name:(NSString *)n;  

-(int)age;  

-(NSString *)name;  

@end  

Human.m:

[plain] view
plaincopy

#import "Human.h"  

  

@implementation Human  

-(id)init  

{  

    if(self=[super init])  

    {  

        age=20;  

        name=@"holy";  

    }  

    return self;  

}  

-(id)initWithAge:(int)a Name:(NSString *)n  

{  

    if (self=[super init]) {  

        age=a;  

        [name release];  

          

        name=[n copy];  

    }  

    return self;  

     

}  

-(int)age  

{  

    return age;  

}  

-(NSString *)name  

{  

    return name;  

}  

@end  

main.m:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

#import "Human.h"  

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

{  

    NSAutoreleasePool *pool;  

    pool =[[NSAutoreleasePool alloc] init];  

      

    Human * human1 = [[Human alloc] init];  

    NSLog(@"名字%@,年龄%d",[human1 name],[human1 age]);  

      

    Human * human2 = [[Human alloc] initWithAge:100 Name:@"GOD"];  

    NSLog(@"名字%@,年龄%d",[human2 name],[human2 age]);  

      

      

    [human1 release];  

    [human2 release];  

    [pool release];//相当于对池中每个对象执行了一次release;  

      

  

}  

输出:

[plain] view
plaincopy

2012-03-14 21:03:24.974 String[460:403] 名字holy,年龄20  

2012-03-14 21:03:24.976 String[460:403] 名字GOD,年龄100  

[plain] view
plaincopy

<span style="font-size: 18px; ">关键字:objective-c ,objective c , oc ,构造函数 ,对象初始化 </span>  

[plain] view
plaincopy

  

[plain] view
plaincopy

<pre name="code" class="plain" style="background-color: rgb(255, 255, 255); "><pre name="code" class="plain"><pre></pre>  

<pre></pre>  

<pre name="code" class="plain" style="background-color: rgb(255, 255, 255); "><pre name="code" class="plain" style="background-color: rgb(255, 255, 255); "><pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

<pre></pre>  

  

</pre></pre></pre></pre>  

objective-c中的@property,@synthesize等简易用法(八)

在objective-c中,我们可以用new简单的代替alloc init,我们今天介绍的是类似于new这种简易用法的另一种OC特性,用@property,@synthesize来代替get,set方法,用起来很简单,可以省掉很多的代码量,当需要用SET,GET方法的地方,我们可以用@property,@synthesize来简单的代替,这时系统会自动给我们生成该变量的set,get方法,@property对应方法的声明部分,@synthesize对应方法的实现部分。

Human.h:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

  

@interface Human : NSObject  

{  

    int age;  

    float height;  

}  

//这里用@property表示,代表了age和height的set,get方法的声明  

@property int age;  

@property float height;  

  

@end  

Human.m:

[plain] view
plaincopy

#import "Human.h"  

  

@implementation Human  

//这里用@synthesize表示age,height变量的set,get方法的实现。  

@synthesize age;  

@synthesize height;  

  

@end  

main.m:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

#import "Human.h"  

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

{  

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  

      

    Human *human = [[Human alloc]init];  

    //“.”在等号左边,相当于set方法。  

      

    human.age=20; //等价于[human setAge:20]  

    human.height=60.50;  

      

    //“.”在等号右边,相当于get方法。  

    float tmp=human.height;//等价于 float tmp = [human height]  

    NSLog(@"年龄%d,身高%f",human.age,tmp);  

      

    [human release];  

    [pool release];//相当于对池中每个对象执行了一次release;  

      

  

}  

输出语句:

2012-03-15 10:11:34.052 String[325:403] 年龄20,身高60.500000

这时系统会自动生成age,height的set,get方法,可以直接使用,那还有一种情况,如果我只想让age有get方法,没有set方法怎么办,也就是说在初始化里给一个默认值,而这个值只能被调用,不能被修改,用@property这种方式可以吗?答案是肯定的,因为@property给我们提供了类似参数的一种可选项,关键词是readonly,readwrite,分别对应着只能读不能修改和读写均可,一般我们不设置readwrite,因为默认就是读写状态。看代码:

Human.h:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

  

@interface Human : NSObject  

{  

    int age;  

    float height;  

}  

//这里用@property表示,代表了age和height的set,get方法的声明  

@property (readonly)int age;  

@property float height;  

  

@end  

Human.m:

[plain] view
plaincopy

#import "Human.h"  

  

@implementation Human  

//重写init方法给age赋初值  

-(id)init  

{  

    if(self=[super init])  

    {  

        age=20;  

    }  

    return self;  

}  

//这里用@synthesize表示age,height变量的set,get方法的实现。  

@synthesize age;  

@synthesize height;  

  

@end  

main.m:

[plain] view
plaincopy

#import <Foundation/Foundation.h>  

#import "Human.h"  

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

{  

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  

      

    Human *human = [[Human alloc]init];  

    human.height=60.5;  

    //human.age=30; 该行如果不注释是会报错的,即age只有get方法,没有set方法  

    NSLog(@"年龄age=%d",human.age);  

      

     

    [human release];  

    [pool release];//相当于对池中每个对象执行了一次release;  

      

  

}  

输出:

2012-03-15 10:32:11.497 String[360:403] 年龄age=20

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7354489
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐