
- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 同一性運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選值
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift - fall through 語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 轉義和非轉義閉包
- Swift - 自動閉包
- Swift 面向物件程式設計
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階特性
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
Swift - 運算子過載
Swift 中的運算子過載
運算子過載是 Swift 程式設計中的一項強大技術。運算子過載允許我們使用自定義程式碼更改現有運算子(如 +、-、/、*、% 等)的工作方式。
它使程式碼更具表現力和可讀性。要過載運算子,我們必須使用“static func”關鍵字定義該運算子的行為。
語法
以下是運算子過載的語法:
static func operatorName() { // body }
示例 1
Swift 程式,過載 + 運算子以計算兩個複數的和。
import Foundation struct ComplexNumber { var real: Double var imag: Double // Overloading + operator to add two complex numbers static func+(left: ComplexNumber, right: ComplexNumber) -> ComplexNumber { return ComplexNumber(real: left.real + right.real, imag: left.imag + right.imag) } } let complexNumber1 = ComplexNumber(real: 2.1, imag: 2.0) let complexNumber2 = ComplexNumber(real: 6.1, imag: 9.0) // Calling + operator to add two complex numbers let sumOfComplexNumbers = complexNumber1 + complexNumber2 print(sumOfComplexNumbers)
輸出
ComplexNumber(real: 8.2, imag: 11.0)
示例 2
Swift 程式,過載自定義字首運算子。
import Foundation struct Car { var price: Double // Overloading the custom prefix operator "++" to increase the price of car static prefix func ++ (carPrice: inout Car) { carPrice.price += 500000.0 } } var currentPrice = Car(price: 2500000.0) // Calling the custom ++ operator to increase the car price ++currentPrice print("Updated car price is", currentPrice.price)
輸出
Updated car price is 2500000.0
Swift 中運算子過載的限制
以下是運算子過載的限制:
- 在 Swift 中,您可以過載有限的運算子,例如算術運算子和自定義運算子。
- 過載運算子時,不允許更改運算子的優先順序或結合性。
- Swift 不支援過載邏輯運算子的短路行為。
- 不要過度使用運算子過載,因為它會使您的程式碼難以閱讀和理解。
運算子函式和普通函式的區別
以下是運算子函式和普通函式的主要區別:
運算子函式 | 普通函式 |
---|---|
它們使用“static func”關鍵字和自定義運算子符號定義。 | 它們使用“func”關鍵字和函式名定義。 |
它們用於自定義運算子的行為。 | 它們用於完成通用任務。 |
當與自定義型別一起使用運算子時,它們被隱式呼叫。 | 它們透過函式名顯式呼叫。 |
它們可以定義為索引、字首或字尾形式。 | 它們只能定義為中綴形式。 |
廣告