美文网首页
Swift 4 Classes

Swift 4 Classes

作者: 艺术农 | 来源:发表于2018-06-14 16:28 被阅读16次

类和结构体很像,同样是自定义类型,同样可以在内部定义属性和方法。我们知道结构体是值类型,而类是引用类型。实际使用时,我们用结构体来代表值,而用类来表示对象。

创建类

class Person {
  var firstName: String
  var lastName: String

  init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
  }

  var fullName: String {
    return "\(firstName) \(lastName)"
  }
}

let john = Person(firstName: "Johnny", lastName: "Appleseed")

我们可以看到类的定义和结构体几乎一样,一个是struct关键字,一个是class而已。
但是,我们细心点可以发现,类是必须要自己实现构造方法的,而不像结构体(系统默认为结构体提供了构造方法)。如果我们没有为类提供构造方法,编译阶段就会报错。

引用类型(Reference types)

在Swift中,结构体的实例是不可变的值,类的实例变量是可变的。因为类的引用只是指向内存区域的指针而已。

When you create a reference type such as class, the system stores the actual instance in a region of memory known as the heap. Instances of a value type such as a struct resides in a region of memory called the stack, unless the value is part of a class instance, in which case the value is stored on the heap with the rest of the class instance.

当创建一个引用类型(如类)时,系统将实际实例存储在一个称为堆的内存区域中。值类型(例如结构)的实例驻留在一个称为堆栈的内存区域中,除非该值是类实例的一部分,在这种情况下,该值与堆实例的其余部分一起存储在堆中。

对象之间的比较(Object identity)

john === homeOwner // true

Swift使用‘===’来比较两个引用是否相等。其实比较的就是两个引用所指向的堆地址是否相同。

下面我们看个例子来详细说明可变和不可变的区别:

struct Grade {
  let letter: String
  let points: Double
  let credits: Double
}

class Student {
  var firstName: String
  var lastName: String
  var grades: [Grade] = []

  init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
  }

  func recordGrade(_ grade: Grade) {
    grades.append(grade)
  }
}

let jane = Student(firstName: "Jane", lastName: "Appleseed")
let history = Grade(letter: "B", points: 9.0, credits: 3.0)
var math = Grade(letter: "A", points: 16.0, credits: 4.0)

jane.recordGrade(history)
jane.recordGrade(math)

上面的代码我们可以发现,recordGrade方法正在修改实例的内容,但是我们并没有使用mutating来修饰这个方法。mutating标记这个方法会修改当前实例的值为新值。但是,在类中我们无需这么做,因为累的实例本身就是可变的。

类扩展

和结构体一样,我们同样可以为类扩展计算属性和方法。

相关文章

网友评论

      本文标题:Swift 4 Classes

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