- #import <Foundation/Foundation.h>
- @interface People : NSObject
- @property(nonatomic,strong) NSString *name;
- @property(nonatomic,assign) int age;
- - (void)setName:(NSString *)name andAge:(int)age;
- @end
- @implementation People
- - (void)setName:(NSString *)name andAge:(int)age
- {
- [self setName:name];
- [self setAge:age];
- }
- - (NSString *)description
- {
- return [NSString stringWithFormat:@"people:姓名:%@,年龄:%d",self.name,self.age];
- }
- - (void)dealloc
- {
- NSLog(@"people:%@----------dealloc",self);
- }
- @end
复制代码- @interface Book : NSObject
- @property(nonatomic,strong) NSString *bookname;
- @property(nonatomic,strong) NSString *product;
- @property(nonatomic,strong) People *author;
- @end
- @implementation Book
- - (NSString *)description
- {
- return [NSString stringWithFormat:@"book:书名:%@,出版社:%@,作者信息:<%@>",self.bookname,self.product,[self.author description]];
- }
- - (void)dealloc
- {
- NSLog(@"%@-------dealloc",self);
- }
- @end
复制代码- @interface Student : People
- @property(nonatomic,assign) int num;
- @property(nonatomic,strong) Book *book;
- - (NSString *)study;
- @end
- @implementation Student
- - (NSString *)study
- {
- NSString *name = self.book.bookname;
- return name;
- }
- - (NSString *)description
- {
- return [NSString stringWithFormat:@"student:学生信息:<%@,学号:%d>读的书信息:<%@>",[super description],self.num,[self.book description]];
-
- }
- - (void)dealloc
- {
- NSLog(@"%@-------dealloc",self);
- }
- @end
复制代码- People* setpeople()
- {
- People *p = [[People alloc] init];
- p.name = @"莫言";
- p.age = 57;
- return p;
- }
- Book* book()
- {
- Book *b = [[Book alloc]init];
- b.bookname = @"《丰乳肥臀》";
- b.product = @"北方出版社";
- b.author = setpeople();
- return b;
- }
- //Student* setstudent()
- int main()
- {
- NSLog(@"------------设计一个对象方法-study:输出书名----------------");
- Student *stu = [[Student alloc] init];
- stu.num = 13;
- stu.book = book();
- stu.name = @"张三";
- stu.age = 17;
- // NSLog(@"%@",stu);
- // NSString *haha= [stu study];
- // NSLog(@"%@",haha);
- // return 0;
- }
复制代码 为什么最后销毁的时候还会有父类销毁?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
|