Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50674
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: iOS平台

2015-03-19 23:15:42

1.-(void)setAge:(int)a setNo:(int)b;  他的方法名字叫setAge:setNo:

2.设置断点:点击代码左侧会生成断点

3.成员变量一般命名时候前面加 “_
4.下面一个例子是使用点语法的一个例子

bbb.h:

#import


@interface bbb : NSObject{

    

    int age;


    

}


-(int)age;


-(void)setAge:(int)a;


@end


bbb.m:

#import "bbb.h"


@implementation bbb


-(int)age{


    

    NSLog(@"get方法");

    return age;


}


-(void)setAge:(int)a{


    NSLog(@"set方法");

    age = a;


}


@end


main.m:

#import


#import "bbb.h"


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

    @autoreleasepool {

        

        bbb *b1 = [[bbb alloc] init];

        

        //点语法

        b1.age = 10;//实际等效于 [b1 setAge:10]    是编译器自动转换的     如果没有setAge方法,则会报错。

        

        int i = b1.age;//实际等效于 int i = [b1 get];  如果是被赋值就调用set方法,其他的就调用get方法

        

    }

    return 0;

}

5.%@ 打印对象

6.下面是自己写构造方法的一个例子

ccc.h:


#import 


@interface ccc : NSObject{


    int _age;

    int _no;


}


-(void)setAge:(int)age setNo:(int)no;

-(int)age;

-(int)no;

//自己写一个构造方法

-(id)initWithAge:(int)age andNo:(int)no;




@end


ccc.m:

#import "ccc.h"


@implementation ccc


-(void)setAge:(int)age setNo:(int)no{


    _age = age;

    _no = no;

    

    return;

}

-(int)age{

    return _age;

}

-(int)no{

    return _no;

}


-(id)initWithAge:(int)age andNo:(int)no{

    

    //首先要调用super的构造方法

    //因为 [super init] 有可能返回nil

    if( (self = [super init]) != nil){

        _age = age;

        _no = no;

    }

    return  self;

    


}


//重写父类的description方法

//当使用%@打印一个对象的时候,会调用下面的方法

-(NSString *)description{

    

    NSString *s = [NSString stringWithFormat:@"age--%d,no--%d",_age,_no];

    return s;

}



@end


main.m:


#import 


#import "ccc.h"


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

    @autoreleasepool {

        

        ccc *c1 = [[ccc allocinitWithAge:100 andNo:222];

        

        NSLog(@"age--%d,no--%d",c1.age,c1.no);

        NSLog(@"%@",c1);

    }

    return 0;


}


阅读(428) | 评论(0) | 转发(0) |
0

上一篇:OC入门篇1

下一篇:OC入门篇3

给主人留下些什么吧!~~