//
// 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;
}
练习
最新推荐文章于 2022-06-11 20:51:44 发布