zoukankan      html  css  js  c++  java
  • Objective-C Polymorphism

    #import <Foundation/Foundation.h>
    
    @interface Shape : NSObject 
    {
      CGFloat area;
    }
    -(void)printArea;
    -(void)calculateArea;
    @end
    
    @implementation Shape
    
    -(void)printArea {
     NSLog(@"The area is %f",area);
    }
    
    -(void)calculateArea {
    }
    
    @end
    
    
    @interface Square : Shape
    {
      CGFloat length;
    }
    
    -(id)initWithSide : (CGFloat)side;
    -(void)calculateArea;
    
    @end
    
    
    @implementation Square 
    
    -(id)initWithSide : (CGFloat)side {
     length = side;
     return self;
    }
    
    -(void)calculateArea {
     area = length * length;
    }
    
    -(void)printArea {
     NSLog(@"The area of square is %f", area);
    }
    
    @end
    
    
    @interface Rectangle : Shape 
    {
     CGFloat length;
     CGFloat breadth;
    }
    -(id)initWithLength: (CGFloat)rLen andBreadth:(CGFloat)rBreadth;
    
    @end
    
    
    @implementation Rectangle
    
    -(id)initWithLength: (CGFloat)rLen andBreadth:(CGFloat)rBreadth {
     length = rLen;
     breadth = rBreadth;
     return self;
    }
    
    -(void)calculateArea {
     area = length * breadth;
    }
    
    -(void)printArea {
     NSLog(@"The area of Rectangle is %f", area);
    }
    
    @end
    
    
    int main(int ar, const char * argv[])
    {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
      Shape *square = [[Square alloc]initWithSide:4.2];
      [square calculateArea];
      [square printArea];
      Shape *rect = [[Rectangle alloc]initWithLength:7.88 andBreadth:6.35];
      [rect calculateArea];
      [rect printArea];
      
      [pool drain];
      return 0;
      
    }
  • 相关阅读:
    Solr基础知识二(导入数据)
    Solr基础知识一(安装配置)
    企业微信机器人
    Mysql+Keepalived双主热备高可用操作记录
    mysql互为主从(双主)配置
    编译安装msyql
    JS 常用的一些功能性函数 (自用)
    JavaScrip 之 DOM (回顾)
    MySQL的表定义语法
    MySQL的数据库定义语法
  • 原文地址:https://www.cnblogs.com/silva/p/5227815.html
Copyright © 2011-2022 走看看