Object-C代码练习【Object-C内存分析】

//
//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值