Swift语法基础:4 - Swift的类与对象

前端之家收集整理的这篇文章主要介绍了Swift语法基础:4 - Swift的类与对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在前面我们讲解完了一些基本的东西,现在来看看Swift里面的类声明已经怎么使用对象.


1.类方法

class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String{
        return "A shape with ​\(numberOfSides)​ sides."
    }
}
var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()
// 打印出来的结果: A shape with7​ sides.

2.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."
    }
}
var namedShape = NamedShape(name: "xiaoming")
var namedDescription = namedShape.simpleDescription()
namedShape.name = "xiaoming"
namedShape.numberOfSides = 100

println("\(namedShape.name),\(namedShape.numberOfSides)")
// 打印出来的结果: xiaoming,100

这个同样是官方的例子,里面numberOfSides是直接借用上面的例子:

class Square: NamedShape {
    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 shape with ​\(numberOfSides)​ sides."
    }
}
let test = Square(sideLength: 5.2,name: "my test square")
println("\(test.area()),\(test.simpleDescription())")
// 打印出来的结果: 27.04,A shape with ​4​ sides.

PS: override这个单词的意思,如果你要重写父类的某个方法就必须得加这个关键字,如果不加的话,编译器就会报错,override还有一个功能就是会去父类查看你所要重写的方法父类中是否存在.


3.Getter方法和Setter方法

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 "A shape with ​\(numberOfSides)​ sides."
    }
}
var triangle = EquilateralTriangle(sideLength: 3.1,name: "a triangle")
triangle.perimeter = 9.9
triangle.sideLength = 1.0
var a = triangle.perimeter
var b = triangle.sideLength

println("\(a),\(b),\(triangle.simpleDescription())")
// 打印出来的结果: 3.0,1.0,A shape with ​3​ sides.

PS: 在例子里面,perimeter在set方法里面对应的是newValuew,它的运算也是在这里.


4.willSet和didSet

在前面,如果我们需要运算某些特定的值,就是用getter和setter,如果不需要运算的话,就使用willSet和didSet:

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")
println(triangleAndSquare.square.sideLength)
println(triangleAndSquare.triangle.sideLength)
triangleAndSquare.square = Square(sideLength: 59,name: "larger square")
println(triangleAndSquare.triangle.sideLength)
// 打印出来的结果: 10.0,10.0,59.0

5.函数的参数
在Swift中,函数的声明可以加参数也可以不加参数,如果加了参数,在调用时就必须以显式声明,第一个参数除外:

class Counter { var count: Int = 0 func incrementBy(amount: Int,numberOfTimes times: Int){ count += amount * times println(count) } } var counter = Counter() counter.incrementBy(2,numberOfTimes:7) // 打印出来的结果: 14

6.关于可选符号 “?”

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

let sideLength = optionalSquare?.sideLength
println(sideLength)
// 打印出来的结果: Optional(2.5)

PS: 可选符 “?” 的意思是,如果在可选符之前的值是空的,那么就会返回nil,后面的东西就不会理会,如果返回的值非空,那么后面的值才会有效.


好了,这次就讲到这里

猜你在找的Swift相关文章