oc 函数多个参数调用

本文介绍了Objective-C的基本概念,包括类的定义、对象的创建及方法调用等,并详细讲解了如何使用属性简化getter和setter方法的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原帖地址 http://blog.youkuaiyun.com/cherry609195946/article/details/19609485


一. 基本概念

   1. OC中没有命名空间机制,也没有包的概念,为了区分不同的类,在类名前加前缀

    2. OC中的关键字都以@开头,用于区分C和C++的关键字,字符串也以@开头,比如:

[objc]  view plain  copy
  1. @interface Student : NSObject  
  2. NSLog(@"Hello World!");  

二.  面向对象

    1. @interface --------> 等于java中的class

     2. 类名后的冒号:---------> 等于java中的extends

     3. 函数前面的减号-  ---------> 表示对象方法

       函数前面的加号+  ---------> 表示类方法,等于java的static方法

     4. 函数的返回类型和参数的类型必须用括号,形参用冒号:表示

     以bean类的头文件示例:

[objc]  view plain  copy
  1. @interface Student : NSObject{  
  2.     int age;  
  3.     int height;  
  4. }//成员变量的声明区间,成员变量必须在此声明  
  5.   
  6. - (int)age;//本来是getAge,但是OC的习惯是用变量来命名get方法  
  7. - (void)setAge:(int)newAge;  
  8. //多形参的函数写法比较特别  
  9. - (void)setAge:(int)newAge andHeight:(int)newHeight;  
  10. @end//类的结束标记,必须写  

    对应的m文件为

[objc]  view plain  copy
  1. #import "Student.h"  
  2. @implementation Student  
  3.   
  4. - (int)age{  
  5.     return age;  
  6. }  
  7. - (void)setAge:(int)newAge{  
  8.     age = newAge;  
  9. }  
  10. - (void)setAge:(int)newAge andHeight:(int)newHeight{  
  11.     age = newAge;  
  12.     height = newHeight;  
  13. }  
  14. @end  

    5. 对象的创建和方法调用

[objc]  view plain  copy
  1. //OC创建对象分2步,先调用静态无参函数alloc申请内存,在调用静态无参函数init初始化  
  2. //1. Student *stu = [Student alloc];//仅仅为对象分陪内存空间  
  3. //2. stu = [stu init];//真正创建对象  
  4. //以上2步一般简写为:  
  5. Student *stu = [[Student alloc] init];   
  6. //设置  
  7. [stu setAge:100];  
  8. [stu setAge:100 andHeight:50];  
  9. //获取  
  10. NSLog(@"age is %i",[stu age]);  
  11. [stu release];//对象使用完毕要释放内存  

   6. 对象的构造方法

[objc]  view plain  copy
  1. @interface Student{  
  2.     int _age;//标准写法  
  3.     int _no;  
  4. }  
  5. - (void)setAge:(int)age;  
  6. - (int)age;  
  7. - (void)setNo:(int)no;  
  8. - (int)no;  
  9. //构造方法  
  10. - (id)initWithAge:(int)age andNo:(int)no;  
  11. @end  
    对应的m文件:

[objc]  view plain  copy
  1. #include "Student.h"  
  2. @implementation Student  
  3.   
  4. - (int)age{  
  5.     return _age;  
  6. }  
  7. - (void)setAge:(int)age{  
  8.     _age = age;  
  9. }  
  10. //...  
  11. //实现构造方法  
  12. - (id)initWithAge:(int)age andNo:(int)no{  
  13.     //以下写法不严谨  
  14.     //self = [super init];  
  15.     //_age = age;  
  16.     //_no = no;  
  17.     if(self=[super init]){  
  18.         _age = age;  
  19.         _no = no;  
  20.     }  
  21.     return self;  
  22. }  
  23. @end  

     7. @property简化set和get

        在头文件中这样声明:

[objc]  view plain  copy
  1. @property int age;//编译器会自动补出其set和get方法  
        在m文件中这样实现:

[objc]  view plain  copy
  1. @synthesize age;//编译器会自动生成set和get方法的实现  

     8. 使用@class 提高编译效率,由于在h文件中,使用include的话,是将其内容全部拷贝过来,会影响编译效率,而且对应的h文件只有有任何改动,又要重新编译,为了解决这个问题,在h文件中,如果引用了另外一个对象,则使用@class Object; 代替include语法,用于告诉编译器该对象是存在的

[objc]  view plain  copy
  1. @class Book;  
  2. @interface Student : NSObject  
  3. @property Book *book;  
  4. @end  
        但是在对应的m文件中,则必须要include入Book.h了.



原帖地址 http://blog.youkuaiyun.com/cherry609195946/article/details/19609485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值