Objective-C Polymorphism

来源:https://www.cnblogs.com/xujinzhong/archive/2018/02/07/8427814.html
-Advertisement-
Play Games

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


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 <Foundation/Foundation.h>
 2 
 3 @interface Shape : NSObject
 4 
 5 {
 6     CGFloat area;
 7 }
 8 
 9 - (void)printArea;
10 - (void)calculateArea;
11 @end
12 
13 @implementation Shape
14 
15 - (void)printArea{
16     NSLog(@"The area is %f", area);
17 }
18 
19 - (void)calculateArea{
20 
21 }
22 
23 @end
24 
25 
26 @interface Square : Shape
27 {
28     CGFloat length;
29 }
30 
31 - (id)initWithSide:(CGFloat)side;
32 
33 - (void)calculateArea;
34 
35 @end
36 
37 @implementation Square
38 
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 @end
53 
54 @interface Rectangle : Shape
55 {
56     CGFloat length;
57     CGFloat breadth;
58 }
59 
60 - (id)initWithLength:(CGFloat)rLength andBreadth:(CGFloat)rBreadth;
61 
62 
63 @end
64 
65 @implementation Rectangle
66 
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 @end
78 
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.000000
2 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.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 出處: https://www.cnblogs.com/easypass/archive/2010/12/ 08/1900127.html 1.資料庫訪問優化法則 要正確的優化SQL,我們需要快速定位能性的瓶頸點,也就是說快速找到我們SQL主要的開銷在哪裡?而大多數情況性能最慢的設備會是瓶頸點,如下 ...
  • 目錄: 資料庫的基本操作 創建、刪除用戶及授權 資料庫字元校對集 創建、刪除資料庫和表 DML操作 DDL操作 索引 事務 一、資料庫的基本操作 二、創建、刪除用戶及授權 三、資料庫字元校對集 字元校對集,即排序規則,在某個字元集的情況下,字元集的排列順序應該是什麼,稱之為校對集。 四、創建、刪除數 ...
  • 連接本地mysql mysql -u root -p 連接遠程mysql mysql -u root -p -h 192.168.1.2 導出本地資料庫某張表(比如導出數據中的USERS表) 回車後要輸入資料庫密碼 mysqldump databases -u root -p --tables US ...
  • 學習目標 啟動和停止Oracle DB和組件 使用Oracle Enterprise Manager 使用SQL*Plus訪問資料庫 修改資料庫初始化參數 描述資料庫啟動階段 描述資料庫關閉選項 查看預警日誌 訪問動態性能視圖 管理框架 Oracle Database 11g發行版2管理框架組件包括 ...
  • 在測試SQL Server 2016 Always On時,在創建偵聽器後,在客戶端使用SSMS, 可以用偵聽器名稱訪問Always On集群,但是使用偵聽器IP訪問時遇到"The target principal name is incorrect. Cannot generate SSPI co... ...
  • 昨天在QQ群里討論一個SQL優化的問題,語句大致如下: 於是手動測試,環境採用Oracle自帶的scott用戶下的emp表。 1.首先查看如下語句的執行計劃(此時表只有主鍵索引): 2.添加IX_TEST(deptno,comm)後查看執行計劃: 發現依然是全表掃描。 3.為deptno列添加非空約 ...
  • id是泛類型,可以用來存放各種類型的對象,使用id也就是使用“動態類型”。 動態類型,就是指,對象實際使用的是哪一個類是在執行期間確定的,而非在編譯期間。 雖然id類型可以定義任何類型的對象,但是不要濫用,如果能夠確定對象數據類型的時候,要使用“靜態類型”,“靜態類型”是在編譯階段檢查錯誤,而不是在 ...
  • l 代表滑動後當前ScrollView可視界面的左上角在整個ScrollView的X軸中的位置,oldl 也就是滑動前的X軸位置。 t 代表滑動後當前ScrollView可視界面的左上角在整個ScrollView的Y軸上的位置,old t也就是移動前的Y軸位置。 說這麼多還不如列印一遍log來得直觀 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...