A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. #import <Foundation/Foundation.h>

  2. @interface People : NSObject
  3. @property(nonatomic,strong) NSString *name;
  4. @property(nonatomic,assign) int age;

  5. - (void)setName:(NSString *)name andAge:(int)age;
  6. @end
  7. @implementation People
  8. - (void)setName:(NSString *)name andAge:(int)age
  9. {

  10.     [self setName:name];
  11.     [self setAge:age];

  12. }
  13. - (NSString *)description
  14. {
  15.     return [NSString stringWithFormat:@"people:姓名:%@,年龄:%d",self.name,self.age];
  16. }
  17. - (void)dealloc
  18. {
  19.     NSLog(@"people:%@----------dealloc",self);
  20. }
  21. @end
复制代码
  1. @interface Book : NSObject
  2. @property(nonatomic,strong) NSString *bookname;
  3. @property(nonatomic,strong) NSString *product;
  4. @property(nonatomic,strong) People *author;
  5. @end
  6. @implementation Book
  7. - (NSString *)description
  8. {
  9.     return [NSString stringWithFormat:@"book:书名:%@,出版社:%@,作者信息:<%@>",self.bookname,self.product,[self.author description]];
  10. }
  11. - (void)dealloc
  12. {
  13.     NSLog(@"%@-------dealloc",self);
  14. }
  15. @end

复制代码
  1. @interface Student : People
  2. @property(nonatomic,assign) int num;
  3. @property(nonatomic,strong) Book *book;
  4. - (NSString *)study;
  5. @end
  6. @implementation Student
  7. - (NSString *)study
  8. {
  9.    NSString *name = self.book.bookname;
  10.     return name;
  11. }
  12. - (NSString *)description
  13. {
  14.     return [NSString stringWithFormat:@"student:学生信息:<%@,学号:%d>读的书信息:<%@>",[super  description],self.num,[self.book description]];
  15.    
  16. }
  17. - (void)dealloc
  18. {
  19.     NSLog(@"%@-------dealloc",self);
  20. }
  21. @end
复制代码
  1. People*  setpeople()
  2. {
  3.    People *p = [[People alloc] init];
  4.     p.name = @"莫言";
  5.     p.age = 57;
  6.     return p;
  7. }


  8. Book* book()
  9. {
  10.     Book *b = [[Book alloc]init];
  11.     b.bookname = @"《丰乳肥臀》";
  12.     b.product = @"北方出版社";
  13.     b.author = setpeople();
  14.     return b;
  15. }


  16. //Student* setstudent()
  17. int main()
  18. {
  19.     NSLog(@"------------设计一个对象方法-study:输出书名----------------");
  20.     Student *stu = [[Student alloc] init];
  21.     stu.num = 13;
  22.     stu.book = book();
  23.     stu.name = @"张三";
  24.     stu.age = 17;
  25. //    NSLog(@"%@",stu);
  26. //    NSString *haha= [stu study];
  27. //    NSLog(@"%@",haha);
  28. // return  0;
  29. }
复制代码
为什么最后销毁的时候还会有父类销毁?
2015-03-17 09:25:15.236 08-05-习题[895:25122] ------------设计一个对象方法-study:输出书名----------------
2015-03-17 09:25:15.238 08-05-习题[895:25122] student:学生信息:<people:姓名:张三,年龄:17,学号:13>读的书信息:<book:书名:《丰乳肥臀》,出版社:北方出版社,作者信息:<people:姓名:莫言,年龄:57>>-------dealloc
2015-03-17 09:25:15.238 08-05-习题[895:25122] people:student:学生信息:<people:姓名:张三,年龄:17,学号:13>读的书信息:<book:书名:《丰乳肥臀》,出版社:北方出版社,作者信息:<people:姓名:莫言,年龄:57>>----------dealloc
2015-03-17 09:25:15.238 08-05-习题[895:25122] book:书名:《丰乳肥臀》,出版社:北方出版社,作者信息:<people:姓名:莫言,年龄:57>-------dealloc
2015-03-17 09:25:15.239 08-05-习题[895:25122] people:people:姓名:莫言,年龄:57----------dealloc



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马