1.Person.h文件
#import <Foundation/Foundation.h>
@class Dog;
@interface Person : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) float weight;
@property (strong, nonatomic) Dog *dog;
- (Person *(^)(void))run;
- (Person *(^)(void))eat;
- (Person *)go;
- (Person *)drink;
@end
2.Person.m文件中
#import "Person.h"
@implementation Person
- (Person *(^)(void))run {
return ^(void){
self.weight -=1;
return self;
};
}
- (Person *(^)(void))eat {
return ^(void){
self.weight +=1;
return self;
};
}
- (Person *)go{
self.weight -=1;
return self;
}
- (Person *)drink {
self.weight +=1;
return self;
}
@end
3.主函数中(main.m)
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Person.h"
#import "Dog.h"
int main(int argc, char * argv[]) {
Person *person = [[Person alloc] init];
person.name = @"小明";
person.weight = 130;
person.run().eat().eat().eat();
NSLog(@"%f", person.weight);
[[[[person run] eat] eat] eat];
NSLog(@"%f", person.weight);
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}