zoukankan      html  css  js  c++  java
  • OC中方法与函数的区别

    方法:方法是Objective-C独有的一种结构,只能在Objective-C中声明、定义和使用,C语言不能声明、定义和使用。

    1、类方法以+号开头,对象方法以-号开头
    + (void) init;        // 类方法
    - (void) show;                // 对象方法

    2、在@interface和@end之间声明,在@implementation和@end之间定义
    @interface Test : NSObject
    // 方法声明
    + (void) init;
    - (void) show;
    @end
    @implementation Test
    // 方法实现
    + (void) init
    {

    }
    - (void) show
    {

    }
    @end
    3、类方法只能由类来调用,对象方法只能由方法来调用
    // 调用类方法
    [Test init];
    // 调用对象方法
    Test *t = [Test new];
    [t show];
    4、方法归类、对象所有。
    5、方法声明和实现中用到的数据类型必须用()括住。


    函数:函数即C语言中的函数,在C和Objective-C中都声明、定义和使用。

    1、函数能写在文件中的任意位置(@interface和@end之间除外),函数归文件所有。
    int add(int num1, int num2)
    {
            return num1 + num2;
    }
    2、函数调用不依赖于对象。
    int sum = add(1, 2);
    3、函数内部不能直接通过成员变量名访问某个对象的成员变量。
    可以选择,但是别选择放弃
  • 相关阅读:
    PHP多条件模糊查询
    纯干货!一款APP从设计稿到切图过程全方位揭秘(转)
    0532. K-diff Pairs in an Array (M)
    0933. Number of Recent Calls (E)
    0139. Word Break (M)
    0713. Subarray Product Less Than K (M)
    0399. Evaluate Division (M)
    0495. Teemo Attacking (M)
    0179. Largest Number (M)
    0389. Find the Difference (E)
  • 原文地址:https://www.cnblogs.com/hangdada/p/4982994.html
Copyright © 2011-2022 走看看