Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

swift 单例的实现

$
0
0

定义Game类,代码如下:

public class Game {

    public var score = 0

    public static let dafaultGame = Game()

    private init(){

    }

    public func addscore(){

        score += 10
    }
}

1、将init方法设置成private
2、定义静态变量
3、赋值给静态变量调用init方法

调用

let game1 = Game.dafaultGame
game1.addscore()
game1.score

let game2 = Game.dafaultGame
game2.addscore()
game2.score
作者:u012903898 发表于2017/5/31 16:14:58 原文链接
阅读:64 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>