博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift语法基础:4 - Swift的类与对象
阅读量:5313 次
发布时间:2019-06-14

本文共 3719 字,大约阅读时间需要 12 分钟。

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


1.类方法

class Shape {
var numberOfSides = 0 func simpleDescription() -> String{ return "A shape with ​\(numberOfSides)​ sides." }}var shape = Shape()shape.numberOfSides = 7var shapeDescription = shape.simpleDescription()// 打印出来的结果: A shape with ​7​ 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 = 100println("\(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.9triangle.sideLength = 1.0var a = triangle.perimetervar b = triangle.sideLengthprintln("\(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?.sideLengthprintln(sideLength)// 打印出来的结果: Optional(2.5)

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


好了, 这次就讲到这里

转载于:https://www.cnblogs.com/iOSCain/p/4333134.html

你可能感兴趣的文章
c++回调函数
查看>>
linux下Rtree的安装
查看>>
【Java】 剑指offer(53-2) 0到n-1中缺失的数字
查看>>
Delphi中ListView类的用法
查看>>
多米诺骨牌
查看>>
Linq 学习(1) Group & Join--网摘
查看>>
asp.net 调用前台JS调用后台,后台掉前台JS
查看>>
Attribute(特性)与AOP
查看>>
苹果手表:大方向和谷歌一样,硬件分道扬镳
查看>>
Competing Consumers Pattern (竞争消费者模式)
查看>>
Android面试收集录15 Android Bitmap压缩策略
查看>>
PHP魔术方法之__call与__callStatic方法
查看>>
ubuntu 安装后的配置
查看>>
【模板】对拍程序
查看>>
【转】redo与undo
查看>>
解决升级系统导致的 curl: (48) An unknown option was passed in to libcurl
查看>>
Java Session 介绍;
查看>>
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>