-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_10_Classes_And_Inheritance.swift
163 lines (107 loc) · 3.31 KB
/
Day_10_Classes_And_Inheritance.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Day 10 : Classes and Inheritance
- Creating your own classes
- Class Inheritance
- Overriding methods
- Final Classes
- Copying objects
- Deinitializers
- Mutability
5 Differences between Structs and Classes
1 ) You need to create an initializer all the time!
2 ) You can create a class based on an existing class (Inheritance, subclassing)
3 ) Structs is value types, but classes are reference type
4 ) Classes have deinitializers (deinit)
5 ) The way they deal with constants, classes can be changed anytime without using "mutating" keyword
*/
// -------------------- Creating your own classes --------------------
class Dog {
var name: String
var breed: String
init(name: String, breed: String) {
self.name = name
self.breed = breed
}
}
let poppy = Dog(name: "Poppy", breed: "Poodle")
// -------------------- Class inheritance --------------------
// Class you inherit from : Parent class, Super class
// The new class is called : Child class
// Dog class was created above, we'll inherit from it
class Poodle: Dog {
init(name: String) {
super.init(name: name, breed: "Poodle")
}
}
// -------------------- Overriding Methods --------------------
// For the redundant function name
class Dog2 {
func barking() {
print("Brooooooo!")
}
}
class Poodle2: Dog2 {
override func barking() {
print("Hok Peep!")
}
}
// -------------------- Final Classes --------------------
// It has a meaning in its word, "Final"
// That means you can't inherit from final class
final class Dog3 {
var name: String
var breed: String
init(name: String, breed: String) {
self.name = name
self.breed = breed
}
}
/*
// This class can not inherit from Dog3
class inheritFromDog3: Dog3 {
init(name: String) {
super.init(name: String, breed: "Poodle")
}
}
*/
// -------------------- Copying Objects --------------------
/*
Structs is value types (if you change instance's value, it won't change inside struct), Classes are reference type (if you change instance's value, it will change inside a class)
*/
class Singer {
var name: String = "Taylor Swift"
}
var singer1 = Singer()
print(singer1.name)
var singer2 = singer1
singer2.name = "Justin Beiber"
// This will change name in Singer class to Justin Beiber too! and singer1.name become Justin too! I mean....everywhere!!
// -------------------- De-initializers --------------------
// Deinitializers : code that gets run when an instance of class is destroyed
class People {
var name: String = "John Doe"
init() {
print("\(name) is alive!")
}
func printGreeting() {
print("Hello, I'm \(name)")
}
// deinitializers : this will be called when the People instance is being destroyed
deinit {
print("\(name) is no more!")
}
}
// Create 3 instances from People class
for _ in 1...3 {
let person = People()
person.printGreeting()
}
// -------------------- Mutability --------------------
// Classes don't need "mutating" keyword
class Singer2 {
var name: String = "Taylor Swift"
}
let taylor = Singer2()
taylor.name = "Ed Sheeran"
print(taylor.name)
// If you want to stop user from changing internal value of the class, use "let" instead of var