swift - mutating(杖举或结构体,通过方法来改变自身的值)

前端之家收集整理的这篇文章主要介绍了swift - mutating(杖举或结构体,通过方法来改变自身的值)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//更改了self的值

struct Point2 {

var x = 0.0,y = 0.0

mutating func moveByX (deltaX:Double,y deltaY:Double){

self = Point2(x:x+deltaX,y: y+deltaY)

}

}


//更改了self的值

enum TriStateSwitch {

case Off,Low,High

mutating func next(){

switch self {

case .Off:

self = Low

case .Low:

self = High

case .High:

self = Off

}

}

}


var ovenLight = TriStateSwitch.Low

ovenLight.next()

ovenLight.next()

原文链接:https://www.f2er.com/swift/323964.html

猜你在找的Swift相关文章