类的组合使用:
类的组合使用,就是类里边的实例变量是自己定义的类。用法与普通类使用一样。
例子:
family.h
#import <Foundation/Foundation.h> #import "Father.h" #import "Mother.h" #import "Son.h" @interface Family : NSObject{
//father Father *_father; //mother Mother *_mother; //son Son *_son; }
- (instancetype)initWithFather:(Father *)father mother:(Mother *)mother son:(Son *)son;
- (void) setFather : (Father *)father; - (void)setMother:(Mother *)mother; - (void)setSon :(Son *)son;
- (Father *) father; - (Mother *)mother; - (Son *)son;
@end |
family.m
#import "Family.h" @implementation Family - (instancetype)initWithFather:(Father *)father mother:(Mother *)mother son:(Son *)son{ _father = father; _mother = mother; _son = son; return self; } - (void) setFather : (Father *)father{ _father = father; } - (void)setMother:(Mother *)mother{ _mother = mother; } - (void)setSon :(Son *)son{ _son = son; } - (Father *) father{ return _father; } - (Mother *)mother{ return _mother; } - (Son *)son{ return _son; } @end |
Son.h
#import <Foundation/Foundation.h>
@interface Son : NSObject{ NSString *_name; NSInteger _age; } //初始化方法 - (instancetype) initWithName:(NSString *)name age:(NSInteger)age; //getter - (NSString *)name; - (void)setName:(NSString *)name;
- (NSInteger)age; - (void)setAge:(NSInteger)age; @end |
Son.m
#import "Son.h" @implementation Son - (instancetype) initWithName:(NSString *)name age:(NSInteger)age{ _name = name; _age = age; return self; } - (NSString *)name{ return _name; } - (void)setName:(NSString *)name{ _name = name; }
- (NSInteger)age{ return _age; } - (void)setAge:(NSInteger)age{ _age = age; }
@end |
Father.h
#import <Foundation/Foundation.h> @interface Father : NSObject{ NSString *_name; } - (instancetype)initWithName:(NSString *)name; - (NSString *)name; - (void) setName :(NSString *)name; @end |
Father.m
#import "Father.h"
@implementation Father
- (instancetype)initWithName:(NSString *)name{ _name = name; return self; }
- (NSString *)name{ return _name; } - (void) setName :(NSString *)name{ _name = name; }
@end |
Mother.h
#import <Foundation/Foundation.h>
@interface Mother : NSObject{ NSString *_name; }
- (instancetype) initWithName :(NSString *)name;
- (NSString *)name; - (void) setName:(NSString *)name; @end |
Mother.m
#import "Mother.h" @implementation Mother - (instancetype) initWithName :(NSString *)name{ _name = name; return self; } - (NSString *)name{ return _name; } - (void) setName:(NSString *)name{ _name = name; } @end |
main.m
#import <Foundation/Foundation.h> #import "Son.h" #import "Family.h" #import "Father.h" #import "Mother.h"
int main(int argc, const char * argv[]) { @autoreleasepool { Son *s = [[Son alloc]initWithName:@"六娃" age:19]; Father *f = [[Father alloc]initWithName:@"小金刚"]; Mother *m = [[Mother alloc]initWithName:@"女王大人"];
Family *family = [[Family alloc]initWithFather:f mother:m son:s]; // //第一种方式 // //getter方法 // Son *s1 = [family son]; // NSLog(@"%@",s1.name);
//第二种方式 NSLog(@"%@",[[family son]name]);//2015-04-14 16:50:01.489 OCLesson2_类的组合使用[2215:127250] 六娃
NSLog(@"%ld",[[family son]age]);//2015-04-14 16:50:01.490 OCLesson2_类的组合使用[2215:127250] 19
NSLog(@"%@",[[family father]name]);//2015-04-14 16:50:01.490 OCLesson2_类的组合使用[2215:127250] 小金刚 NSLog(@"%@",[[family mother]name]);//2015-04-14 16:50:01.490 OCLesson2_类的组合使用[2215:127250] 女王大人
//第三种方式 //点语法 可以自动识别getter和setter方法 //getter NSLog(@"%@",s.name); //setter s.name = @"七娃"; //getter NSLog(@"%@",s.name); //getter NSLog(@"%@",family.son.name); //getter NSLog(@"%ld",family.son.age); //getter NSLog(@"%@",family.father.name); //getter NSLog(@"%@",family.mother.name);
} return 0; } |