OC-自动释放池、多态、协议

来源:互联网 发布:br是什么软件 编辑:程序博客网 时间:2024/05/16 14:15

1. 自动释放池

1.1在ARC中是已经过时的技术1.2是一个(对象的集合)1.3当自动释放池的作用域结束时,池中的所有对象自动被释放1.4自动释放池与工厂方法1.5自动释放池的嵌套

实例代码:
SHPoint类:

SHPoint.h#import <Foundation/Foundation.h>@interface SHPoint : NSObject@property int x;@property int y;-(id)initWithX:(int)x andY:(int)y;+(id)pointWithX:(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;}+(id)pointWithX:(int)x andY:(int)y{    __autoreleasing SHPoint *p = [[SHPoint alloc] initWithX:x andY:y];    return p;}-(void)show{    NSLog(@"(%d,%d)", self.x, self.y);}-(void)dealloc{    NSLog(@"点(%d,%d)被释放了", self.x, self.y);}@end

main函数:

#import <Foundation/Foundation.h>#import "SHPoint.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        {            __autoreleasing SHPoint *p = [[SHPoint alloc] initWithX:10 andY:20];            [p show];        }        NSLog(@"------");    }    return 0;}

实例运行结果:
2016-08-25 08:31:51.842 day17_02[557:16665] (10,20)
2016-08-25 08:31:51.843 day17_02[557:16665] ——
2016-08-25 08:31:51.843 day17_02[557:16665] 点(10,20)被释放了

2. 多态

2.1一个对外接口,多个内在实现2.2在OC程序中,体现为父类的指针可以指向子类的对象,从而调用子类中 重写的父类方法2.3使用形式    2.3.1函数参数    2.3.2数组    2.3.3返回值

实例代码:
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类,继承Animal类:

SHDog.h:#import "SHAnimal.h"@interface SHDog : SHAnimal-(void)watchHome;@end
SHDog.m:#import "SHDog.h"@implementation SHDog-(void)watchHome{    NSLog(@"狗狗%@正在看家", self.name);}-(void)eat{    NSLog(@"狗狗%@正在啃骨头", self.name);}@end

SHCat类,继承Animal类:

SHCat:#import "SHAnimal.h"@interface SHCat : SHAnimal-(void)catchMouse;@end
SHCat.m:#import "SHCat.h"@implementation SHCat-(void)catchMouse{    NSLog(@"猫咪%@正在抓老鼠", self.name);}-(void)eat{    NSLog(@"猫咪%@正在吃鱼", self.name);}@end

main函数:

#import <Foundation/Foundation.h>#import "SHDog.h"#import "SHCat.h"//多态概念void test1(){    SHAnimal *a = [[SHDog alloc] initWithName:@"小黑子" andAge:3];    [a eat];//调用子类中重写的方法    [a sleep];    //[a watchHome];//父类的指针不能调用子类中派生的方法    a = [[SHCat alloc] initWithName:@"老咪" andAge:1];    [a eat];}//参数多态void show(SHAnimal *a){    [a eat];}void test2(){    SHDog *dog = [[SHDog alloc] initWithName:@"小黑子" andAge:3];    show(dog);    SHCat *cat = [[SHCat alloc] initWithName:@"老咪" andAge:1];    show(cat);}//数组多态void test3(){    SHAnimal *a[3];    a[0] = [[SHAnimal alloc] initWithName:@"无名氏" andAge:0];    a[1] = [[SHDog alloc] initWithName:@"小黑子" andAge:3];    a[2] = [[SHCat alloc] initWithName:@"老咪" andAge:1];    for (int i = 0; i < 3; i++)    {        [a[i] eat];    }}//返回值多态typedef enum{    ANIMAL, DOG, CAT,}Type;SHAnimal* get(Type type){    switch (type) {        case ANIMAL:            return [[SHAnimal alloc] initWithName:@"无名氏" andAge:0];        case DOG:            return [[SHDog alloc] initWithName:@"小黑子" andAge:3];        case CAT:            return [[SHCat alloc] initWithName:@"老咪" andAge:1];    }}void test4(){    [get(DOG) eat];    [get(CAT) eat];}int main(int argc, const char * argv[]) {    @autoreleasepool {        test4();    }    return 0;}

3. 协议(另一种形式的多态)

3.1是一种要求,或是一种规则3.2对应程序来讲,是只声明函数名,不实现3.3协议必须被某个类采纳,且在该类中给出协议中方法的函数体3.4对于采纳协议的类,可以和其他类一样使用3.5协议可以被继承,包括多个父协议3.6协议可以多重采纳3.7使用形式    3.7.1数组    3.7.2参数    3.7.3返回值

实例代码:
创建一个SHMyProtocol协议:

SHMyProtocol.h#import <Foundation/Foundation.h>@protocol SHMyProtocol <NSObject>@property NSString *content;-(void)show;@end

创一个SHProtocol1协议:

#import <Foundation/Foundation.h>@protocol SHProtocol1 <NSObject>@optional-(void)method0;@required//默认的-(void)method1;@end

创建SHProtocol2…5协议

协议中有method2...5的方法

SHMyClass类:

SHMyClass.h:#import <Foundation/Foundation.h>#import "SHMyProtocol.h"@interface SHMyClass : NSObject<SHMyProtocol>//采纳协议@end
SHMyClass.m:#import "SHMyClass.h"@implementation SHMyClass@synthesize content = _content;-(void)show{    NSLog(@"秀一下,%@", self.content);}@end

main函数:

#import <Foundation/Foundation.h>#import "SHMyClass.h"#import "SHClassA.h"#import "SHClassB.h"#import "SHClassC.h"void test1(){    SHMyClass *obj = [[SHMyClass alloc] init];    obj.content = @"协议中的属性";    [obj show];    id<SHMyProtocol> id1 = obj;    id1.content = @"万能指针使用属性";    [id1 show];}void test2(){    SHClassA *obj = [[SHClassA alloc] init];    id<SHProtocol1> id1 = obj;    //[id1 method];//id1只能调用协议中的方法,不能调用类中自定义的方法    [id1 method0];    [id1 method1];    //[id1 method2];//不能调用SHProtocol2中的方法    id<SHProtocol2> id2 = obj;    //[id2 method];    [id2 method0];    [id2 method1];    [id2 method2];}void test3(){    SHClassB *obj = [[SHClassB alloc] init];    id<SHProtocol1> id1 = obj;    [id1 method0];    [id1 method1];    id<SHProtocol2> id2 = obj;    [id2 method0];    [id2 method1];    [id2 method2];    id<SHProtocol3> id3 = obj;    [id3 method3];    id<SHProtocol4> id4 = obj;    [id4 method0];    [id4 method1];    [id4 method2];    [id4 method3];    [id4 method4];}void test4(){    SHClassC *obj = [[SHClassC alloc] init];    id<SHProtocol4, SHProtocol5> id1 = obj;    [id1 method0];    [id1 method1];    [id1 method2];    [id1 method3];    [id1 method4];    [id1 method5];}//数组协议多态void test5(){    id<SHProtocol2> b[2];    b[0] = [[SHClassA alloc] init];    b[1] = [[SHClassB alloc] init];    for (int i = 0; i < 2; i++)    {        [b[i] method1];    }}//参数协议多态void fun(id<SHProtocol2> a){    [a method1];}void test6(){    SHClassA *objA = [[SHClassA alloc] init];    fun(objA);    SHClassB *objB = [[SHClassB alloc] init];    fun(objB);}//返回值协议多态typedef enum{    CLASSA, CLASSB, CLASSC,}Type;id<SHProtocol2> get(Type type){    switch (type) {        case CLASSA:            return [[SHClassA alloc] init];        case CLASSB:            return [[SHClassB alloc] init];        case CLASSC:            return [[SHClassC alloc] init];    }}void test7(){    [get(CLASSA) method0];    [get(CLASSB) method0];}int main(int argc, const char * argv[]) {    @autoreleasepool {        test7();    }    return 0;}

思考练习

创建一个SHTransportation类,类中有一个方法叫print的 方法,该方法默认输出 “显示交通工具信息”,这个类作为父 类,派生出三个子类SHTaxi的士类、SHBus巴士类和SHBike 自行车类。SHTaxi的士类覆盖了父类的print方法,改成自己 的输出,”交通工具为的士”;SHBus巴士类覆盖了父类的 print方法,改成自己的输出,”交通工具为巴士”;SHBike 自 行车类覆盖了父类的print方法,改成自己的输出,”交通工具 为单车”。
在主程序中,创建三个交通工具,使用多态输出交通工具信息。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析:
SHTransportation类:

SHTransportation.h:#import <Foundation/Foundation.h>@interface SHTransportation : NSObject-(void)print;@end
SHTransportation.m:#import "SHTransportation.h"@implementation SHTransportation-(void)print{    NSLog(@"显示交通工具信息");}@end

SHTaxi类:

SHTaxi.h:#import "SHTransportation.h"@interface SHTaxi : SHTransportation@end
SHTaxi.m:#import "SHTaxi.h"@implementation SHTaxi-(void)print{    NSLog(@"交通工具为的士");}@end

SHBus类:

SHBus.h:#import "SHTransportation.h"@interface SHBus : SHTransportation@end
SHBus.m:#import "SHBus.h"@implementation SHBus-(void)print{    NSLog(@"交通工具为巴士");}@end

SHBike类:

SHBike.h:#import "SHTransportation.h"@interface SHBike : SHTransportation@end
SHBike.m:#import "SHBike.h"@implementation SHBike-(void)print{    NSLog(@"交通工具为单车");}@end

main函数:

#import <Foundation/Foundation.h>#import "SHTaxi.h"#import "SHBus.h"#import "SHBike.h"void show(SHTransportation *t){    [t print];}int main(int argc, const char * argv[]) {    @autoreleasepool {        show([[SHTaxi alloc] init]);        show([[SHBus alloc] init]);        show([[SHBike alloc] init]);    }    return 0;}

原创博文未经允许禁止转载,否则将追究法律责任

0 0
原创粉丝点击