类和实例方法有什么区别?类方法和实例方法有什么区别?实例方法是否是访问器(getter和setter),而类方法几乎是所有其他东西?
3 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
@interface MyClass : NSObject+ (void)aClassMethod;- (void)anInstanceMethod;@end
[MyClass aClassMethod];MyClass *object = [[MyClass alloc] init];[object anInstanceMethod];
NSString
+stringWithFormat:
NSArray
+arrayWithArray:
NSArray
-count
临摹微笑
TA贡献1982条经验 获得超2个赞
+
-
static int numberOfPeople = 0;@interface MNPerson : NSObject { int age; //instance variable}+ (int)population; //class method. Returns how many people have been made.- (id)init; //instance. Constructs object, increments numberOfPeople by one.- (int)age; //instance. returns the person age@end@implementation MNPerson- (id)init{ if (self = [super init]){ numberOfPeople++; age = 0; } return self;}+ (int)population{ return numberOfPeople;}- (int)age{ return age;}@end
MNPerson *micmoo = [[MNPerson alloc] init];MNPerson *jon = [[MNPerson alloc] init];NSLog(@"Age: %d",[micmoo age]);NSLog(@"%Number Of people: %d",[MNPerson population]);
+ (int)square:(int)num{ return num * num;}
[MathFunctions square:34];
而不必实例化类!
+ (NSArray *)arrayWithObject:(id)object
- 3 回答
- 0 关注
- 878 浏览
添加回答
举报
0/150
提交
取消