OC-dealloc、组合与聚合以及面向对象的三大特征...

1. dealloc方法

//对象死亡之前的遗嘱

实例代码
SHExample类:

SHExample.m
#import "SHExample.h"

@implementation SHExample
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"对象被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"对象被销毁了");
}
@end

main函数:

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

int main(int argc, const char * argv[]) {
    @autoreleasepool
    {
        SHExample *e = [[SHExample alloc] init];
        NSLog(@"------");
    }
    NSLog(@"++++++");
    return 0;
}

运行结果:
2016-08-25 09:08:26.883 day16_01[639:30976] 对象被创建了
2016-08-25 09:08:26.884 day16_01[639:30976] ——
2016-08-25 09:08:26.885 day16_01[639:30976] 对象被销毁了
2016-08-25 09:08:26.885 day16_01[639:30976] ++++++

2. 组合与聚合

2.1是指两个(或两个以上)类之间的一种关系
2.2是(一个)整体类与(剩下的多个)部分类之间的关系
2.3组合是两个(或以上)类的强关系,即同生共死
2.4聚合是两个(或以上)类的弱关系,非同生共死

实例代码
SHButton类:

SHButton.h:
#import <Foundation/Foundation.h>

@interface SHButton : NSObject

@end
SHButton.m:
#import "SHButton.h"

@implementation SHButton
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"按钮被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"按钮被销毁了");
}
@end

SHEdit类:

SHEdit.h:
#import <Foundation/Foundation.h>

@interface SHEdit : NSObject

@end
SHEdit.m
#import "SHEdit.h"

@implementation SHEdit
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"编辑框被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"编辑框被销毁了");
}
@end

SHWindow类:

SHWindow.h:
#import <Foundation/Foundation.h>
#import "SHButton.h"
#import "SHEdit.h"

@interface SHWindow : NSObject
{
    SHButton *_button;//部分类SHButton只能作为整体类SHWindow的成员变量,不能用property定义成属性
    SHEdit *_edit;
}
@end
SHWindow.m:
#import "SHWindow.h"

@implementation SHWindow
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"窗口被创建了");
        _button = [[SHButton alloc] init];
        _edit = [[SHEdit alloc] init];
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"窗口被销毁了");
}
@end

3. 面向对象的三大特征

3.1封装与类
3.2继承与派生
3.3多态

4. 继承与派生

4.1继承是两个类之间的一种关系,是父类与子类的关系,是“is a”的关系,是讲父类中的所有属性和方法直接在子类中复用
4.2派生:在子类中添加自己的属性和方法
4.3重写:在子类中对父类继承过来的方法重新定义函数体
4.4隐藏:在子类重写父类的方法之后,用子类的对象调用该方法时,优先调用子类重写的方法

实例代码
SHAnimal类:

SHAnimal.h:
#import <Foundation/Foundation.h>

@interface SHAnimal : NSObject
@property NSString *name;
@property int age;
-(id)initWithName:(NSString*)name andAge:(int)age;
-(void)eat;
-(void)sleep;
@end
SHAnimal.m:
#import "SHAnimal.h"

@implementation SHAnimal
-(id)initWithName:(NSString *)name andAge:(int)age
{
    if (self = [super init])
    {
        self.name = name;
        self.age = age;
    }
    return self;
}
-(void)eat
{
    NSLog(@"动物%@吃", self.name);
}
-(void)sleep
{
    NSLog(@"动物%@睡", self.name);
}
@end

SHDog类:

SHDog.h:
#import "SHAnimal.h"

@interface SHDog : SHAnimal
-(void)watchHome;
@property int weight;
-(id)initWithName:(NSString *)name andAge:(int)age andWeight:(int)weight;
@end
SHDog.m:
#import "SHDog.h"

@implementation SHDog
-(id)initWithName:(NSString *)name andAge:(int)age andWeight:(int)weight
{
    if (self = [super initWithName:name andAge:age])
    {
        self.weight = weight;
    }
    return self;
}
-(void)watchHome
{
    NSLog(@"狗狗%@正在看家", self.name);
}
-(void)eat
{
    [super eat];
    NSLog(@"狗狗%@正在啃骨头", self.name);
}
@end

SHCat类:

SHCat.h:
#import "SHAnimal.h"

@interface SHCat : SHAnimal
-(void)catchMouse;

@end
SHCat.m:
#import "SHCat.h"

@implementation SHCat
-(void)catchMouse
{
    NSLog(@"猫咪%@正在抓老鼠", self.name);
}
@end

main函数:

#import <Foundation/Foundation.h>
#import "SHDog.h"
#import "SHCat.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        SHDog *dog = [[SHDog alloc] init];
        dog.name = @"旺财";
        dog.age = 3;
        dog.weight = 20;
        [dog eat];
        [dog sleep];
        [dog watchHome];

        SHCat *cat = [[SHCat alloc] init];
        cat.name = @"加菲";
        cat.age = 1;
        [cat eat];
        [cat sleep];
        [cat catchMouse];

        SHDog *dog1 = [[SHDog alloc] initWithName:@"小黑子" andAge:1 andWeight:5];
        [dog1 watchHome];
    }
    return 0;
}

5. MRC

5.1淘汰的技术
5.2RC:引用计数,是指alloc总动分配的一块存储空间,用于记录持有该空间的指针个数
5.3使用retainCount查看RC的值
5.4使用retain使RC加1
5.5使用release使RC减1
    5.5.1减1后结果为0,则先调用dealloc,然后释放RC所在的对象空间
    5.5.2结果不为0,则release直接返回
5.6retain做property的参数时,可以在set方法中自动添加持有方法,使RC加1。注意:一旦添加该参数,必须在dealloc中使RC再减1(即release)

实例代码:
SHPoint类:

SHPoint.h:
#import <Foundation/Foundation.h>

@interface SHPoint : NSObject
@property int x;
@property int y;
-(id)initWithX:(int)x andY:(int)y;
-(void)show;
@end
SHPoint.m:
#import "SHPoint.h"

@implementation SHPoint
-(id)initWithX:(int)x andY:(int)y
{
    if (self = [super init])
    {
        self.x = x;
        self.y = y;
    }
    return self;
}
-(void)show
{
    NSLog(@"(%d,%d)", self.x, self.y);
}
@end

SHCircle类:

SHCircle.h:
#import <Foundation/Foundation.h>
#import "SHPoint.h"

@interface SHCircle : NSObject
@property(retain) SHPoint *center;
@property double radius;
@end
SHCircle.m:
#import "SHCircle.h"

@implementation SHCircle
-(void)dealloc
{
    [self.center release];
    [super dealloc];//MRC必须有此句
}
@end

main函数:

#import <Foundation/Foundation.h>
#import "SHPoint.h"
#import "SHCircle.h"

void test1()
{
    SHPoint *p = [[SHPoint alloc] initWithX:10 andY:20];
    NSLog(@"%lu", [p retainCount]);
    [p show];
    [p release];
}

void test2()
{
    SHPoint *p = [[SHPoint alloc] initWithX:10 andY:20];
    NSLog(@"%lu", [p retainCount]);
    SHPoint *p1 = p;
    NSLog(@"%lu", [p retainCount]);
    [p1 retain];
    NSLog(@"%lu", [p retainCount]);
    [p1 release];
    NSLog(@"%lu", [p retainCount]);
    [p release];
    NSLog(@"%lu", [p retainCount]);
}

void test3()
{
    SHPoint *p = [[SHPoint alloc] initWithX:10 andY:20];
    SHCircle *c = [[SHCircle alloc] init];
    c.center = p;
    //[c.center retain];
    c.radius = 5;
    [p release];
    ;//...
    //[c.center release];
    [c release];
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        test2();
    }
    return 0;
}

每日一练

1、有一幢房子,它有门、窗、墙
房子作为整体类,门窗墙作为部分类,按组合方式编写
。。。按聚合方式编写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值