1
00:00:00,000 --> 00:00:02,500
(intro music)

2
00:00:05,412 --> 00:00:07,431
The next concept that it's time

3
00:00:07,431 --> 00:00:09,335
to talk about is Classes.

4
00:00:09,335 --> 00:00:11,080
Now classes are fundamental part

5
00:00:11,080 --> 00:00:13,065
of object oriented programming.

6
00:00:13,065 --> 00:00:15,542
Now we do some classes with the previous videos

7
00:00:15,542 --> 00:00:18,707
and that's because string and int are classes.

8
00:00:18,707 --> 00:00:22,225
So think of a class as some sort of thing in an application.

9
00:00:22,225 --> 00:00:25,058
Now effectively, they are nouns within an application.

10
00:00:25,058 --> 00:00:29,311
Like, for example a player or an enemy or a train maybe.

11
00:00:29,311 --> 00:00:31,169
Now, using computer game analogies,

12
00:00:31,169 --> 00:00:34,380
these could all be classes in a game that we've written.

13
00:00:34,380 --> 00:00:36,563
So, we're going to go through the process

14
00:00:36,563 --> 00:00:39,164
of creating a class so that you can get a good handle

15
00:00:39,164 --> 00:00:41,637
on why you'd want to use a class.

16
00:00:41,637 --> 00:00:45,343
Now, the Android framework contains a load of classes,

17
00:00:45,343 --> 00:00:46,237
and a lot of the work in writing

18
00:00:46,237 --> 00:00:49,267
Android programmes is already done for us.

19
00:00:49,267 --> 00:00:52,680
And all the widgets that we add to our apps are classes.

20
00:00:52,680 --> 00:00:55,513
The text view widget that displays so it displayed

21
00:00:55,513 --> 00:00:57,227
hollow word in the last section

22
00:00:57,227 --> 00:01:00,594
was an object created from the text view class.

23
00:01:00,594 --> 00:01:03,183
So, we didn't have to a lot to display the text,

24
00:01:03,183 --> 00:01:05,119
because the Android framework took care

25
00:01:05,119 --> 00:01:07,208
of everything needed to centre the widget

26
00:01:07,208 --> 00:01:09,472
and ultimately display the text.

27
00:01:09,472 --> 00:01:10,493
So, once we've seen how

28
00:01:10,493 --> 00:01:13,198
to write a class, we'll look at how to use them.

29
00:01:13,198 --> 00:01:16,031
Now, as an example of some classes we're gonna create

30
00:01:16,031 --> 00:01:19,688
some basic ones that you can use in an adventure game.

31
00:01:19,688 --> 00:01:22,080
Now, we're not gonna be producing a full game,

32
00:01:22,080 --> 00:01:25,435
but we use some of the classes that a game may use just

33
00:01:25,435 --> 00:01:28,234
to look at the concept of classes.

34
00:01:28,234 --> 00:01:30,317
Now, to create a class we need to make sure

35
00:01:30,317 --> 00:01:34,165
that the project pane is actually expanded as it is

36
00:01:34,165 --> 00:01:36,625
on the right hand side and you need to come over here

37
00:01:36,625 --> 00:01:39,101
to the java folder, right click that,

38
00:01:39,101 --> 00:01:43,018
select new, and select Kotlin file slash class.

39
00:01:44,419 --> 00:01:47,239
Now you're gonna type in player for the name of the class.

40
00:01:47,239 --> 00:01:49,412
(keys typing)

41
00:01:49,412 --> 00:01:51,541
So, we're creating a class called player,

42
00:01:51,541 --> 00:01:53,186
which is going to compute to

43
00:01:53,186 --> 00:01:56,582
have some basic information about player in a game.

44
00:01:56,582 --> 00:01:58,272
Now the convention is that class

45
00:01:58,272 --> 00:02:00,249
then start with a capital P,

46
00:02:00,249 --> 00:02:02,996
and you saw that I used a capital P for the word player,

47
00:02:02,996 --> 00:02:05,532
so make sure to use that capital P.

48
00:02:05,532 --> 00:02:09,100
Now, in the Kind of drop down here, if I click on that now,

49
00:02:09,100 --> 00:02:11,606
you want to choose class here.

50
00:02:11,606 --> 00:02:13,732
Now, it's not that important, you can leave it set

51
00:02:13,732 --> 00:02:16,968
to file if you want, but doing it this way,

52
00:02:16,968 --> 00:02:18,195
changing it and selecting class

53
00:02:18,195 --> 00:02:19,749
can save you a bit of typing.

54
00:02:19,749 --> 00:02:22,902
So click on Ok, and you can see

55
00:02:22,902 --> 00:02:26,244
that Android Studios automatically created

56
00:02:26,244 --> 00:02:29,157
the basic declaration for a new class.

57
00:02:29,157 --> 00:02:31,164
Now, there's not a lot of code in there,

58
00:02:31,164 --> 00:02:33,292
so if you changed in file a moment ago,

59
00:02:33,292 --> 00:02:35,179
you wouldn't have a lot of code to type in.

60
00:02:35,179 --> 00:02:37,714
You'd have to just type in what you see on the screen there.

61
00:02:37,714 --> 00:02:40,778
So, the good thing with a class is it can be a container

62
00:02:40,778 --> 00:02:44,506
for lots of variables, and it can also have programme code

63
00:02:44,506 --> 00:02:46,846
that relates just to a player.

64
00:02:46,846 --> 00:02:48,217
Now, we're gonna go through and explore

65
00:02:48,217 --> 00:02:49,817
that a little bit further.

66
00:02:49,817 --> 00:02:53,017
What we'll start off doing is setting up some variables

67
00:02:53,017 --> 00:02:54,541
that would be appropriate if

68
00:02:54,541 --> 00:02:57,122
this was a player class in a game.

69
00:02:57,122 --> 00:02:58,525
Alright, so what are some things

70
00:02:58,525 --> 00:03:01,137
that maybe a player in a game would have?

71
00:03:01,137 --> 00:03:03,476
Well they're gonna need a name to start with.

72
00:03:03,476 --> 00:03:05,151
We're not gonna have any players without names,

73
00:03:05,151 --> 00:03:07,627
so whenever we create new players,

74
00:03:07,627 --> 00:03:09,800
we're gonna have to give them a name.

75
00:03:09,800 --> 00:03:12,396
Now in Kotlin we can specify the name

76
00:03:12,396 --> 00:03:14,615
in what's called the primary constructor.

77
00:03:14,615 --> 00:03:16,697
Now, there are a couple of ways of doing that,

78
00:03:16,697 --> 00:03:18,961
but the most common is to add the variable

79
00:03:18,961 --> 00:03:21,376
to the class declaration.

80
00:03:21,376 --> 00:03:23,218
So, we're gonna have a go at doing

81
00:03:23,218 --> 00:03:25,980
that so at line 5 we've got class Player,

82
00:03:25,980 --> 00:03:28,229
and we're gonna come over here to the end of the word player

83
00:03:28,229 --> 00:03:30,160
add a parentheses and in there we're gonna type

84
00:03:30,160 --> 00:03:33,577
val space name colon and the word string.

85
00:03:37,180 --> 00:03:39,567
So what we've done there is to tell Kotlin

86
00:03:39,567 --> 00:03:41,288
that a name must be supplied

87
00:03:41,288 --> 00:03:44,200
whenever new player objects are created.

88
00:03:44,200 --> 00:03:47,325
And we're going to be seeing how to create them shortly.

89
00:03:47,325 --> 00:03:48,668
Yeah we also need to keep track of

90
00:03:48,668 --> 00:03:50,887
how many lives a player has as well as

91
00:03:50,887 --> 00:03:54,162
their score and the level they've reached in the game.

92
00:03:54,162 --> 00:03:56,139
So we need a few more variables.

93
00:03:56,139 --> 00:03:59,127
So let's go ahead and create those as well.

94
00:03:59,127 --> 00:04:00,441
And we're gonna put those in the class

95
00:04:00,441 --> 00:04:02,704
starting on line 6 as you can see here.

96
00:04:02,704 --> 00:04:06,537
We're gonna type Var space lives equals three.

97
00:04:08,522 --> 00:04:12,439
Var level equals one and Var score equals zero.

98
00:04:15,042 --> 00:04:17,170
Alright so that's the basic class created.

99
00:04:17,170 --> 00:04:19,676
Now, it doesn't do a lot at the moment,

100
00:04:19,676 --> 00:04:22,060
but it'll have a name when we create it

101
00:04:22,060 --> 00:04:24,520
as well as default values for

102
00:04:24,520 --> 00:04:27,147
the number of lives, the level, and the score.

103
00:04:27,147 --> 00:04:29,486
Now, players start off with three lives and they'll start

104
00:04:29,486 --> 00:04:32,656
at level one, and their score will actually be zero.

105
00:04:32,656 --> 00:04:34,376
Notice that I've declared those

106
00:04:34,376 --> 00:04:38,201
three variables using var rather than val.

107
00:04:38,201 --> 00:04:40,827
Now the name won't change after we create the player,

108
00:04:40,827 --> 00:04:42,639
so that's declared to be val.

109
00:04:42,639 --> 00:04:44,978
We expect the score and the level to increase

110
00:04:44,978 --> 00:04:47,151
and they player also may lose lives,

111
00:04:47,151 --> 00:04:50,517
so consequently these variables have to be vars.

112
00:04:50,517 --> 00:04:52,214
Now, another thing to notice is the way

113
00:04:52,214 --> 00:04:56,108
Kotlin can infer the types of variables a lot of the time.

114
00:04:56,108 --> 00:04:58,085
And we have talked about that before.

115
00:04:58,085 --> 00:04:59,646
Now we've assigned integer values to

116
00:04:59,646 --> 00:05:02,378
lives, level, and score, so we don't have

117
00:05:02,378 --> 00:05:04,323
to specify their type.

118
00:05:04,323 --> 00:05:06,979
Now, we haven't assigned any value to name yet,

119
00:05:06,979 --> 00:05:10,366
so we have to tell Kotlin that's going to hold the string.

120
00:05:10,366 --> 00:05:15,075
And that's why we've used the string type here on line five.

121
00:05:15,075 --> 00:05:16,931
Alright, let's create some players

122
00:05:16,931 --> 00:05:19,965
and see what we can do with our basic player class.

123
00:05:19,965 --> 00:05:22,546
So we're gonna switch back to Main.kt,

124
00:05:22,546 --> 00:05:24,840
so we're gonna click back up here in the tab

125
00:05:24,840 --> 00:05:26,063
and if for some reason that's not open

126
00:05:26,063 --> 00:05:29,474
you can double click main.kt over here to open it up.

127
00:05:29,474 --> 00:05:31,934
And what we're gonna do is delete all this other code,

128
00:05:31,934 --> 00:05:36,190
completely right up to, but not including line one.

129
00:05:36,190 --> 00:05:39,314
So, I've just got the first line fun main

130
00:05:39,314 --> 00:05:41,488
and nothing in between the code block

131
00:05:41,488 --> 00:05:45,518
on the block of code from lines two and onwards.

132
00:05:45,518 --> 00:05:49,080
Alright so how do we actually create a player called Tim?

133
00:05:49,080 --> 00:05:53,080
Well we can actually type val tim equals Player,

134
00:05:56,068 --> 00:05:57,985
then double quotes Tim.

135
00:06:00,822 --> 00:06:03,989
And you can type println tim dot name.

136
00:06:05,455 --> 00:06:07,288
Println tim dot lives.

137
00:06:09,395 --> 00:06:11,228
Println tim dot level.

138
00:06:13,470 --> 00:06:15,637
And Println tim dot score.

139
00:06:18,677 --> 00:06:21,016
And by the way, ignore this name colon

140
00:06:21,016 --> 00:06:22,903
that's appeared before tim.

141
00:06:22,903 --> 00:06:24,820
I'll come back to that soon and explain what it is.

142
00:06:24,820 --> 00:06:26,420
It's not part of the code,

143
00:06:26,420 --> 00:06:28,259
so you don't actually have to type that in.

144
00:06:28,259 --> 00:06:29,783
Alright, so on line two we've

145
00:06:29,783 --> 00:06:33,677
created a new instance of the player class.

146
00:06:33,677 --> 00:06:36,168
Now that's an important concept to understand,

147
00:06:36,168 --> 00:06:38,220
the difference between the class

148
00:06:38,220 --> 00:06:40,243
and instances of that class.

149
00:06:40,243 --> 00:06:42,250
Now, one way to look at it is to consider

150
00:06:42,250 --> 00:06:45,027
the classification of life on this planet.

151
00:06:45,027 --> 00:06:47,955
Now all life forms fit into different classifications,

152
00:06:47,955 --> 00:06:50,913
and we're all members of a class called humans.

153
00:06:50,913 --> 00:06:53,132
Now, we weren't all created from a template

154
00:06:53,132 --> 00:06:54,475
that defines a human,

155
00:06:54,475 --> 00:06:57,479
and we're all individual instances of human.

156
00:06:57,479 --> 00:07:00,105
So my name property has the value tim,

157
00:07:00,105 --> 00:07:02,414
but yours probably has a different value.

158
00:07:02,414 --> 00:07:04,950
Now, of course people can share the same name,

159
00:07:04,950 --> 00:07:07,907
but if there's another Tim Buchalka instance out there

160
00:07:07,907 --> 00:07:09,869
they'll have different values for other properties

161
00:07:09,869 --> 00:07:12,707
such as their height or hair colour or whatever.

162
00:07:12,707 --> 00:07:14,729
So the class is called human

163
00:07:14,729 --> 00:07:17,839
and we're all instances of the human class.

164
00:07:17,839 --> 00:07:21,370
And that's really the difference between the two things.

165
00:07:21,370 --> 00:07:24,042
So the class is just a template and the values

166
00:07:24,042 --> 00:07:25,853
for these properties will be assigned

167
00:07:25,853 --> 00:07:29,098
when we create instances of the player class.

168
00:07:29,098 --> 00:07:32,244
So let's actually run this programme and see what happens.

169
00:07:32,244 --> 00:07:34,744
(mouse click)

170
00:07:37,949 --> 00:07:39,269
Okay, so we can see the values of

171
00:07:39,269 --> 00:07:41,699
the properties printed out over here.

172
00:07:41,699 --> 00:07:43,706
Now, we used dot notation to refer

173
00:07:43,706 --> 00:07:46,664
to the various properties that Tim has.

174
00:07:46,664 --> 00:07:50,317
So, on line three Tim dot name refers to the name property.

175
00:07:50,317 --> 00:07:52,240
And then the other ones Tim dot lives

176
00:07:52,240 --> 00:07:55,455
refers to the lives property and so on.

177
00:07:55,455 --> 00:07:59,107
Now as well as properties, classes can also have behaviour.

178
00:07:59,107 --> 00:08:00,979
And we're gonna change some of the properties

179
00:08:00,979 --> 00:08:02,986
and it's gonna get a bit tedious typing all of

180
00:08:02,986 --> 00:08:05,121
those println statements in,

181
00:08:05,121 --> 00:08:07,935
and it'd be really handy if player instances

182
00:08:07,935 --> 00:08:10,274
were able to display these values.

183
00:08:10,274 --> 00:08:11,518
So, one way to do this is to add a

184
00:08:11,518 --> 00:08:14,371
show function to the player class.

185
00:08:14,371 --> 00:08:17,978
So, let's go back to the player class file and do that.

186
00:08:17,978 --> 00:08:20,580
So we're gonna come down here, and we're going

187
00:08:20,580 --> 00:08:22,496
to leave a gap between the

188
00:08:22,496 --> 00:08:24,474
var score line and the start of the function.

189
00:08:24,474 --> 00:08:28,609
Type fun show parentheses, then put a code block,

190
00:08:28,609 --> 00:08:31,069
then we're actually gonna type println,

191
00:08:31,069 --> 00:08:34,297
then we're going to do parentheses,

192
00:08:34,297 --> 00:08:38,282
then we're gonna do three double quotes.

193
00:08:38,282 --> 00:08:42,010
Three like that, then we're gonna press enter.

194
00:08:42,010 --> 00:08:43,359
And you'll make a space there, then you're gonna type

195
00:08:43,359 --> 00:08:46,942
name colon dollar name, lives dollar lives,

196
00:08:53,373 --> 00:08:56,623
the next line level colon dollar level,

197
00:08:57,885 --> 00:09:01,885
and then the last line score colon dollar score.

198
00:09:03,093 --> 00:09:04,095
Then for the ending parentheses

199
00:09:04,095 --> 00:09:06,359
I'm gonna do three double quotes again.

200
00:09:06,359 --> 00:09:07,692
One, two, three.

201
00:09:08,699 --> 00:09:10,676
So the show function is quite simple.

202
00:09:10,676 --> 00:09:13,248
It just prints the values of the properties.

203
00:09:13,248 --> 00:09:15,301
Now, I've used triple speech marks with

204
00:09:15,301 --> 00:09:17,112
double quotes to format the values,

205
00:09:17,112 --> 00:09:19,014
which we haven't seen before.

206
00:09:19,014 --> 00:09:20,568
Now, using these triple double quotes like this

207
00:09:20,568 --> 00:09:23,979
is a way to split a string over several lines,

208
00:09:23,979 --> 00:09:25,458
and you'll see the effect

209
00:09:25,458 --> 00:09:27,390
that it actually has when we run the programme.

210
00:09:27,390 --> 00:09:29,579
So we'll go back to main.kt, and you can now

211
00:09:29,579 --> 00:09:32,567
remove all these println method calls now

212
00:09:32,567 --> 00:09:34,710
because we don't need them anymore.

213
00:09:34,710 --> 00:09:36,431
So let's now move these println statements,

214
00:09:36,431 --> 00:09:38,031
the four of them.

215
00:09:38,031 --> 00:09:40,491
Actually what I'll do is I'll comment them out

216
00:09:40,491 --> 00:09:44,574
and I'm gonna call the Tim show function instead.

217
00:09:46,619 --> 00:09:49,036
So I'm gonna do Tim dot show.

218
00:09:50,301 --> 00:09:52,551
So let's actually run this.

219
00:09:55,961 --> 00:09:56,794
And you can see we've got

220
00:09:56,794 --> 00:10:00,154
name, tim, lives, level, and score.

221
00:10:00,154 --> 00:10:04,003
So we used dot notation to call functions on class instances

222
00:10:04,003 --> 00:10:06,387
as well as to access the properties.

223
00:10:06,387 --> 00:10:10,221
So, here tim dot show that we are using on line seven,

224
00:10:10,221 --> 00:10:14,719
that calls the show function on the variable tim.

225
00:10:14,719 --> 00:10:17,767
And if you recall tim's an instance of the player class,

226
00:10:17,767 --> 00:10:21,344
and all player instances will have a show function.

227
00:10:21,344 --> 00:10:24,755
Now you can see that by creating another player instance.

228
00:10:24,755 --> 00:10:27,623
So I'm gonna call this one Louise.

229
00:10:27,623 --> 00:10:30,873
I'm gonna type val louise equals player

230
00:10:32,423 --> 00:10:34,840
capital P parentheses louise.

231
00:10:37,343 --> 00:10:41,162
Then I'm gonna type louise dot level

232
00:10:41,162 --> 00:10:45,658
equals five then you'll type louise dot show.

233
00:10:45,658 --> 00:10:49,628
So we provide the name louise when we create the new player

234
00:10:49,628 --> 00:10:53,537
instance and I've also set louise's level to five.

235
00:10:53,537 --> 00:10:55,460
So if we run the programme now, we should get

236
00:10:55,460 --> 00:11:00,127
the details of Louise printed out after mine, after tim.

237
00:11:02,056 --> 00:11:03,119
And you can see we've got name tim

238
00:11:03,119 --> 00:11:06,144
lives three level one score zero,

239
00:11:06,144 --> 00:11:08,740
and then louise lives three level five,

240
00:11:08,740 --> 00:11:11,004
this time, and score zero.

241
00:11:11,004 --> 00:11:14,173
So each player will have a different name and their scores

242
00:11:14,173 --> 00:11:17,011
and these other values will also be different.

243
00:11:17,011 --> 00:11:19,803
Now we're storing these values as properties in the class

244
00:11:19,803 --> 00:11:22,097
and each instance of the player class

245
00:11:22,097 --> 00:11:25,750
will have its own values for those properties.

246
00:11:25,750 --> 00:11:28,104
Now the code's slightly untidy because

247
00:11:28,104 --> 00:11:31,515
louise was created with a default level

248
00:11:31,515 --> 00:11:34,956
of one then we changed it on the next line to five.

249
00:11:34,956 --> 00:11:37,628
It'd actually be neater to specify her level

250
00:11:37,628 --> 00:11:40,586
when we created the louise instance of player.

251
00:11:40,586 --> 00:11:44,404
Now, in java you have different constructors for class.

252
00:11:44,404 --> 00:11:45,813
So, you could do something like this.

253
00:11:45,813 --> 00:11:48,032
Go back up to louise and we could put

254
00:11:48,032 --> 00:11:52,365
something like comma five and then delete this line.

255
00:11:54,928 --> 00:11:55,761
Now we've got an error because

256
00:11:55,761 --> 00:12:00,130
that class isn't set up to accept a level when we create it.

257
00:12:00,130 --> 00:12:03,450
Now as I said in Java, you create different constructors

258
00:12:03,450 --> 00:12:05,337
that will accept different arguments

259
00:12:05,337 --> 00:12:07,012
when creating the class.

260
00:12:07,012 --> 00:12:09,367
Now let's also possible on kotlin,

261
00:12:09,367 --> 00:12:12,626
but kotlin allows default values for arguments.

262
00:12:12,626 --> 00:12:15,237
Now, you'll see Java classes with different constructors

263
00:12:15,237 --> 00:12:17,954
when we start using the Android framework classes.

264
00:12:17,954 --> 00:12:20,112
So don't worry too much about them at this stage,

265
00:12:20,112 --> 00:12:21,878
they're just Java's way of doing

266
00:12:21,878 --> 00:12:23,765
what we're about to do in kotlin.

267
00:12:23,765 --> 00:12:26,346
So what we can do is change our player class

268
00:12:26,346 --> 00:12:29,953
to allow the number of lives and the level to be specified

269
00:12:29,953 --> 00:12:32,654
when we create the class instances.

270
00:12:32,654 --> 00:12:35,477
So let's actually go back to player.kt

271
00:12:35,477 --> 00:12:38,394
and up here on the line, line five,

272
00:12:39,641 --> 00:12:42,600
we're going to put a comma after the string,

273
00:12:42,600 --> 00:12:46,767
comma space var level column space int equals one.

274
00:12:51,550 --> 00:12:53,562
Now, because we've now declared the level properly

275
00:12:53,562 --> 00:12:55,328
in the constructor we can

276
00:12:55,328 --> 00:12:57,502
delete its declaration inside the class.

277
00:12:57,502 --> 00:12:59,403
In fact, we have to do that,

278
00:12:59,403 --> 00:13:01,199
you can't declare the same thing twice

279
00:13:01,199 --> 00:13:02,150
and you can see we actually

280
00:13:02,150 --> 00:13:05,727
got an error here because of that, conflicting declarations.

281
00:13:05,727 --> 00:13:08,644
So, let's go ahead and delete that.

282
00:13:10,533 --> 00:13:12,586
And the errors then disappear.

283
00:13:12,586 --> 00:13:14,337
So, now that we've actually done that,

284
00:13:14,337 --> 00:13:17,174
we can provide a value for level when we create louise,

285
00:13:17,174 --> 00:13:19,061
so the error in main should have been fixed.

286
00:13:19,061 --> 00:13:21,038
So if we go back to that now,

287
00:13:21,038 --> 00:13:23,876
you can see the error fixed itself automatically

288
00:13:23,876 --> 00:13:26,543
and if we run the programme now...

289
00:13:29,421 --> 00:13:31,640
We should see that level has been set to five,

290
00:13:31,640 --> 00:13:32,998
which it has for louise

291
00:13:32,998 --> 00:13:36,062
and that's because we set the level to be five.

292
00:13:36,062 --> 00:13:37,994
In the case of tim, level is set to one

293
00:13:37,994 --> 00:13:39,971
because that's the default value.

294
00:13:39,971 --> 00:13:42,431
So now in other words because we haven't provided a value

295
00:13:42,431 --> 00:13:46,304
for tim on line two the default value of one is used.

296
00:13:46,304 --> 00:13:49,986
So in other words tim level stays at level one.

297
00:13:49,986 --> 00:13:52,039
And you may have noticed when I switched

298
00:13:52,039 --> 00:13:56,039
back to the main function that line nine changed

299
00:13:57,910 --> 00:13:59,577
and you saw level actually appear.

300
00:13:59,577 --> 00:14:02,814
So, the value of five now has the word level in front of it.

301
00:14:02,814 --> 00:14:04,368
And as I mentioned previously,

302
00:14:04,368 --> 00:14:06,059
that's not part of the code,

303
00:14:06,059 --> 00:14:08,111
that's Android studio showing us

304
00:14:08,111 --> 00:14:11,024
what the various arguments represent.

305
00:14:11,024 --> 00:14:14,511
So louise is the value we're providing for the name,

306
00:14:14,511 --> 00:14:17,514
and five's the value for the level.

307
00:14:17,514 --> 00:14:20,261
Now, that's quite a neat feature and it's very helpful

308
00:14:20,261 --> 00:14:23,295
to have a reminder of what the arguments are.

309
00:14:23,295 --> 00:14:24,155
It can be confusing when

310
00:14:24,155 --> 00:14:26,132
you've watched the code on a video though.

311
00:14:26,132 --> 00:14:28,924
You don't have to type the name colon and level colon here,

312
00:14:28,924 --> 00:14:30,887
they're actually put on the screen automatically

313
00:14:30,887 --> 00:14:32,411
by Android Studio.

314
00:14:32,411 --> 00:14:35,414
Now, if you're using the default colour scheme,

315
00:14:35,414 --> 00:14:38,373
the argument name's hints appear in a light grey,

316
00:14:38,373 --> 00:14:40,788
with a light background as you can see here.

317
00:14:40,788 --> 00:14:43,405
But, in the dark colour scheme, if you're running that,

318
00:14:43,405 --> 00:14:46,363
they're the same, but the background's a bit harder to see,

319
00:14:46,363 --> 00:14:48,944
against dark color's black background.

320
00:14:48,944 --> 00:14:51,470
So remember that the display by Android studio,

321
00:14:51,470 --> 00:14:53,523
to remind you what the arguments are,

322
00:14:53,523 --> 00:14:54,715
they're not part of the code

323
00:14:54,715 --> 00:14:56,481
and you shouldn't be typing them in.

324
00:14:56,481 --> 00:14:58,055
Alright, before I show you another way

325
00:14:58,055 --> 00:15:00,229
to specify the values for these arguments,

326
00:15:00,229 --> 00:15:03,498
It's actually time for challenge.

327
00:15:03,498 --> 00:15:06,509
So your challenge is to modify the player class

328
00:15:06,509 --> 00:15:09,090
so that its constructor allows different values

329
00:15:09,090 --> 00:15:12,122
for the lives and score properties.

330
00:15:12,122 --> 00:15:14,054
Now the defaults for both will be the defaults

331
00:15:14,054 --> 00:15:16,303
that the class is currently using.

332
00:15:16,303 --> 00:15:17,662
When you've made the changes,

333
00:15:17,662 --> 00:15:20,273
create two new instances of the player class.

334
00:15:20,273 --> 00:15:22,250
And You can call them anything you like.

335
00:15:22,250 --> 00:15:23,759
So, the first instance should

336
00:15:23,759 --> 00:15:26,355
have a level of four and eight lives.

337
00:15:26,355 --> 00:15:29,434
The second one should have a level of two, five lives,

338
00:15:29,434 --> 00:15:31,607
and a score of 1000.

339
00:15:31,607 --> 00:15:33,811
Use the show function to print out the values

340
00:15:33,811 --> 00:15:35,366
for your new instances.

341
00:15:35,366 --> 00:15:39,018
And note, when you add parameters that have default values,

342
00:15:39,018 --> 00:15:42,882
they must be listed after any parameters that don't.

343
00:15:42,882 --> 00:15:45,297
So name must be the first parameter that's declared

344
00:15:45,297 --> 00:15:46,489
in the player class.

345
00:15:46,489 --> 00:15:48,829
And that's why I added level after it.

346
00:15:48,829 --> 00:15:50,827
So, that's the challenge pause the video now,

347
00:15:50,827 --> 00:15:53,059
see how you go, and I'll see you when you get back,

348
00:15:53,059 --> 00:15:54,734
and I'll show you the solution.

349
00:15:54,734 --> 00:15:56,401
Pause the video now.

350
00:15:58,296 --> 00:16:00,228
Alright so to allow the number of lives

351
00:16:00,228 --> 00:16:03,926
and the score to be set when we create new player instances

352
00:16:03,926 --> 00:16:07,171
we need to add two more parameters to the class declaration.

353
00:16:07,171 --> 00:16:10,371
Now, because that results in duplicate declarations

354
00:16:10,371 --> 00:16:11,441
for the two properties,

355
00:16:11,441 --> 00:16:14,882
we also have to delete them from inside the class.

356
00:16:14,882 --> 00:16:16,316
So, let's go have a look at doing that.

357
00:16:16,316 --> 00:16:19,561
Go to the player class, and we're gonna start by

358
00:16:19,561 --> 00:16:21,515
leaving val name string there

359
00:16:21,515 --> 00:16:23,613
and also leave the level as it is.

360
00:16:23,613 --> 00:16:25,484
After the int equals one for level,

361
00:16:25,484 --> 00:16:30,151
we're gonna put a comma var lives colon int equals three

362
00:16:32,418 --> 00:16:35,918
comma and var score colon int equals zero.

363
00:16:38,425 --> 00:16:40,628
Right, now we need to delete these two lines,

364
00:16:40,628 --> 00:16:43,013
the var lives equals three and var score equals zero,

365
00:16:43,013 --> 00:16:44,899
because we've defined them now as arguments

366
00:16:44,899 --> 00:16:47,843
in the class definition on line five.

367
00:16:47,843 --> 00:16:49,080
And that should fix the errors,

368
00:16:49,080 --> 00:16:51,163
which you can see that they have been fixed.

369
00:16:51,163 --> 00:16:53,669
So that's to change to our player class.

370
00:16:53,669 --> 00:16:55,102
Now that we've done that

371
00:16:55,102 --> 00:16:57,034
we can create two new player objects

372
00:16:57,034 --> 00:17:00,068
and specify their lives and score.

373
00:17:00,068 --> 00:17:03,479
Now, I use the term objects there instead of instances.

374
00:17:03,479 --> 00:17:04,958
They are actually the same thing.

375
00:17:04,958 --> 00:17:05,790
You'll hear people talk

376
00:17:05,790 --> 00:17:08,701
about instances of a class or objects.

377
00:17:08,701 --> 00:17:10,663
An object is just an instance of a class,

378
00:17:10,663 --> 00:17:13,907
so the two terms are used interchangeably.

379
00:17:13,907 --> 00:17:17,002
Alright so let's go back to the main.kt

380
00:17:17,002 --> 00:17:19,991
and let's add this extra, two extra instances.

381
00:17:19,991 --> 00:17:24,407
So start with val space gr8 equals player parentheses

382
00:17:28,020 --> 00:17:31,270
gr8 comma space comma four comma eight,

383
00:17:36,653 --> 00:17:39,736
then val one two watch equals player,

384
00:17:45,724 --> 00:17:46,992
and we'll use the name Ace

385
00:17:46,992 --> 00:17:49,659
comma two comma five comma 1000.

386
00:17:53,799 --> 00:17:55,535
Then let's actually show theirs as well,

387
00:17:55,535 --> 00:17:59,535
so gr8 dot show and then one two watch dot show.

388
00:18:02,432 --> 00:18:04,599
So let's now run the code.

389
00:18:06,311 --> 00:18:09,315
And you can see we've got the output now for the other two,

390
00:18:09,315 --> 00:18:12,107
the tim then the louise, but also for the gr8,

391
00:18:12,107 --> 00:18:14,084
lives is eight and levels is four,

392
00:18:14,084 --> 00:18:17,417
and Ace lives five level two score 1000.

393
00:18:18,884 --> 00:18:21,027
And you may have noticed when I was typing the arguments

394
00:18:21,027 --> 00:18:22,913
that Android Studio showed the name

395
00:18:22,913 --> 00:18:24,966
of the argument that corresponds to the position

396
00:18:24,966 --> 00:18:26,898
where I'm typing the numbers.

397
00:18:26,898 --> 00:18:28,422
Now you may have wondered what use these

398
00:18:28,422 --> 00:18:30,837
argument names hints were when I mentioned them before,

399
00:18:30,837 --> 00:18:33,101
but now that we've got three numbers in the row for

400
00:18:33,101 --> 00:18:36,135
one two watch you can see that they're useful.

401
00:18:36,135 --> 00:18:37,116
Without those hints you'd have to remember

402
00:18:37,116 --> 00:18:40,814
what order you defined the parameters in the player class,

403
00:18:40,814 --> 00:18:43,063
but now we can clearly see that five is the number

404
00:18:43,063 --> 00:18:45,734
of lives and 1000 is the score.

405
00:18:45,734 --> 00:18:47,832
This is the code on line 13.

406
00:18:47,832 --> 00:18:49,794
And as I said we ran the programme

407
00:18:49,794 --> 00:18:51,892
and we got the correct results.

408
00:18:51,892 --> 00:18:54,518
Now, by the way, setting the score like this would be useful

409
00:18:54,518 --> 00:18:57,446
if we were storing a saved game for these two players,

410
00:18:57,446 --> 00:18:59,695
and having to create them with the values they had

411
00:18:59,695 --> 00:19:01,884
when they saved the game, in case you're wondering.

412
00:19:01,884 --> 00:19:04,872
Now, I've used modern text speak style names here,

413
00:19:04,872 --> 00:19:06,638
just to show that you have...

414
00:19:06,638 --> 00:19:08,645
You can have numbers in variable names.

415
00:19:08,645 --> 00:19:10,788
So variable names mustn't start with a number,

416
00:19:10,788 --> 00:19:14,109
but you can use numbers within the names of variables.

417
00:19:14,109 --> 00:19:15,181
Alright, so that's been a

418
00:19:15,181 --> 00:19:17,565
quick overview of what classes are.

419
00:19:17,565 --> 00:19:19,482
Now you'll find as you go through the course

420
00:19:19,482 --> 00:19:22,123
that Android has a lot of classes built into it.

421
00:19:22,123 --> 00:19:24,916
A great deal of our programme will be using the functionality

422
00:19:24,916 --> 00:19:27,512
provided by those built in classes.

423
00:19:27,512 --> 00:19:29,443
And we're going to be exploring a lot of

424
00:19:29,443 --> 00:19:30,953
that as we go through,

425
00:19:30,953 --> 00:19:34,212
but for now though let's move on to the next video.

