码迷,mamicode.com
首页 > 其他好文 > 详细

属性修饰词

时间:2014-10-06 22:39:30      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:color   io   ar   文件   sp   c   on   cti   log   

接口文件1:Animal.h

#import <Foundation/Foundation.h>
#import "Cat.h"

@interface Animal : NSObject
{
//    NSString *_color;
//    NSString *_name;
    Cat *_cat;
}
@property (nonatomic,assign,readwrite) NSString *color,*name;
@property (nonatomic,retain,readwrite) Cat *cat;
- (instancetype) init;
- (void) run;
- (void) dealloc;

@end

实现文件1:Animal.m

#import "Animal.h"

@implementation Animal
- (instancetype) init
{
    if ([super init])
    {
        _color = @"";
        _name = @"";
    }
    return self;
}
- (void) run
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}
- (void) dealloc
{
    [_cat release];
    [super dealloc];
}

@end

 

接口文件2:Dog.h

#import "Animal.h"

@interface Dog : Animal
{
    int _age;
}
@property (nonatomic,assign) int age;
- (void) bark;
- (void) dealloc;

@end



实现文件2:Dog.m

#import "Dog.h"

@implementation Dog
- (void) bark
{
    NSLog(@"汪汪!");
}
- (void) dealloc
{
    [super dealloc];
}

@end

 

接口文件3:Cat.h

#import <Foundation/Foundation.h>

@interface Cat : NSObject
{
@private
    int weight;
}
- (void) jump;
- (void) dealloc;

@end

 

实现文件3:Cat.m

#import "Cat.h"

@implementation Cat
- (void) jump
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}
- (void) dealloc
{
    [super dealloc];
}

@end

 

测试文件:main.m

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Dog.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        Animal *animal = [[Animal alloc] init];
        Dog *dog = [[Dog alloc] init];
        Cat *cat = [[Cat alloc] init];
        Cat *cat1 = [[Cat alloc] init];
        [cat1 autorelease];
        
        NSLog(@"%lu",[animal retainCount]);
        NSLog(@"%lu",[dog retainCount]);
        NSLog(@"%lu",[cat retainCount]);
        NSLog(@"%lu",[cat1 retainCount]);
        
        animal.cat = cat;
        dog.cat = cat;
        
        NSLog(@"%lu",[animal retainCount]);
        NSLog(@"%lu",[dog retainCount]);
        NSLog(@"%lu",[cat retainCount]);
        NSLog(@"%lu",[cat1 retainCount]);
        
        [animal release];
        NSLog(@"%lu",[animal retainCount]);
        [dog release];
        NSLog(@"%lu",[dog retainCount]);
        [cat release];
        NSLog(@"%lu",[cat retainCount]);
        NSLog(@"%lu",[cat1 retainCount]);
        
    }
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

属性修饰词

标签:color   io   ar   文件   sp   c   on   cti   log   

原文地址:http://www.cnblogs.com/jiuchabaikaishui/p/4008754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!