• 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

  • 相关阅读:
    进位制 与成熟表示
    例题 3-6 环状序列
    -------------------开启我的手残之旅---------我就是喜欢写笔记-------咋滴啦?-----
    图的遍历---------开始开始-------o(∩_∩)o 哈哈
    -----------什么是图?------------
    并查集-----集合以及计算----
    ----堆----希望这是一个容易上手的工具--------
    kafka-docker----(how to setup http proxy in container??)
    FW: Dockerfile RUN, CMD & ENTRYPOINT
    telnet --- no route to host solution "iptables -F " in the target machine
  • 原文地址:https://www.cnblogs.com/lianfu/p/5095975.html
Copyright © 2020-2023  润新知