iOS进阶之路

OC--实现多态--继承

2019-01-07  本文已影响0人  幻影道哥

多态:多态性可以简单地概括为“一个接口,多种方法”,程序在运行时才决定调用的函数,它是面向对象编程领域的核心概念。多态(polymorphism),字面意思多种形态.

OC的实现原理:不同的SubClass通过重写SubClass的方法来实现不同的需求,然后调用SuperClass的方法把SubClass处理结果以参数的形式传递给SuperClass.

ps:高级使用

最近由于项目架构需求,研究下了指针指向.发现父类的指针可以指向子类. SubClass的参数可以是个model类型.不同的结果通过model的子类实现.

实现代码:

1.再SuperClass.h上面声明方法.

@interface SuperClass : NSObject
//调用结果
- (void)showSubData;
//子类处理完毕后回调此方法
- (void)subComeBack:(NSString *)callBack;
//高级使用返回方法
- (void)ObjectsubComeBack:(Model*)result;
@end
/************************声明子类实现的方法***********************/
@interface SuperClass (subclasing)

//不同的子类重写此方法,处理不同的事件. 处理完调用父类 subComeBack的方法返回处理结果
- (void)sendSuperData:(NSString *)str;

@end

2.再SuperClass.m实现方法


@implementation SuperClass


- (void)showSubData
{
    NSString *str = @"superData";
    [self sendSuperData:str]; //调用子类方法,如果有子类,就会调用子类的方法,因为接口部分已经跟定了这个是子类实现的,
}

/**
 常规回调

 @param callBack 子类返回值 不同的子类返回值不同
 */
- (void)subComeBack:(NSString *)callBack
{
    NSLog(@"回调参数-%@",callBack);
}

/*****
 高级回调
 ***/
- (void)ObjectsubComeBack:(Model*)result {
    if (result.type == LWtype) {
        thowClass *call = (thowClass*)result;
        NSLog(@"%@---%@",call.name,call.eat);
    }else {
        threeClass *call = (threeClass*)result;
        NSLog(@"%@---%@",call.counry,call.helloword);
    }
    
    NSLog(@"%@", [NSString stringWithFormat: @"返回参数%@",NSStringFromClass([result class])]);
}

//子类没有重写此方法时,默认调用父类的实现
- (void)sendSuperData:(NSString *)str
{
    NSLog(@"子类没实现,运行这个方法~");
    [self subComeBack:@"父类默认实现"];
}

3.SubClass.h声明

@interface SubClass : SuperClass

@end
//nextClass是SuperClass的另一种实现
@interface nextClass : SuperClass

@end

4.SubClass.m实现

@implementation SubClass

//重写父类方法
- (void)sendSuperData:(NSString *)str
{
    NSLog(@"父类传递过来的 = %@",str);
    //调用父类方法
    [super subComeBack:[NSString stringWithFormat:@"多态%@子类返回处理结果回 处理结果 老王翻墙",NSStringFromClass([nextClass class])]];
    
    //高级使用
    thowClass *cass = [[thowClass alloc] init];
    cass.type = LWtype;
    [cass setValue:@"老王" forKey:@"name"];
    [cass setValue:@"偷吃" forKey:@"eat"];
    [super ObjectsubComeBack:cass];
}


@end

@implementation nextClass
//重写父类方法
- (void)sendSuperData:(NSString *)str
{
    NSLog(@"父类传递过来的 = %@",str);
    //调用父类方法
    [super subComeBack:[NSString stringWithFormat:@"多态%@子类返回处理结果回 处理结果 老宋翻墙",NSStringFromClass([nextClass class])]];
    
    //高级使用
    threeClass *cass = [[threeClass alloc] init];
    cass.type = LStype;
    [cass setValue:@"女儿国" forKey:@"counry"];
    [cass setValue:@"真好" forKey:@"helloword"];
    [super ObjectsubComeBack:cass];
}

@end

5.高级使用Model.h

@class thowClass;
@class thowClass;

typedef NS_ENUM(NSInteger,OverTheWall){
    LWtype = 1, //老王类型
    LStype = 2, //老宋类型
};

@interface Model : NSObject
@property(nonatomic,copy)NSString *hello;
@property(nonatomic,copy)NSString *word;
@property (nonatomic,assign)OverTheWall type;//翻墙类型
@end
/***********************************高级实现*********************************/
@interface thowClass : Model
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *eat;
@end


@interface threeClass : Model
@property(nonatomic,copy)NSString *counry;
@property(nonatomic,copy)NSString *helloword;
@end

6.高级使用Model.m

@implementation Model

@end

@implementation thowClass

@end

@implementation threeClass

@end
上一篇 下一篇

猜你喜欢

热点阅读