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
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
相关文章推荐
- objective-c中的构造函数(对象初始化)(七)
- objective-c中的构造函数(对象初始化)(七)
- 课程设计__类和对象的简单使用(构造函数初始化)
- 多重继承、虚基类继承时,没有显式调用基类构造函数,对象是如何初始化的问题
- 【IOS 开发学习总结-OC-15】objective-c面向对象之——对象初始化
- objective-c对象初始化
- 设计三角形类,通过增加构造函数,是对象在定义是能够进行初始化
- 《第五周实验报告1-3》---增加构造函数,使对象在定义时能够进行初始化
- 派生类的构造函数及其对象的初始化
- 【iOS开发之Objective-C】初始化对象
- iOS学习(六)Objective-C 对象的初始化
- 设计三角形类,通过增加构造函数,使对象在定义时能够进行初始化2
- 面向对象第二个特征----继承III(子父类中构造函数初始化过程2)
- Objective-C 学习笔记 - part 3 - 为对象分配内存并初始化
- Objective-C 2.0 with Cocoa Foundation---对象的初始化以及实例变量的作用域(2)
- Objective-C 编程语言(3) 定义类---分配并初始化对象,init方法的返回值,初始化方法的实现
- Objective-C 2.0 with Cocoa Foundation--- 7,对象的初始化以及实例变量的作用域
- OC中对象的初始化(便利初始化函数以及便利构造函数)
- Objective-C 2.0 with Cocoa Foundation--- 7,对象的初始化以及实例变量的作用域
- Objective-C对象的申请空间与初始化