- //这是Person.h
- @interface Person : NSObject
- +(id)person;
- -(void)run;
- @end
复制代码
- //这是Person.m
- #import "Person.h"
- @implementation Person
- -(void)dealloc{
- NSLog(@"Person dealloc");
- [super dealloc];
- }
- +(id)person{
- return [[[self alloc] init] autorelease]; //返回的是对象的空间
- }
- -(void)run{
- NSLog(@"人在走");
- }
复制代码
- //这是头文件Student.h
- #import "Person"
- @interface Student: Person
-
- -(void)run;
- @end
复制代码
- //这是实现文件Student.m
- #import "Student.h"
- @implementation student
- -(void)dealloc{
- NSLog(@"Student dealloc");
- [super dealloc];
- }
- -(void)run{
- NSLog(@"Student run");
- }@end
复制代码
- //这是main.m 文件
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[]){
- @autoreleasepool{
-
-
- Person * p = [Person person];
-
- student * stu = [student new];
- [stu run];
-
- }
- }
复制代码
为什么上面的代码运行结果是
--------------------
Student run
Student dealloc
Person dealloc
Person dealloc
----------------------
难道不是应该按顺序,先让p release,然后再让Student release? |
|