李洪強iOS開發之OC[017]函數和方法的區別

來源:互聯網
上載者:User

標籤:

 

 

 

//

//  main.m

//  15 - 函數和對象的方法的區別

//

//  Created by vic fan on 16/7/12.

//  Copyright © 2016年 李洪強. All rights reserved.

//

 

    函數和對象方法的區別

對象方法:

- (void)run;

#import <Foundation/Foundation.h>

 

@interface Person : NSObject{

@public

    //定義執行個體變數

    NSString *_name;//姓名

    int _age;//年齡

}

//方法的聲明;

- (void)run;//人跑的方法的聲明

@end

//類的實現

@implementation Person

//方法的實現

- (void)run{

    NSLog(@"周杰倫的床邊故事");

}

 

@end

 

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

    @autoreleasepool {

        /**

         *  建立對象

         */

        Person *p = [Person new];

        /**

         *  給執行個體變數賦值

         */

        p->_age = 12;

        p->_name = @"李洪強";

        

        //調用方法

        [p run];

      //  對象方法不能調用函數

        

        

        對象方法:

        1 對象方法都以 - 開頭,類方法以 + 開頭

        2對象方法只能由對象來調用,不能當做函數一樣調用

        3對象方法歸類/對象所有

        4 對象方法的實現只能寫在@implementtation...@end中

        對象方法的聲明只能寫在 @interface...@end中

    

        

        函數:

        1 所有的函數都是平行的

        2 函數使用的時候,可以直接調用

        3 函數不存在了隸屬關係

        4 函數不能寫在 @interface 中的,但是

        可以寫在@implation中的,不可以寫在主函數中的

        5 函數不能訪問對象中的執行個體變數

        

        

        

        

        

        

        

    }

    return 0;

}

 

李洪強iOS開發之OC[017]函數和方法的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.