zoukankan      html  css  js  c++  java
  • objective-c中的继承和重载

      昨天在论坛上看到,很多人使用着面向对象的语言,却用面向过程的写法,面向对象的三大特征------封装 继承 多态,今天就来温习一下继承.

      

      继承,是子类将继承父类所有的变量和方法,省去了在子类中重复写相同的代码,提高了代码的简洁,同时易于修改.

      在继承中重载时,要保证子类和父类中的方法名称一样,并且相同的返回类型,参数的个数与重载的方法相同

     

    示例代码:

    ClassA.h

    @interface ClassA : NSObject
    {
        int x;
    }
    -(void)input;
    
    @end

    ClassA.m

    @implementation ClassA
    
    -(void)input{
        x = 100;
        NSLog(@"ClassA中输入:%d",x);
    }
    
    @end

    ClassB.h

    @interface ClassB : ClassA
    
    -(void)input;//重载
    
    -(void)printf;
    
    @end

    ClassB.m

    @implementation ClassB
    
    -(void)input{
    //    [super input];//调用父类的方法
        x = 200;//ClassA中的方法进行重写
    }
    
    -(void)printf{
        NSLog(@"ClassB中输出:%d",x);
    }
    
    @end

    main.m

    #import <Foundation/Foundation.h>
    #import "ClassB.h"
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            // insert code here...
            NSLog(@"Hello, World!");
            
            ClassB *b = [[ClassB alloc] init];
            [b input];
            [b printf];
            
        }
        return 0;
    }
  • 相关阅读:
    opendressinghash //use resize array
    ChainingHash
    Hash function
    stack && queue
    random_select
    counting sort
    master theorem
    各排序算法及其比较
    视图中添加主键的方法
    oracle表空间的扩展
  • 原文地址:https://www.cnblogs.com/mo-shou/p/3497163.html
Copyright © 2011-2022 走看看