zoukankan      html  css  js  c++  java
  • iOS多态 动态绑定

    多态和动态绑定是为了解决父类调用子类的问题   

      首先,声明三个类aa bb cc 都继承于fist类

    #import <Foundation/Foundation.h>

    @interface fist : NSObject

    {

        SEL name;

    }

    -(void)print;

    @end

    @implementation fist

    -(void)print

    {

        NSLog(@"我是aa");

    }

    @end

     

    @interface aa : fist

    -(void)nihao;

    @end

    @implementation aa

     

    -(void)nihao

    {

        NSLog(@"buhdddddddddao");

    }

     

    @end

     

     

    @interface bb : fist

    -(void)nihao;

    @end

    @implementation bb

     

    -(void)nihao

    {

        NSLog(@"我是bb");

    }

     

    @end

    @interface cc : aa

    -(void)nihao;

    @end

    @implementation cc

     

    -(void)nihao

    {

        NSLog(@"我是cc");

    }

     

    @end

     

     

     

    int main(int argc, const char * argv[])

    {

     

        @autoreleasepool {

            

            aa *a=[[aa alloc] init];//声明aa的对象

            bb *b=[[bb alloc] init]; //'''

            cc *c=[[cc alloc] init];

                    

            

            fist *dd[3]={a,b,c};//可以用父类  id 或者NSobject

      for (int i=0; i<3; i++) {

                 SEL gong;

                gong=@selector(nihao);//定一个选择器

                if ([dd[i] respondsToSelector:gong]==YES) {//逐个遍历出来

                    [dd[i] performSelector:gong];

                    [dd[i] release];

         }

                }

                     

            }

     

            

        }

        return 0;

    }

  • 相关阅读:
    基本目标与达成方法
    终于搞定在VS2010中将CString转换为const char*
    【HBase学习之一】HBase简介
    Origin2017画分组柱状图
    映射是什么?函数是什么?映射与函数的关系?
    PPT一次性禁用所有动画效果
    跨模态检索技术调研
    卷积核与特征提取
    深入理解卷积层,全连接层的作用意义
    cbow 与 skip-gram的比较
  • 原文地址:https://www.cnblogs.com/flyingdreaming/p/polymorphic.html
Copyright © 2011-2022 走看看