Classes, structures, and enumerations can all define instance methods懒构;
Classes, structures, and enumerations can also define type methods。 In Objective-C, classes are the only types that can define methods鸣剪,In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.
Instance Methods ? 實(shí)例方法
Class
class Counter{
? ? ? ? ?var count=0
? ? ? ? ?func increment() {
? ? ? ? ? ? ? ? ?count+=1
? ? ? ? ?}
? ? ? ? ?func increment(by amount:Int) {
? ? ? ? ? ? ? ? ?count +=amount
? ? ? ? ?}
? ? ? ? ?func reset() {
? ? ? ? ? ? ? ? ?count=0
? ? ? ? ?}
}
let counter=Counter() ??// the initial counter value is 0
counter.increment() ? ??// the counter's value is now 1
counter.increment(by:5) ? ??// the counter's value is now 6
counter.reset() ? ??// the counter's value is now 0
Struct
struct Point{
? ? ? ? ? var x=0.0,y=0.0
? ? ? ? ? func isToTheRightOf(x:Double) ->Bool{
? ? ? ? ? ? ? ? ? return self.x>x
? ? ? ? ? }
}
let somePoint=Point(x:4.0,y:5.0)
if somePoint.isToTheRightOf(x:1.0) ?{
? ? ? ?print("This point is to the right of the line where x == 1.0")
}
// Prints "This point is to the right of the line where x == 1.0"
Modifying Value Types from Within Instance Methods ?從實(shí)例方法中修改值類型
struct Point{
? ? ? ? ? var x=0.0,y=0.0
? ? ? ? ? mutating func moveBy(x deltaX:Double,y deltaY:Double) { //mutating關(guān)鍵字被添加到其定義徐钠,以使其能夠修改其屬性。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?x +=deltaX
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?y +=deltaY
? ? ? ? ? }
}
var somePoint=Point(x:1.0,y:1.0)
somePoint.moveBy(x:2.0,y:3.0) // 改變實(shí)例屬性
print("The point is now at (\(somePoint.x),\(somePoint.y))")?
// Prints "The point is now at (3.0, 4.0)"
let fixedPoint=Point(x:3.0,y:3.0)
fixedPoint.moveBy(x:2.0,y:3.0)
// this will report an error 因?yàn)閒ixedPoint是個(gè)常量無(wú)法改變其實(shí)例屬性
Assigning to self Within a Mutating Method ?重新改變自己
struct Point{
? ? ? ? ? var x=0.0,y=0.0
? ? ? ? ? mutating func moveBy(x deltaX:Double,y deltaY:Double) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? self=Point(x:x+deltaX,y:y+deltaY) //實(shí)例對(duì)象調(diào)用實(shí)例方法改變對(duì)象本身
? ? ? ? ? }
}
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 is now equal to .high
ovenLight.next() ? ? ??// ovenLight is now equal to .off
Type Methods ? 類方法
class SomeClass{
? ? ? ? ? class func someTypeMethod() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? // type method implementation goes here
? ? ? ? ? }
}
SomeClass.someTypeMethod()
struct LevelTracker{
? ? ? ? ? static var highestUnlockedLevel=1
? ? ? ? ? var currentLevel=1
? ? ? ? ? static func unlock( _ level:Int) { //類方法? 操作的是類屬性highestUnlockedLevel
? ? ? ? ? ? ? ? ? ? ? ? ? ? if level>highestUnlockedLevel {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?highestUnlockedLevel=level
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? static func isUnlocked( _ level:Int)? -> Bool{ // 類方法 操作的是類屬性highestUnlockedLevel
? ? ? ? ? ? ? ? ? ? ? ? ? ? return level <= highestUnlockedLevel
? ? ? ? ? }
? ? ? ? ? @discardableResult ?//忽略返回值的代碼不一定是錯(cuò)誤
? ? ? ? ? mutating func advance(to level:Int) ->Bool{ // 可修改的實(shí)例方法 ? 操作的是實(shí)例屬性
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if LevelTracker.isUnlocked(level) ?{ // 調(diào)用類方法
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?currentLevel=level
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return true
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return false
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? }
}
class Player{
? ? ? ? ?var tracker = LevelTracker()
? ? ? ? ?let playerName:String
? ? ? ? ?func complete(level:Int) {
? ? ? ? ? ? ? ? ?LevelTracker.unlock(level+1) // 調(diào)用類方法
? ? ? ? ? ? ? ? ?tracker.advance(to:level+1) // 調(diào)用實(shí)例方法
? ? ? ? ?}
? ? ? ? ?init(name:String) { //屬性構(gòu)造器
? ? ? ? ? ? ? ? playerName=name
? ? ? ? ?}
}
var player=Player(name:"Argyrios")
player.complete(level:1)
print("highest unlocked level is now\(LevelTracker.highestUnlockedLevel)")
// Prints "highest unlocked level is now 2"
player=Player(name:"Beto")
if player.tracker.advance(to:6) ?{
? ? ? ? print("player is now on level 6")
}else{
? ? ? ? print("level 6 has not yet been unlocked")
}
// Prints "level 6 has not yet been unlocked"