OC中-方法到底是如何使用的?,oc方法使用

來源:互聯網
上載者:User

OC中-方法到底是如何使用的?,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、函數內部不能直接通過成員變數名訪問某個對象的成員變數。

加txs8882909  大家一起交流

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.