您的位置:首页 > 移动开发 > Swift

Swift -- 方法

2016-01-03 11:50 281 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: