码迷,mamicode.com
首页 > 编程语言 > 详细

swift5.x 运算符重载

时间:2020-05-25 09:27:52      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:size   cal   default   一个   first   init   out   控制器   super   

//
//  ViewController.swift
//  swiftT
//
//  Created by wjwdive on 2019/1/9.
//  Copyright ? 2019年 wjwdive. All rights reserved.
//

import UIKit

struct Size {
    var width = 0.0, height = 0.0
        init() {
            self.init(width: 2.0, height: 2.0)
        }
    init(width: Double, height: Double){
        self.width = width
        self.height = height
    }
}

class Car {
    var speed:Double
    init(speed:Double) {
        self.speed = speed
    }
    convenience init() {
        self.init(speed: 60.0)
    }
}

class Bus: Car {
    var wheels : Int
    init(wheels: Int) {
        self.wheels = wheels
        super.init(speed: 120.0)
    }
    convenience init() {
        self.init(wheels: 6)
    }
}

struct Vector2D {
    var x = 0.0, y = 0.0
}

//声明 自定义的运算符
prefix operator +++
// x 相加,y相减
infix operator +- : AdditionPrecedence
// x平方, y 平方和  按照乘法优先级
//infix operator *^ : MultiplicationPrecedence
//自定义优先级
infix operator *^ : MyPrecendernce

//运算符优先级 设置
precedencegroup MyPrecendernce {
    associativity: left
    lowerThan: AdditionPrecedence
}

extension Vector2D : Equatable{
    //加法运算符重载
    static func + (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y + right.y)
    }
    //一元运算符重载 在z声明运算符函数的时候在关键字之前执行prefix和postfix限定符
    static prefix func - (vector: Vector2D) -> Vector2D {
        return Vector2D(x: -vector.x, y: -vector.y)
    }
    
    //组合运算符的重载, 需要把运算符的左参数设置成 inout 类型,因为这个参数的值会在运算符函数内直接被修改
    static func += (left: inout Vector2D, right: Vector2D) {
//        left = left + right
        left = Vector2D(x: left.x + right.x, y: left.y + right.y)
    }
    
    //等价运算符重载 == ,!=
    //自定义的类和结构图不接受等价运算符的默认实现,所以想要使用的话需要重载并且遵循标准库的Equatable 协议
    static func == (left: Vector2D, right: Vector2D) -> Bool {
        return (left.x == right.x) && (left.y == right.y)
    }
    
    static func != (left: Vector2D, right: Vector2D) -> Bool {
        return !(left.x == right.x) && (left.y == right.y)
    }
    
    
    // 自定义运算符 声明和实现自定义运算符s需要声明是 前置,中置, 后置 (prefix infix postfix)
    //例如 定义一个+++ 双倍运算符
    static prefix func +++ (vector: Vector2D) -> Vector2D {
        return Vector2D(x: vector.x + vector.x, y: vector.y + vector.y)
    }
    
    //可以自定义运算符的 优先级和结核性
    //声明一个 x相加+ y相减- 的操作符
    static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x + right.x, y: left.y - right.y)
    }
    
    //自定义平方和 *& 操作符(x 相乘, y 平方和)
    static func *^ (left: Vector2D, right: Vector2D) -> Vector2D {
        return Vector2D(x: left.x * right.x, y: left.y * right.y + left.y * right.y )
    }
    
}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        var size = Size()
//        print(size.width, size.height)
//        var size2 = Size()
//        print(size2.width, size2.height)
        self.title = "控制器"
        let a: String? = "dd"
        if let b = a {
            print(type(of: a), type(of: b))
        }
        let c = a ?? "default"
        print(c, type(of: c))
        let d = "ddd"
        print("d: \(d)")
        
        var str1: String? = "str1"
        var str2: String? = "str2"
        if let acturalStr1 = str1,let acturalStr2 = str2 {
            let count1 = acturalStr1.count, count2 = acturalStr2.count
            print(count1, count2)
        }
        
        while let acturalStr1 = str1, let acturalStr2 = str2 {
            let count1 = acturalStr1.count, count2 = acturalStr2.count
            print("22", count1, count2)
            break
        }
        
        //可选项的隐式展开, swift的初始化类中
        var strr: String! = "str!"
        let count = strr.count
        print("str! count", count)
        
        
        var strOp: Optional<String> = "str optional"
        if let actStrOp = strOp {
            let count = actStrOp.count
            print(count)
        }
        
        print(1.description)
        
        //运算符+ 重载
        let vector = Vector2D(x: 3.0, y: 1.0)
        let anotherVector = Vector2D(x: 2.0, y: 4.0)
        let combinedVector = vector + anotherVector;
        print(combinedVector)//Vector2D(x: 5.0, y: 5.0)
        
        //运算符- 重载
        let positiveVector = Vector2D(x: 3.0, y: 4.0)
        let negativeVector = -positiveVector
        print(negativeVector)//Vector2D(x: -3.0, y: -4.0)
        
        //运算符 += 重载
        var origional = Vector2D(x: 1.0, y: 2.0)
        let vectorToAdd = Vector2D(x: 3.0, y: 4.0)
        origional += vectorToAdd
        print("+= 重载 : ", origional)//+= 重载 :  Vector2D(x: 4.0, y: 6.0)
        
        let v1 = Vector2D(x: 1, y: 2)
        let v2 = Vector2D(x: 1, y: 2)
        print("== 重载 : ", v1 == v2)//== 重载 :  true
        
        let vectorTobeDoubed = Vector2D(x: 1.0, y: 2.0)
        let doubleVector = +++vectorTobeDoubed
        print("+++ 自定义重载", doubleVector)//+++ 自定义重载 Vector2D(x: 2.0, y: 4.0)

        let firstV = Vector2D(x: 1.0, y: 3.0)
        let secondV = Vector2D(x: 1.0, y: 1.0)
        let result = firstV +- secondV
        print("重载 +- : ", result)//重载 +- :  Vector2D(x: 2.0, y: 2.0)

        
        let thirdV = Vector2D(x: 1.0, y: 2.0)
        let result11 = firstV *^ secondV
        print("重载 *^ : ", result11)//重载 *^ :  Vector2D(x: 1.0, y: 6.0)

        //自定义了优先级,会先算 +-, 后算 *^
        let result12 = firstV +- secondV *^ thirdV
        print("优先级结合 +- *^ ", result12)//优先级结合 +- *^  Vector2D(x: 2.0, y: 8.0)

        
    }

}





swift5.x 运算符重载

标签:size   cal   default   一个   first   init   out   控制器   super   

原文地址:https://www.cnblogs.com/wjw-blog/p/12915303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!