類和實例方法有什么區別?類方法和實例方法有什么區別?實例方法是否是訪問器(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;}@endMNPerson *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 關注
- 941 瀏覽
添加回答
舉報
0/150
提交
取消
