-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathContents.swift
74 lines (59 loc) · 2.16 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import UIKit
class Spaceship {
let name: String
var health: Int
var position: Int
func moveLeft() {
position -= 1
}
func moveRight() {
position += 1
}
func wasHit() {
health -= 5
}
init(name: String, health: Int, position: Int) {
self.name = name
self.health = health
self.position = position
}
}
let falcon = Spaceship(name: "Falcon", health: 100, position: 0)
class Fighter: Spaceship {
let weapon: String
var remainingFirePower: Int
func fire() {
if remainingFirePower > 0 {
remainingFirePower -= 1
} else {
print("You have no more fire power.")
}
}
init(name: String, health: Int, position: Int, wepon: String, remainingFirePower: Int ) {
self.weapon = wepon
self.remainingFirePower = remainingFirePower
super.init(name: name, health: health, position: position)
}
}
class ShieldedShip: Fighter {
var shieldStrength: Int
override func wasHit() {
if shieldStrength > 0 {
shieldStrength -= 5
} else {
super.wasHit()
}
}
init(name: String, health: Int, position: Int, wepon: String, remainingFirePower: Int, shieldStrength: Int) {
self.shieldStrength = shieldStrength
super.init(name: name, health: health, position: position, wepon: wepon, remainingFirePower: remainingFirePower)
}
}
let defender = ShieldedShip(name: "Defender", health: 100, position: 0, wepon: "Cannon", remainingFirePower: 10, shieldStrength: 25)
let sameShip = falcon
print(sameShip.position)
print(falcon.position)
sameShip.moveLeft()
print(sameShip.position)
print(falcon.position)
// The position changed on both sameShip and falcon because they both reference the same address. An update to one instance will update all those which share the same address. If both were structs instead of classes, you would be assigining a literal value to the variable. If you create a variable equal to another structure variable, the value is copied. Operations performed on either variable will be reflected only on the modified variable.