首页 > 代码库 > 属性修饰词
属性修饰词
接口文件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]));
}
属性修饰词