练习

//
//  main.m
//  MemoryManagerAutoRelease
//
//  Created by jimzhai on 13-2-8.
//  Copyright (c) 2013年 jimzhai. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Engine : NSObject
{
    int power;
    NSString *brand;
}

- (id)initWithPower:(int)_power Brand:(NSString*)_brand;

@property int power;

@property (assign)NSString* brand;

@end//Engine

@implementation Engine

@synthesize power = power ;

@synthesize brand = brand;

- (id) initWithPower: (int)_power Brand:(NSString*)_brand
{
    if(self = [super init])
    {
        power = _power;
        brand = _brand;
    }
    return self;
}

@end


@interface Car : NSObject
{
    int price;
    NSString *brand;
    Engine *engine;
}
@property int price;

@property(assign) NSString* brand;

@property(retain) Engine* engine;

- (id)initWithPrice:(int)_price Brand:(NSString*)_brand Engine: (Engine*)_engine;

@end//Car

@implementation Car

@synthesize price = price;

@synthesize brand = brand;

@synthesize engine = engine;

- (id)init
{
    self = [super init];
    if (self) {
        price = 100000;
        brand = @"BMW";
        engine = [Engine alloc];
        [engine setBrand:@"BWM"];
        [engine setPower:2600];
    }
    return self;

}
- (id)initWithPrice:(int)_price Brand: (NSString*)_brand Engine: (Engine*)_engine
{
    if(self = [super init])
    {
        price = _price;
        brand = _brand;
        engine = _engine;
    }
    return self;
}

-(NSString*) description
{
    return [NSString stringWithFormat:@"price: %d \nbrand: %@\nengine retain: %ld \nengine brand: %@",price,brand,[engine retainCount],[engine brand]];
}
- (void)dealloc
{
    [engine release];
    [super dealloc];
}
@end//Car

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Car *car = [[Car alloc] initWithPrice:70000 Brand:@"BMW" Engine:[[Engine alloc]initWithPower:800000 Brand:@"GTA"]];
//        [car setBrand:@"BMW"];
        Engine *engine = [car engine];
        NSLog(@"%@",[car description]);
        
        Engine *new_engine = [Engine alloc];
        [new_engine setBrand:@"MESIDESE"];
        [new_engine setPower:36000];
        [car setEngine:new_engine];
        NSLog(@"OLOLOLOLOLOL: %ld",[engine retainCount]);
        [new_engine release];
        NSLog(@"%@",[car description]);
        [car autorelease];
        
        
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值