1 重构学生与学校的练习

1.1 问题

本案例要求用字典解决下述问题。问题是:有一个学校,该学校有两个学院,每个学院中又有两个班级,而在每个班级中有两名学生。

现在作如下要求:

1)显示所有学生的信息。

2)只显示姓名为“zhangsan”的学生的信息。

3)只显示年龄为18岁的学生的信息。

4)如果每个学生有本书,显示所有学生的书的名称和价格。

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:定义类Book

首先,在Day04工程中新添加Book.h文件,用于定义新的类Book。

代码如下所示:

 
  1. #import <Foundation/Foundation.h>
  2. @interface Book : NSObject<NSCopying>
  3. @property(nonatomic, copy)NSString *name;
  4. @property(nonatomic,assign)int price;
  5. @end

在上述代码中,以下代码:

 
  1. @interface Book : NSObject<NSCopying>

定义了类Book,该类采用了NSCopying协议。

在上述代码中,以下代码:

  1. @property(nonatomic, copy)NSString *name;

在Book类中定义的属性name是NSString类的对象,用于存储书的名字。它有两个参数,一个是nonatomic,它代表对属性赋值的时候不加锁,即在多线程环境下访问时可能会出现数据错误,如果需要在多线程环境下运行,为保证数据不会出现错误,可使用atomic参数,它会在对属性赋值的时候加锁。另一个参数是copy,该参数一般用于NSObject类及其子类的对象,这些对象在赋值时实现深拷贝,即属性name指向的对象是赋值给它的对象的副本。

在上述代码中,以下代码:

  1. @property(nonatomic,assign)int price;

在Book类中定义的整型属性price,用于存储书的价格。它有两个参数,一个是nonatomic,另一个参数是assign,对于C语言的基本数据类型,只能选取这个参数。

然后,在类Book的实现部分,即在Book.m文件中,添加两个方法的实现,一个是copyWithZone方法,该方法是在NSCopying协议中声明的方法,用于深拷贝;另一个是dealloc方法,由于使用ARC,该方法在本类中实际上没有任何实际意义,只是在类的对象销毁时,在控制台上输出一个提示。

代码如下所示:

  1. #import "Book.h"
  2. @implementation Book
  3. -(id)copyWithZone:(NSZone*)zone
  4. {
  5. Book* book = [[Book allocWithZone:zone] init];
  6. book.name = self.name;
  7. book.price = self.price;
  8. return book;
  9. }
  10. -(void)dealloc{
  11. NSLog(@"书对象销毁了 price:%d",self.price);
  12. }
  13. @end

上述代码中,copyWithZone方法不能在类外直接被调用,它是在向一个对象发送copy消息时,由copy消息调用。在该方法中,以下代码:

  1. Book* book = [[Book allocWithZone:zone] init];
  2. book.name = self.name;
  3. book.price = self.price;

生成一个本类对象的副本。需要注意的是分配空间使用的是allocWithZone方法。

上述代码中,dealloc方法也不能在类外直接被调用,它是在一个对象被release并且对象的引用计数为0时,由内存管理程序调用的方法。

步骤二:定义类TRStudent

首先,在Day04工程中新添加TRStudent.h文件,用于定义新的类TRStudent。

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "Book.h"
  3. @interface TRStudent : NSObject
  4. @property(nonatomic,assign)int age;
  5. @property(nonatomic,copy)NSString* name;
  6. @property(nonatomic,copy)Book *book;
  7. -(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;
  8. +(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;
  9. -(void)print:(id)condition;
  10. @end

在上述代码中,定义了类Student,在类中有三个带参属性。

一个属性是整型变量age,如下代码所示:

  1. @property(nonatomic,assign) int age;

用于存储学生的年龄。

另一个属性是name,如下代码所示:

  1. @property(nonatomic,copy)NSString* name;

用于存储学生的姓名。

最后一个属性是book,如下代码所示:

  1. @property(nonatomic,copy)Book *book;

用于存储学生正在学习的书。值得注意的是,Book类采纳NSCopying协议,是因为此处使用了copy这个属性参数。

上述代码中,以下代码:

  1. -(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;
  2. +(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;

声明了TRStudent类的带参初始化方法和带参工厂方法。

上述代码中,以下代码:

  1. -(void)print:(id)condition;

声明了一个print方法,该方法用于根据形参condition的不同,打印不同的TRStudent类对象的属性值。

然后,在类Student的实现部分,即在Student.m文件中,添加带参初始化方法和工厂初始化方法以及print方法的实现。

代码如下所示:

  1. #import "TRStudent.h"
  2. @implementation TRStudent
  3. -(id)initWithAge:(int)age
  4. andName:(NSString*)name
  5. andBook:(Book*)book{
  6. self = [super init];
  7. if (self) {
  8. self.age = age;
  9. self.name = name;
  10. self.book = book;
  11. }
  12. return self;
  13. }
  14. +(id)studentWithAge:(int)age
  15. andName:(NSString*)name
  16. andBook:(Book*)book{
  17. return [[TRStudent alloc]initWithAge:age andName:name andBook:book];
  18. }
  19. -(void)print:(id)condition
  20. {
  21. bool a = [condition isKindOfClass:[NSNumber class]];
  22. bool b = [condition intValue] == self.age;
  23. bool c = [condition isKindOfClass:[NSString class]];
  24. bool d = [self.name isEqualToString:condition];
  25. if ((a && b) || (c && d))
  26. NSLog(@"stu name:%@, age:%d", self.name, self.age);
  27. }
  28. @end

上述代码中,以下代码:

  1. bool a = [condition isKindOfClass:[NSNumber class]];
  2. bool b = [condition intValue] == self.age;

定义了两个bool类型的变量。

变量a被赋值为形参condition是否是NSNumber类对象的判断结果,其中方法isKindOfClass的作用是判断对象condition是否是NSNumber类的对象。方法isKindOfClass的形参为Class类型的值,该类型的值通过工厂方法class获得。

变量b被赋值为形参condition的值是否与当前对象age属性值相等的判断结果。通过向形参condition发送intValue消息,如果condition的值为数字字符串,则将其转换成整型数据。

上述代码中,以下代码:

  1. bool c = [condition isKindOfClass:[NSString class]];
  2. bool d = [self.name isEqualToString:condition];

定义了两个bool类型的变量。

变量c被赋值为形参condition是否是NSString类对象的判断结果。

变量d被赋值为当前对象name属性值是否与形参condition的值相等的判断结果。方法isEqualToString是NSString类的方法,用于判断两个字符串是否相等。

上述代码中,以下代码:

  1. if ((a && b) || (c && d))
  2. NSLog(@"stu name:%@, age:%d", self.name, self.age);

如果条件(a && b)为真则表示形参condition为NSNumber类的对象并且将其拆箱后的值与当前对象age属性值相等。

如果条件(c && d)为真则表示形参condition为NSString类的对象并且与当前对象name属性值是同一字符串。

步骤三:在主程序中创建八个学生对象

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. }
  41. return 0;
  42. }

上述代码中,以下代码:

  1. //创建学生对象
  2. Book* book1 = [[Book alloc] init];
  3. book1.name = @"sanguo";
  4. book1.price = 20;
  5. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];

首先创建了一个Book类的对象book1,然后使用工厂方法studentWithAge:andName:andBook:创建TRStudent类的对象stu1。

步骤四:在主程序中创建班级字典

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. }
  46. return 0;
  47. }

上述代码中,以下代码:

  1. //班级
  2. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  3. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  4. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  5. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];

创建了四个字典对象,每个字典对象为一个班级,班级中有两个学生作为字典的值value,并为其定义了编号@“1”和@“2”作为关键字key。其中使用工厂方法dictionaryWithObjectsAndKeys:创建字典对象。值得注意的是,在形参的最后是nil,它代表放入字典中的键值对结束。

步骤五:在主程序中创建学院和学校

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. //学院
  46. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  47. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  48. //学校
  49. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];
  50. }
  51. return 0;
  52. }

上述代码中,以下代码:

  1. //学院
  2. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  3. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  4. //学校
  5. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];

创建了两个学院字典,每个学院字典中有两个班级字典作为字典的值value,并为其定义了编号@“1”和@“2”作为关键字key。还创建了一个学校字典,其中包含上述两个学院字典作为字典的值value,并为其定义了编号@“1”和@“2”作为关键字key。

步骤六:在主程序中遍历输出所有学生信息

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. //学院
  46. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  47. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  48. //学校
  49. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];
  50. //遍历学校
  51. NSEnumerator *e = [tarena objectEnumerator];
  52. NSDictionary* college;
  53. while (college = [e nextObject])
  54. {
  55. //遍历学院
  56. NSEnumerator *e1 = [college objectEnumerator];
  57. NSDictionary* class;
  58. while (class = [e1 nextObject])
  59. {
  60. //遍历班级
  61. NSEnumerator *e2 = [class objectEnumerator];
  62. TRStudent* stu;
  63. while (stu = [e2 nextObject])
  64. {
  65. NSLog(@"stu name:%@, age:%d", stu.name, stu.age);
  66. }
  67. }
  68. }
  69. }
  70. return 0;
  71. }

上述代码中,以下代码:

  1. //遍历学校
  2. NSEnumerator *e = [tarena objectEnumerator];
  3. NSDictionary* college;
  4. while (college = [e nextObject])
  5. {
  6. //遍历学院
  7. NSEnumerator *e1 = [college objectEnumerator];
  8. NSDictionary* class;
  9. while (class = [e1 nextObject])
  10. {
  11. //遍历班级
  12. NSEnumerator *e2 = [class objectEnumerator];
  13. TRStudent* stu;
  14. while (stu = [e2 nextObject])
  15. {
  16. NSLog(@"stu name:%@, age:%d", stu.name, stu.age);
  17. }
  18. }
  19. }

通过三重枚举遍历方法遍历所有学生的信息。以下代码:

  1. //遍历学校
  2. NSEnumerator *e = [tarena objectEnumerator];

是通过NSDictionary类的objectEnumerator方法,获取学校tarena的值value的枚举器。

以下代码:

  1. NSDictionary* college;
  2. while (college = [e nextObject])

是通过NSEnumerator类的nextObject方法,使用循环逐个获取学校tarena中的每一个值value对象。

以下代码:

  1. //遍历学院
  2. NSEnumerator *e1 = [college objectEnumerator];
  3. NSDictionary* class;
  4. while (class = [e1 nextObject])

是通过建立学院college的值value枚举器,使用循环逐个获得每一个值value对象。

以下代码:

  1. //遍历班级
  2. NSEnumerator *e2 = [class objectEnumerator];
  3. TRStudent* stu;
  4. while (stu = [e2 nextObject])

是通过建立班级class的值value枚举器,使用循环逐个获得每一个值value对象。

步骤七:在主程序中遍历显示指定条件的学生信息

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. //学院
  46. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  47. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  48. //学校
  49. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];
  50. //遍历学校
  51. NSEnumerator *e = [tarena objectEnumerator];
  52. NSDictionary* college;
  53. while (college = [e nextObject])
  54. {
  55. //遍历学院
  56. NSEnumerator *e1 = [college objectEnumerator];
  57. NSDictionary* class;
  58. while (class = [e1 nextObject])
  59. {
  60. //遍历班级
  61. NSEnumerator *e2 = [class objectEnumerator];
  62. TRStudent* stu;
  63. while (stu = [e2 nextObject])
  64. {
  65. NSLog(@"stu name:%@, age:%d", stu.name, stu.age);
  66. }
  67. }
  68. }
  69. //遍历学校
  70. e = [tarena objectEnumerator];
  71. while (college = [e nextObject])
  72. {
  73. //遍历学院
  74. NSEnumerator *e1 = [college objectEnumerator];
  75. NSDictionary* class;
  76. while (class = [e1 nextObject])
  77. {
  78. //遍历班级
  79. NSEnumerator *e2 = [class objectEnumerator];
  80. TRStudent* stu;
  81. while (stu = [e2 nextObject])
  82. {
  83. //输出年龄为18岁的学生信息
  84. [stu print:[NSNumber numberWithInt:18]];
  85. //输出姓名为“张三”的学生信息
  86. [stu print:@"zhangsan"];
  87. }
  88. }
  89. }
  90. }
  91. return 0;
  92. }

上述代码中,以下代码:

  1. //输出年龄为18岁的学生信息
  2. [stu print:[NSNumber numberWithInt:18]];
  3. //输出姓名为“张三”的学生信息
  4. [stu print:@"zhangsan"];

调用TRSTudent的print:方法,有选择的打印对象stu的信息。print:方法有一个实参[NSNumber numberWithInt:18]作为选择的条件,如若条件成立,则打印对象stu的属性。

步骤八:在主程序中显示所有学生的书的名称和价格

代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. //学院
  46. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  47. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  48. //学校
  49. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];
  50. //遍历学校
  51. NSEnumerator *e = [tarena objectEnumerator];
  52. NSDictionary* college;
  53. while (college = [e nextObject])
  54. {
  55. //遍历学院
  56. NSEnumerator *e1 = [college objectEnumerator];
  57. NSDictionary* class;
  58. while (class = [e1 nextObject])
  59. {
  60. //遍历班级
  61. NSEnumerator *e2 = [class objectEnumerator];
  62. TRStudent* stu;
  63. while (stu = [e2 nextObject])
  64. {
  65. NSLog(@"stu name:%@, age:%d", stu.name, stu.age);
  66. }
  67. }
  68. }
  69. //遍历学校
  70. e = [tarena objectEnumerator];
  71. while (college = [e nextObject])
  72. {
  73. //遍历学院
  74. NSEnumerator *e1 = [college objectEnumerator];
  75. NSDictionary* class;
  76. while (class = [e1 nextObject])
  77. {
  78. //遍历班级
  79. NSEnumerator *e2 = [class objectEnumerator];
  80. TRStudent* stu;
  81. while (stu = [e2 nextObject])
  82. {
  83. //输出年龄为18岁的学生信息
  84. [stu print:[NSNumber numberWithInt:18]];
  85. //输出姓名为“张三”的学生信息
  86. [stu print:@"zhangsan"];
  87. }
  88. }
  89. }
  90. //遍历学校
  91. e = [tarena objectEnumerator];
  92. while (college = [e nextObject])
  93. {
  94. //遍历学院
  95. NSEnumerator *e1 = [college objectEnumerator];
  96. NSDictionary* class;
  97. while (class = [e1 nextObject])
  98. {
  99. //遍历班级
  100. NSEnumerator *e2 = [class objectEnumerator];
  101. TRStudent* stu;
  102. while (stu = [e2 nextObject])
  103. {
  104. NSLog(@"book name:%@, price:%d", stu.book.name, stu.book.price);
  105. }
  106. }
  107. }
  108. }
  109. return 0;
  110. }

上述代码再一次使用了三重枚举遍历,这一次与步骤六(遍历输出所有学生信息)中的三重枚举遍历相比,区别是最后使用NSLog输出为书的名称和价格,不是学生的姓名和年龄。

1.3 完整代码

本案例中,类Book声明,即Book.h文件,完整代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. @interface Book : NSObject<NSCopying>
  3. @property(nonatomic, copy)NSString *name;
  4. @property(nonatomic,assign)int price;
  5. @end

类Book实现,即Book.m文件,完整代码如下所示:

  1. #import "Book.h"
  2. @implementation Book
  3. -(id)copyWithZone:(NSZone*)zone
  4. {
  5. Book* book = [[Book allocWithZone:zone] init];
  6. book.name = self.name;
  7. book.price = self.price;
  8. return book;
  9. }
  10. -(void)dealloc{
  11. NSLog(@"书对象销毁了 price:%d",self.price);
  12. }
  13. @end

本案例中,类TRStudent声明,即TRStudent.h文件,完整代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "Book.h"
  3. @interface TRStudent : NSObject
  4. @property(nonatomic,assign)int age;
  5. @property(nonatomic,copy)NSString* name;
  6. @property(nonatomic,copy)Book *book;
  7. -(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;
  8. +(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;
  9. -(void)print:(id)condition;
  10. @end

类TRStudent实现,即TRStudent.m文件,完整代码如下所示:

  1. #import "TRStudent.h"
  2. @implementation TRStudent
  3. -(id)initWithAge:(int)age
  4. andName:(NSString*)name
  5. andBook:(Book*)book{
  6. self = [super init];
  7. if (self) {
  8. self.age = age;
  9. self.name = name;
  10. self.book = book;
  11. }
  12. return self;
  13. }
  14. +(id)studentWithAge:(int)age
  15. andName:(NSString*)name
  16. andBook:(Book*)book{
  17. return [[TRStudent alloc]initWithAge:age andName:name andBook:book];
  18. }
  19. -(void)print:(id)condition
  20. {
  21. bool a = [condition isKindOfClass:[NSNumber class]];
  22. bool b = [condition intValue] == self.age;
  23. bool c = [condition isKindOfClass:[NSString class]];
  24. bool d = [self.name isEqualToString:condition];
  25. if ((a && b) || (c && d))
  26. NSLog(@"stu name:%@, age:%d", self.name, self.age);
  27. }
  28. @end

主程序,即main.m,完整代码如下所示:

  1. #import <Foundation/Foundation.h>
  2. #import "TRStudent.h"
  3. int main(int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. // insert code here...
  7. //创建学生对象
  8. Book* book1 = [[Book alloc] init];
  9. book1.name = @"sanguo";
  10. book1.price = 20;
  11. TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1];
  12. Book* book2 = [[Book alloc] init];
  13. book2.name = @"shuihu";
  14. book2.price = 18;
  15. TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2];
  16. Book* book3 = [[Book alloc] init];
  17. book3.name = @"xiyouji";
  18. book3.price = 28;
  19. TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3];
  20. Book* book4 = [[Book alloc] init];
  21. book4.name = @"hongluomeng";
  22. book4.price = 24;
  23. TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4];
  24. Book* book5 = [[Book alloc] init];
  25. book5.name = @"fengshenyanyi";
  26. book5.price = 22;
  27. TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5];
  28. Book* book6 = [[Book alloc] init];
  29. book6.name = @"liaozhaizhiyi";
  30. book6.price = 15;
  31. TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6];
  32. Book* book7 = [[Book alloc] init];
  33. book7.name = @"sanxiawuyi";
  34. book7.price = 17;
  35. TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7];
  36. Book* book8 = [[Book alloc] init];
  37. book8.name = @"yuefeizhuan";
  38. book8.price = 27;
  39. TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8];
  40. //班级
  41. NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];
  42. NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];
  43. NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];
  44. NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil];
  45. //学院
  46. NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];
  47. NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil];
  48. //学校
  49. NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil];
  50. //遍历学校
  51. NSEnumerator *e = [tarena objectEnumerator];
  52. NSDictionary* college;
  53. while (college = [e nextObject])
  54. {
  55. //遍历学院
  56. NSEnumerator *e1 = [college objectEnumerator];
  57. NSDictionary* class;
  58. while (class = [e1 nextObject])
  59. {
  60. //遍历班级
  61. NSEnumerator *e2 = [class objectEnumerator];
  62. TRStudent* stu;
  63. while (stu = [e2 nextObject])
  64. {
  65. NSLog(@"stu name:%@, age:%d", stu.name, stu.age);
  66. }
  67. }
  68. }
  69. //遍历学校
  70. e = [tarena objectEnumerator];
  71. while (college = [e nextObject])
  72. {
  73. //遍历学院
  74. NSEnumerator *e1 = [college objectEnumerator];
  75. NSDictionary* class;
  76. while (class = [e1 nextObject])
  77. {
  78. //遍历班级
  79. NSEnumerator *e2 = [class objectEnumerator];
  80. TRStudent* stu;
  81. while (stu = [e2 nextObject])
  82. {
  83. //输出年龄为18岁的学生信息
  84. [stu print:[NSNumber numberWithInt:18]];
  85. //输出姓名为“张三”的学生信息
  86. [stu print:@"zhangsan"];
  87. }
  88. }
  89. }
  90. //遍历学校
  91. e = [tarena objectEnumerator];
  92. while (college = [e nextObject])
  93. {
  94. //遍历学院
  95. NSEnumerator *e1 = [college objectEnumerator];
  96. NSDictionary* class;
  97. while (class = [e1 nextObject])
  98. {
  99. //遍历班级
  100. NSEnumerator *e2 = [class objectEnumerator];
  101. TRStudent* stu;
  102. while (stu = [e2 nextObject])
  103. {
  104. NSLog(@"book name:%@, price:%d", stu.book.name, stu.book.price);
  105. }
  106. }
  107. }
  108. }
  109. return 0;
  110. }
posted on 2015-12-15 19:52  A蜗牛为梦想而生A  阅读(242)  评论(0编辑  收藏  举报