现在的位置: 首页 > 综合 > 正文

『iOS学习笔记』 – 变量 属性 方法 实现

2012年09月01日 ⁄ 综合 ⁄ 共 1096字 ⁄ 字号 评论关闭

 

『iOS学习笔记』 - 变量 属性 方法 实现

 

1、代码说明:

Person.h

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
{   
    int age,sex;//变量的定义
    int height,width;
}

@property int age,sex;//属性的定义
@property char height;

//-(void) setAge;
-(int) setAge1 :(int)a;
-(int) setWH :(int)w :(int)h; 
/* 方法的定义
 格式  
-(返回的数据类型) 方法名称 :(参数1的数据类型)参数1名称   :(参数2的数据类型)参数2名称
*/

@end

Person.m

Person.m

#import "Person.h"

@implementation Person

@synthesize age,sex;//访问器

//@synthesize height;
/*
 【我的注解】 
  @synthesize 引用 @property 关联 @interface
  引用不到,或者关联不到,均会抛错。 
*/


#pragma mark ------setAge----

//-(void) setAge;
//{
//    age=20;
//}

#pragma mark ------setAge1------
-(int) setAge1 :(int)a
{
    age=a;
    return age;
}

#pragma mark ------setWH------
-(int) setWH :(int)w :(int)h  //方法的实现
{
    width = 100;
    height=175;
    return age*height;
}

@end

 

main.m

main.m

#import <Foundation/Foundation.h>

#import "Person.h"

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

    @autoreleasepool {
        
        Person *person=[Person alloc];
        [person init];
        
        person.age=1;//属性
                
        NSLog(@"person.ag = %i",person.age);//输出属性,注意类型匹配,否则抛错
        NSLog(@"person = %@",person);//输出对象
        
        [person setWH:6 :10];//方法
        
        [person release];//如果使用了ARC机制,release就不能用了。
        
        
    }
    return 0;
}

 

 

2、我的注解(详见下面三张图):

@synthesize 引用 @property 关联 @interface

引用不到,或者关联不到,均会抛错。

抱歉!评论已关闭.