增加类方法一种手断,不能有成员变量。这个跟c++与java抽象类与接口还是有些不一样。类函数分类。
2.扩展extersion
匿名的categroy,实现可以有成员变量。
下面是实现的例子:
//
// Category.h
// objc
//
// Created by sunkey on 12-8-13.
// Copyright (c) 2012年 sunkey. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Computer : NSObject
{
int i;
}
-(void)PCName;
//Category is implementing a method which will also be implemented by its primary class
//-(void)Width;
@end
//extersion
@interface Computer()
{
int iValue;
}
-(void)SetIVaule:(int)newValue;
@end
@interface Computer (NoeBook)
{
//Ivars may not be placed in categories
//int j;
}
-(void)NoteName;
-(void)Width;
-(void)Height;
+(void)NSCPU;
@end
//
// Category.m
// objc
//
// Created by sunkey on 12-8-13.
// Copyright (c) 2012年 sunkey. All rights reserved.
//
#import "Category.h"
@implementation Computer
-(void)PCName
{
i = -1;
NSLog(@"pc %d\n",i);
[self NoteName];
}
/*
-(void)Width
{
i = 200;
NSLog(@"width %d\n",i);
}
*/
-(void)SetIVaule:(int)newValue
{
iValue = newValue;
}
@end
@implementation Computer (NoeBook)
-(void)NoteName
{
//[self PCName];
i = 1;
NSLog(@"note %d\n",i);
}
-(void)Width
{
i = 100;
NSLog(@"width %d\n",i);
}
-(void)Height
{
NSLog(@"height %d\n",i);
}
+(void)NSCPU
{
NSLog(@"intel \n");
}
@end
扩展:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html