• Swift -- 方法


    class Rect{

        private struct swidth{

            static var Int = 0

        }

        private struct sheight{

            static var height: Int = 0

        }

        internal class var 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

  • 相关阅读:
    洛谷P3811题解
    洛谷P3353在你窗外闪耀的星星-题解
    Map根据value来排序
    java8 groupby count
    Java反射
    maven profile环境切换
    获取nginx代理情况下的真实ip
    获取request里header的name和value
    git 删除iml文件
    java list 排序
  • 原文地址:https://www.cnblogs.com/lianfu/p/5095975.html
Copyright © 2020-2023  润新知