子类用一个名为subitems的数组属性保存父类对象,为什么不能获取父类对象的信息呢???在main函数中subitems为空,不知道为什么。

 

main.m如下


 1 #import <Foundation/Foundation.h>
 2 #import "BNRContainer.h"
 3 int main(int argc, const char * argv[]) {
 4     @autoreleasepool {
 5         
 6         //创建一个NSMutableArray对象,并用items变量保存该对象的地址
 7         BNRContainer *items = [[BNRContainer alloc] init];
 8         for (int i = 0; i < 10; i++) {
 9             BNRItem *item = [BNRItem randomItem];
10             [items.subitems addObject:item];
11         }
12         // item数组一直为空,是为什么????????
13         for (BNRItem *item in [items subitems]) {
14            NSLog(@"%@",item);
15        }
16         NSLog(@"%@",items);
17         //释放items所指向的NSMutableArray对象
18         items = nil;
19        
20     }
21     return 0;
22 }

 

 

BNRContainer.h

1 #import "BNRItem.h"
2 
3 @interface BNRContainer : BNRItem
4 @property (nonatomic)NSMutableArray *subitems;
5 - (NSString *)description;
6 @end

BNRContainer.m


 1 #import "BNRContainer.h"
 2 
 3 @implementation BNRContainer
 4 - (NSString *)description
 5 {
 6     NSString *string = [[NSString alloc] init];
 7     int sumtalto = 0;
 8     NSLog(@"%@",self.subitems);
 9         NSMutableArray *array = [self subitems];// 这个方法不对吗?? 不能获取subitems 中的BNRItem对象??
10         for (int i = 0;i < [array count];i++) {
11             
12             sumtalto += [array[i] valueInDollars] + [self valueInDollars];
13            
14         }
15         string = [NSString stringWithFormat:@"BNRItem and BNRContainer is total %d ",sumtalto];
16         return string ;
17     
18 
19 }
20 @end

 

 

BNRItem.h

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface BNRItem : NSObject
 4 {
 5     NSString *_itemName;
 6     NSString *_serialNumber;
 7     int _valueInDollars;
 8     NSDate *_dateCreated;
 9 
10 }
11 
12 + (instancetype)randomItem;
13 //BNRItem类的指定初始化方法
14 - (instancetype)initWithItemName:(NSString *)name
15                   valueInDollars:(int)value
16                     serialNumber:(NSString *)sNumber;
17 
18     
19 
20 
21 - (instancetype)initWithItemName:(NSString *)name;
22 
23 - (instancetype)initWithItemName:(NSString *)name
24                     serialNumber:(NSString *)sNumber;
25 - (void)setItemName:(NSString *)str;
26 - (NSString *)itemName;
27 
28 - (void)setSerialNumber:(NSString *)str;
29 - (NSString *)serialNumber;
30 
31 - (void)setValueInDollars:(int)v;
32 - (int)valueInDollars;
33 
34 - (NSDate *)dateCreated;
35 @end

BNRItem.m

  1 #import "BNRItem.h"
  2 
  3 @implementation BNRItem
  4 
  5 + (instancetype)randomItem
  6 {
  7    //创建不可变数组对象,包含三个形容词
  8     NSArray *randomAdjectiveList = @[@"Fluffy",@"Rusty",@"Shiny"];
  9     
 10     //创建不可变数组对象,包含三个名词
 11     
 12     NSArray *randomNounList = @[@"Bear",@"Spork",@"Mac"];
 13     
 14     
 15     //根据数组对象所含对象的个数,得到随机索引
 16     //注意:运算符%是模运算符,运算后得到的是余数
 17     //因此adjectiveIndex 是一个0到2(包括2)的随机数
 18     
 19     NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
 20     NSInteger nounIndex = arc4random() % [randomNounList count];
 21     
 22     //注意,类型为NSInteger的变量不是对象
 23     //NSInteger是一种针对unsigned long(无符号长整数)的类型定义
 24     
 25     NSString *randomName = [NSString stringWithFormat:@"%@  %@",
 26                                   randomAdjectiveList[adjectiveIndex],
 27                                        randomNounList[nounIndex]];
 28 
 29     int randomValue = arc4random() % 100;
 30     
 31     NSString *randomserialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
 32                                     '0' + arc4random() % 10,
 33                                     'A' + arc4random() % 26,
 34                                     '0' + arc4random() % 10,
 35                                     'A' + arc4random() % 26,
 36                                     '0' + arc4random() % 10];
 37     
 38     BNRItem *newItem = [[self alloc] initWithItemName:randomName
 39                                        valueInDollars:randomValue
 40                                          serialNumber:randomserialNumber];
 41     
 42     return newItem;
 43 
 44 
 45 
 46 }
 47 - (instancetype)initWithItemName:(NSString *)name
 48                   valueInDollars:(int)value
 49                     serialNumber:(NSString *)sNumber
 50 {
 51     //调用父类的指定初始化方法
 52     self = [super init];
 53     
 54     //父类的初始化方法是否成功创建了父类对象
 55     if (self) {
 56         //为实例变量设定初始值
 57         
 58         _itemName = name;
 59         _valueInDollars = value;
 60         _serialNumber = sNumber;
 61         
 62         //设置_dateCreated 的值为系统当前时间
 63         _dateCreated = [[NSDate alloc] init];
 64     }
 65     
 66     //返回初始化后对象的新地址
 67     
 68     return self;
 69 
 70 }
 71 - (instancetype)initWithItemName:(NSString *)name
 72 {
 73     return [self initWithItemName:name
 74                    valueInDollars:0
 75                      serialNumber:@""];
 76 
 77 }
 78 - (instancetype)init
 79 {
 80     return [self initWithItemName:@"Item"];
 81 
 82 }
 83 - (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber
 84 {
 85     return [self initWithItemName:name
 86                    valueInDollars:0
 87                      serialNumber:sNumber];
 88 
 89 }
 90 - (void)setItemName:(NSString *)str
 91 {
 92     _itemName = str ;//与self.itemName = str ;有什么区别?
 93     
 94 }
 95 - (NSString *)itemName
 96 {
 97     return _itemName;
 98 
 99 }
100 - (void)setSerialNumber:(NSString *)str
101 {
102 
103     _serialNumber = str;
104     
105 }
106 - (NSString *)serialNumber
107 {
108     return _serialNumber;
109 }
110 - (void)setValueInDollars:(int)v
111 {
112     _valueInDollars = v;
113 }
114 - (int)valueInDollars
115 {
116     return _valueInDollars;
117 }
118 - (NSDate *)dateCreated
119 {
120     return _dateCreated;
121 }
122 - (NSString *)description
123 {
124   NSString *descriptionString =
125     [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d,recorded on %@",
126                       self.itemName,
127                       self.serialNumber,
128                       self.valueInDollars,
129                       self.dateCreated ];
130     
131     return descriptionString;
132 }
133 @end

 

posted @ 2016-05-04 18:11  liqiwa  阅读(503)  评论(0编辑  收藏  举报