A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Flower. 中级黑马   /  2015-12-2 11:16  /  737 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*方法与函数的区别*/
#import <Foundation/Foundation.h>

@interface Test : NSObject{
    @public
    int age;
   
}

-(void) add:(int) i num2:(int) a;
@end
@implementation Test

-(void) add:(int) i num2:(int) a{
    NSLog(@"haha...%d",age);
}

@end


//函数
void add(int i,int a);
void add(int i,int a){
    //age=100;错误
    NSLog(@"haha..函数.");
}

//在函数内访问对象的属性必须通过对象
void test(Test* t);
void test(Test* t){
    t->age=100;
     NSLog(@"haha..函数.%d",t->age);
}

int main(){
    //方法的调用
    Test* t= [Test new];
    [t add:2 num2:3];
  
    //函数的调用
    add(2,3);
   
    test(t);
    return 0;
}

1 个回复

倒序浏览
我们说一个类有方法和字段,也可以说它有成员函数和成员变量,函数和变量是程序层面的术语,而方法和字段是设计层面的术语。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马