美文网首页
Swift语法备忘录4-Objects and Classes

Swift语法备忘录4-Objects and Classes

作者: 叶子扬 | 来源:发表于2018-01-07 13:19 被阅读0次

    Swift Tour

    声明类

    使用关键字class声明一个类,除非在类的上下文里,属性的声明也跟恒量或者变量一样。函数和方法也是酱紫

    示例:

    声明一个Shape类,里面一个属性numberOfSides,一个方法simpleDescription。
    使用点语法访问实例的属性和方法

    class Shape {
        var numberOfSides = 0
        func simpleDescription() -> String {
            return "A shape with \(numberOfSides) sides."
        }
    }
    
    var shape = Shape()
    shape.numberOfSides = 7
    var shapeDescription = shape.simpleDescription()
    

    init方法

    上面的代码缺少一个重要的设置,init方法,添加后如下

    class NamedShape {
        var numberOfSides: Int = 0
        var name: String
    
        init(name: String) {
           self.name = name
        }
    
        func simpleDescription() -> String {
           return "A shape with \(numberOfSides) sides."
        }
    }
    
    注意:self的name属性是如何赋值的,是通过init方法传值的,其实就是通过函数赋值的。因此,创建NamedShape对象的时候,必须同时给name赋值,否则报错Missing argument for parameter 'name' in call

    override

    without override, is detected by the compiler as an error.
    重写父类方法,必须在方法前加关键字override。下面代码重写了simpleDescription函数

       var sideLength: Double
    
       init(sideLength: Double, name: String) {
           self.sideLength = sideLength
           super.init(name: name)
           numberOfSides = 4
       }
    
       func area() -> Double {
           return sideLength * sideLength
       }
    
       override func simpleDescription() -> String {
           return "A square with sides of length \(sideLength)."
       }
    }
    let test = Square(sideLength: 5.2, name: "my test square")
    test.area()
    test.simpleDescription()
    

    set方法和get方法

    下面代码定义一个正三角形,对周长perimeter的set方法和get方法重写,用意是:
    在设置边长的时候,赋值周长;在设置周长的时候,重置边长
    其中,newValue是一个含蓄的名称,你也可以在set方法里给一个明确的名称(就是自定义的名称)

    class EquilateralTriangle: NamedShape {
        var sideLength: Double = 0.0
    
        init(sideLength: Double, name: String) {
            self.sideLength = sideLength
            super.init(name: name)
            numberOfSides = 3
        }
    
        var perimeter: Double {
            get {
                 return 3.0 * sideLength
            }
            set {
                sideLength = newValue / 3.0
            }
        }
    
        override func simpleDescription() -> String {
            return "An equilateral triangle with sides of length \(sideLength)."
        }
    }
    var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
    print(triangle.perimeter) // 3.1
    triangle.perimeter = 9.9
    print(triangle.sideLength) // 3.3
    

    注意:

    Notice that the initializer for the EquilateralTriangle class has three different steps:
    • Setting the value of properties that the subclass declares.
    • Calling the superclass’s initializer.
    • Changing the value of properties defined by the superclass. Any additional setup work that uses methods, getters, or setters can also be done at this point.

    wiiSet 和 didSet

    If you don’t need to compute the property but still need to provide code that is run before and after setting a new value, use willSet and didSet. The code you provide is run any time the value changes outside of an initializer. For example, the class below ensures that the side length of its triangle is always the same as the side length of its square.
    属性观察

    class TriangleAndSquare {
        var triangle: EquilateralTriangle {
            willSet {
                square.sideLength = newValue.sideLength
            }
        }
        var square: Square {
            willSet {
                triangle.sideLength = newValue.sideLength
            }
        }
        init(size: Double, name: String) {
            square = Square(sideLength: size, name: name)
            triangle = EquilateralTriangle(sideLength: size, name: name)
        }
    }
    var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
    print(triangleAndSquare.square.sideLength)
    print(triangleAndSquare.triangle.sideLength)
    triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
    print(triangleAndSquare.triangle.sideLength)
    

    拆包符号‘?’

    如果一个是可选的(可能为nil),可以在操作前添加?,操作包含了方法,属性,下标。
    如果?前面的值是nil,那么,?后面的一切表达式都将被忽略。否则,这个可选值就是拆包(unwrapped),?后面的值将会一如既往的正常运行

    let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
    let sideLength = optionalSquare?.sideLength
    

    相关文章

      网友评论

          本文标题:Swift语法备忘录4-Objects and Classes

          本文链接:https://www.haomeiwen.com/subject/xqtunxtx.html