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

objective-c 类和对象中静态数据成员和静态成员函数(类方法)详解

2012-05-03 17:19 405 查看
作为初学者,相信大家都对objective-c的静态数据成员和静态成员函数都挺纠结的,我现在和大家分享下我对这个知识点的理解,请大家多多指点....

如果大家学过C++,那么都应该知道和了解静态数据成员和静态成员函数是怎么声明和使用的;现在就让我来说说objective-c和C++在静态成员在使用和声明上的异同点吧。

        首先来说说声明:

A:静态数据成员的声明

C++是如下声明的:

class  Human{
public:
int volume();
private:
static  int  height;
}


objective-c 的声明和C++的声明有很大不一样。你不能在@interface里声明,只能在它的外面声明,但是,如果你在.h里面声明的话,你将得到两个静态数据成员,一个是类的静态数据成员,一个是全局的静态数据成员。相信大家对这个都不太理解,好的,那我先上代码:

先上类Human.h的代码:

//
//  Human.h
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
static int ludehai;
@interface Human : NSObject
{
//int age;
//int ludehai;
float height;
int ear;
float eyes;
NSString *leftHand;
NSString *rightHand;
}
/*这里用@property表示,代表了age和height的set,get方法的声明
这相当于
@property(readwrite) int age;
@property(readwrite) float height;
*/
@property int age;/*这里相当于作如下声明:
- (int)age;
-(void)setAge:(int)newAge;  */
@property float height;//同上

//这里用@property表示,代表了ear的get方法的声明
@property(readonly) int ear;/*这里相当于作如下声明:
- (int)ear;
*/

//assign: 默认类型,setter方法直接赋值,而不进行retain操作
@property(assign) float eyes;

//
@property(copy) NSString *leftHand;

@property(retain) NSString *rightHand;
+(void) test;
+(void)setLudehai:(int)c;
+(int)getLudehai;

@end


在上Human.ml的代码

//
//  Human.m
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Human.h"

@implementation Human

//这里用@synthesize表示age,height变量的set,get方法的实现。
@synthesize age;
/*相当于这样实现方法
- (int)age
{
return age;
}
-(void)setAge:(int)newAge
{
age = newAge;
}*/
/*如何这样写,那么age返回值就会是输入的两倍
-(int)age{
return age * 2;
}*/
@synthesize height;//同上

//这里@synthesize表示ear变量的get方法的实现
@synthesize ear;/* 相当于这样实现代码
- (int)ear
*/

@synthesize eyes;

@synthesize leftHand;

@synthesize rightHand;

//重写init方法给age赋初值
-(id)init
{
if(self=[super init])
{
ear=20;
}
return self;
}

+(void) test{
NSLog(@"test+");
}
+(void)setLudehai:(int)c{
ludehai = c;
}
+(int)getLudehai{
return ludehai;
}
@end


最后,当然,不能少了main.m的代码:

//
//  main.m
//  @property@synthesize
//
//  Created by b126 on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Human.h"

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

@autoreleasepool {

// insert code here...
[Human test];
ludehai = 50;
[Human setLudehai:100];
NSLog(@"%i",[Human getLudehai]);
NSLog(@"%i",ludehai);
}
return 0;
}


好的,最后的输出结果是:

2012-05-03 17:03:10.850 @property@synthesize[1186:403] test+
2012-05-03 17:03:10.853 @property@synthesize[1186:403] 100
2012-05-03 17:03:10.853 @property@synthesize[1186:403] 50

相信大家看到输出结果的时候肯定想自己去试试。

其实,当我看到输出结果的时候也挺惊讶,我的理解如下:在Human.h里声明:

static int ludehai;


当你在main.m里将Human.h包含进来的时候,objective-c就给ludehai这个变量分配了两个内存空间,一个是类Human的,一个全局的Human,这样我们可以给他们赋予两个不同的值。

好了,那我们该在那里声明类的静态数据成员呢?

经过我的测试,我觉得我们应该把类的静态数据成员声明在Human.m里面:

#import "Human.h"

@implementation Human
static int ludehai;
这样,objective-c就给ludehai这个变量分配一个空间了,在main.m里不能直接调用到了,就只能通过静态成员函数调用和赋值。

B:静态成员函数

               C++的静态成员函数是这样声明的:

     static int volume();

objective-c的静态成员函数也叫类方法,是这样声明的:

+(void) test;
+(void)setLudehai:(int)c;
+(int)getLudehai;
对,在objective-c里,“+”声明的函数是静态成员函数(类方法)。“-”声明的叫实例方法。他们两者的区别是:“+”的要通过类名调用,“-”必须是对象调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐