博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C Polymorphism
阅读量:6364 次
发布时间:2019-06-23

本文共 2671 字,大约阅读时间需要 8 分钟。

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Objective-C polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Consider the example, we have a class Shape that provides the basic interface for all the shapes. Square and Rectangle are derived from the base class Shape.

We have the method printArea that is going to show about the OOP feature polymorphism.

1 #import 
2 3 @interface Shape : NSObject 4 5 { 6 CGFloat area; 7 } 8 9 - (void)printArea;10 - (void)calculateArea;11 @end12 13 @implementation Shape14 15 - (void)printArea{16 NSLog(@"The area is %f", area);17 }18 19 - (void)calculateArea{20 21 }22 23 @end24 25 26 @interface Square : Shape27 {28 CGFloat length;29 }30 31 - (id)initWithSide:(CGFloat)side;32 33 - (void)calculateArea;34 35 @end36 37 @implementation Square38 39 - (id)initWithSide:(CGFloat)side{40 length = side;41 return self;42 }43 44 - (void)calculateArea{45 area = length * length;46 }47 48 - (void)printArea{49 NSLog(@"The area of square is %f", area);50 }51 52 @end53 54 @interface Rectangle : Shape55 {56 CGFloat length;57 CGFloat breadth;58 }59 60 - (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;61 62 63 @end64 65 @implementation Rectangle66 67 - (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth{68 length = rLength;69 breadth = rBreadth;70 return self;71 }72 73 - (void)calculateArea{74 area = length * breadth;75 }76 77 @end78 79 80 int main(int argc, const char * argv[])81 {82 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];83 Shape *square = [[Square alloc]initWithSide:10.0];84 [square calculateArea];85 [square printArea];86 Shape *rect = [[Rectangle alloc]87 initWithLength:10.0 andBreadth:5.0];88 [rect calculateArea];89 [rect printArea]; 90 [pool drain];91 return 0;92 }

When the above code is compiled and executed, it produces the following result:

1 2013-09-22 21:21:50.785 Polymorphism[358:303] The area of square is 100.0000002 2013-09-22 21:21:50.786 Polymorphism[358:303] The area is 50.000000

In the above example based on the availability of the method calculateArea and printArea, either the method in the base class or the derived class executed.

Polymorphism handles the switching of methods between the base class and derived class based on the method implementation of the two classes.

转载于:https://www.cnblogs.com/xujinzhong/p/8430499.html

你可能感兴趣的文章
虎牙直播在微服务改造方面的实践和总结
查看>>
怎样将优酷网站下载的视频KUX转MP4格式
查看>>
MongoDB 分组统计
查看>>
二进制状态码
查看>>
Vue 中 CSS 动画原理
查看>>
关于 Promise 的 9 个提示
查看>>
算法复习
查看>>
安卓中高级开发面试知识点之——缓存
查看>>
Java的初始化顺序
查看>>
《css揭秘》读书笔记
查看>>
js 判断回文字符串
查看>>
shields小徽章是如何生成的?以及搭建自己的shield服务器
查看>>
猫头鹰的深夜翻译:spring事务管理
查看>>
记一次使用Spring REST Docs + travis + github自动生成API接口文档的操作步骤(下)...
查看>>
[CSS]《CSS揭秘》第四章——视觉效果
查看>>
机器学习和数据科学领域必读的10本免费书籍
查看>>
混沌工程 - 软件系统高可用、弹性化的必由之路
查看>>
一度让我头大到起飞的表单验证
查看>>
在vue项目中全局引入一个 .scss文件
查看>>
REST APIs自动化测试
查看>>