Swift -- 方法

class Rect{

    private struct swidth{

        static var width: Int = 0

    }

    private struct sheight{

        static var height: Int = 0

    }

    internal class var width: Int{

        get{

            return swidth.width

        }

        set{

            swidth.width = newValue

        }

    }

    internal class var height: Int{

        get{

        return sheight.height

        }

        set{

            sheight.height = newValue

        }

    }

    class func setSize(w: Int, height: Int){

        Rect.width = w

        Rect.height = height

    }

    class func getArea() -> Int{

        return Rect.width * Rect.height

    }

}

Rect.setSize(20, height: 50)

Rect.width

Rect.height

Rect.getArea()

// 闭包

class Student{

    var score: [Int] = {

        var scores: [Int] = Array()

        for m in 0...3{

            scores.append(m)

        }

        return scores

    }()

}

var stu = Student()

stu.score

Student().score

// 嵌套类型

struct BlackjackCard{

    enum Suit: String{

        case Spades = "first", Hearts = "second", Diamonds = "third", Clubs = "fourth"

    }    

    enum Rank: Int{

        case Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten

        case Jack, Queen, King, Ace

        struct Values{

            let first: Int, second: Int?

        }

        var values: Values{

            switch self{

            case .Ace: return Values(first: 1, second: 11)

            case .Jack, .Queen, .King: return Values(first: 10, second: nil)

            default: return Values(first: self.rawValue, second: nil)

            }

        }

    }

    

    let rank: Rank, suit: Suit

    var description: String{

        var output = "suit is \(suit.rawValue)"

        output += " value is \(rank.values.first)"

        if let second = rank.values.second{

            output += " or \(second)"

        }

        return output

    }

}

let theAceOfSpades = BlackjackCard(rank: .Ace, suit: .Spades)

theAceOfSpades.description

let heartsSymbol = BlackjackCard.Suit.Hearts.rawValue

posted @ 2016-01-03 11:50  以神之名  阅读(212)  评论(0编辑  收藏  举报