class

This is an example of a struct definition and a class definition

struct Resolution {
  var width = 0
  var height = 0
}
class VideoMode {
  var resolution = Resolution()
  var interlaced = false
  var frameRate = 0.0
  var name: String?
}

The Resolution structure definition and the VideoMode class definition only describe the appearance of Resolution or VideoMode, create an instance of the structure or class:

let resolution = Resolution(width: 1920)
let someVideoMode = VideoMode()

Example

use data type

class Student {
  var name: String
  var year: Int
  var gpa: Double
  var honors: Bool
}

Use default property values

class Student {
  var name = ""
  var gpa = 0.0
  var honors = false
}

Inherit

Suppose we have a BankAccount class:

class BankAccount {
  var balance = 0.0
  func deposit(amount: Double) {
    balance += amount
  }
  func withdraw(amount: Double) {
    balance -= amount
  }
}

SavingsAccount extends BankAccount class

class SavingsAccount: BankAccount {
  varinterest = 0.0
  func addInterest() {
    let interest = balance \*0.005
    self. deposit(amount: interest)
  }
}

The new SavingsAccount class (subclass) automatically gets all the characteristics of the BankAccount class (superclass). Additionally, the SavingsAccount class defines an .interest property and an .addInterest() method.

Class Attributes

var ferris = Student()
ferris.name = "Ferris Bueller"
ferris.year = 12
ferris.gpa = 3.81
ferris.honors = false

instance of the class

class Person {
  var name = ""
  var age = 0
}
var sonny = Person()
// sonny is now an instance of Person

reference type (class)

class Player {
  var name: String
  init(name: String) {
    self.name = name
  }
}
var player1 = Player(name: "Tomoko")
var player2 = player1
player2.name = "Isabella"
print(player1.name) // Isabella
print(player2.name) // Isabella
Comments