1、类调用
1_ClassCall.m
#import <Foundation/Foundation.h>
#import "1_ClassDef.h"
int main(int argc, char* args[])
{
@autoreleasepool
{
KFPerson* person;
person = [[KFPerson alloc] init];
[person say:@"孙中山"];
[person
setName: @"古天乐"
andAge: 41];
NSString* myInfoStr = [person info];
NSLog(@"info: %@", myInfoStr);
[KFPerson foo];
}
}
1_ClassDef.h
#import <Foundation/Foundation.h>
@interface KFPerson : NSObject
{
// 定义变量
NSString* _name;
int _age;
}
// 定义setName:andAge方法
- (void) setName: (NSString*) name andAge: (int) age;
- (void) say: (NSString*) content;
- (NSString*) info;
+ (void) foo;
@end
1_ClassDef.m
#import <Foundation/Foundation.h>
#import "1_ClassDef.h"
@implementation KFPerson
{
// 此变量只能在impl部份使用
int _testAttr;
}
- (void) setName: (NSString*) n andAge: (int) a
{
_name = n;
_age = a;
}
- (void) say: (NSString*) c
{
NSLog(@"hello: %@", c);
}
- (NSString*) info
{
[self test];
// return [NSString stringWithFormat: @"我是一个人,名字是%@, 年龄为%d" ,_name , _age];
return _name;
}
- (void) test
{
NSLog(@"我只能在impl部份被调用");
}
+ (void) foo
{
NSLog(@"可以直接通过类名调用, 相当于JAVA的static方法");
}
@end
2、函数
2_Function.h
#import <Foundation/Foundation.h>
@interface KFUser1 : NSObject
{
@public
NSString* test;
NSString* _test;
}
@property NSString* name;
@property NSString* pass;
@property NSDate* birth;
@end
2_Function.m
#import "2_Function.h"
@implementation KFUser1
- (void) setValue: (id) value forUndefinedKey: (id) key
{
NSLog(@"setValue:forUndefinedKey key[%@] is not exists, value = %@", key, value);
}
@end
2_FunctionTest.m
#import <Foundation/Foundation.h>
#import "2_Function.h"
int main(int argc, char* args[])
{
@autoreleasepool
{
KFUser1* user1;
user1 = [[KFUser1 alloc] init];
[user1 setName: @"jingle.ljl"];
NSLog(@"name=%@", [user1 name]);
[user1 setValue:@"testValue" forKey:@"test"];
NSLog(@"test-value=%@", user1->test);
NSLog(@"_test-value=%@", user1->_test);
[user1 setValue:@"testValue" forKey:@"test1"];
user1->test = @"testvalue1";
NSLog(@"value=%@", user1->test);
}
}
KFItem.h
#import <Foundation/Foundation.h>
@interface KFItem : NSObject
@property (nonatomic, copy) NSString* name;
@property (nonatomic, assign) int price;
@end
KFItem.m
#import "KFItem.h"
@implementation KFItem
@end
KFItemView.h
#import <Foundation/Foundation.h>
#import "KFItem.h"
@interface KFItemView : NSObject
@property(nonatomic, weak) KFItem* item;
- (void) showItemInfo;
@end
KFItemView.m
#import "KFItemView.h"
@implementation KFItemView
- (void) showItemInfo
{
NSLog(@"item 物品名为%@, 价格为%d", self.item.name, self.item.price);
}
- (void) setItem: (KFItem*) item
{
self->_item = item;
// 为item添加监听器
[self.item addObserver: self
forKeyPath: @"name"
options: NSKeyValueObservingOptionNew
context: nil];
[self.item addObserver: self
forKeyPath: @"price"
options: NSKeyValueObservingOptionNew
context: nil];
}
// 重写此方法. 当数据模型改变时, 回调监听器的该方法
- (void) observeValueForKeyPath: (NSString*) keyPath
ofObject: (id) object
change: (NSDictionary*) change
context: (void*) context
{
NSLog(@"--observerValueForKeyPath被调用---");
NSLog(@"keyPath=%@, object=%@, change=%@, context=%@", keyPath, object, [change objectForKey:@"new"], context);
}
- (void) dealloc
{
[self.item removeObserver: self forKeyPath: @"name"];
[self.item removeObserver: self forKeyPath: @"price"];
}
@end
KFItemViewTest.m
#import "KFItemView.h"
int main(int argc, char* args[])
{
@autoreleasepool
{
KFItem* item = [[KFItem alloc] init];
item.name = @"星吧克";
item.price = 5.0;
KFItemView* view = [[KFItemView alloc] init];
view.item = item;
[view showItemInfo];
item.name = @"涨价啦";
item.price = 6.0;
}
}
网友评论