【转】oc中的类方法与对象方法

原文:

OC中方法与函数的区别
http://bbs.itheima.com/thread-138029-1-1.html
(出处: 黑马程序员IT技术论坛)

 

方法:方法是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、方法声明和实现中用到的数据类型必须用()括住。

posted @ 2016-08-15 15:58  Keizo  阅读(159)  评论(0编辑  收藏  举报