//
// main.m
// Person_2_video
//
// Created by on 14-9-14.
// Copyright (c) 2014年 apple. All rights reserved.
//
// Object-C的内存分析
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// 1、类是静态的概念,存放在代码区
// 2、对象是alloc出来的,存放在堆区,堆区是用来动态存放内存的
// 类的每个实例变量在不同的对象中都有不同的值(静态变量除外)
// 3、方法也只是在被调用的时候,程序运行的时候才占用内存
// 当类没有指定初始化方法的时候,编译器会为类自动添加init方法,如果有,编译器就不再添加
// 这里的initWithAge就是指定初始化方法
Person *person = [[Person alloc] initWithAge:20 identify:50];
NSLog(@"%d", [person age]);
[person setAge:50];
NSLog(@"identify = %d, age = %d", [person identify], [person age]);
// 返回当前类的类名
NSLog(@"self class is: %@", [person selfClassName]);
// 返回父类的类名
NSLog(@"super class is: %@", [person superClassName]);
}
return 0;
} // main
//
// Person.h
// Person_2_video
//
// Created by on 14-9-14.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int identify;
int age;
}
- (id) initWithAge: (int) _age identify: (int) _identify;
- (int) identify;
- (int) age;
- (void) setAge: (int) _age;
- (id) selfClassName;
- (id) superClassName;
@end
//
// Person.m
// Person_2_video
//
// Created by on 14-9-14.
// Copyright (c) 2014年 apple. All rights reserved.
//
#import "Person.h"
@implementation Person
- (id) initWithAge: (int) _age identify: (int) _identify {
if (self = [super init]) {
age = _age;
identify = _identify;
}
return self;
} // initWithAge
- (int) identify {
return identify;
} // identify
- (int) age {
return age;
} // age
- (void) setAge: (int) _age {
age = _age;
} // setAge
- (id) selfClassName {
return [self class];
} // selfClass
- (id) superClassName {
return [self superclass];
} // superClass
@end
转载于:https://my.oschina.net/are1OfBlog/blog/313458