(intro music)
The next concept that it's time
to talk about is Classes.
Now classes are fundamental part
of object oriented programming.
Now we do some classes with the previous videos
and that's because string and int are classes.
So think of a class as some sort of thing in an application.
Now effectively, they are nouns within an application.
Like, for example a player or an enemy or a train maybe.
Now, using computer game analogies,
these could all be classes in a game that we've written.
So, we're going to go through the process
of creating a class so that you can get a good handle
on why you'd want to use a class.
Now, the Android framework contains a load of classes,
and a lot of the work in writing
Android programmes is already done for us.
And all the widgets that we add to our apps are classes.
The text view widget that displays so it displayed
hollow word in the last section
was an object created from the text view class.
So, we didn't have to a lot to display the text,
because the Android framework took care
of everything needed to centre the widget
and ultimately display the text.
So, once we've seen how
to write a class, we'll look at how to use them.
Now, as an example of some classes we're gonna create
some basic ones that you can use in an adventure game.
Now, we're not gonna be producing a full game,
but we use some of the classes that a game may use just
to look at the concept of classes.
Now, to create a class we need to make sure
that the project pane is actually expanded as it is
on the right hand side and you need to come over here
to the java folder, right click that,
select new, and select Kotlin file slash class.
Now you're gonna type in player for the name of the class.
(keys typing)
So, we're creating a class called player,
which is going to compute to
have some basic information about player in a game.
Now the convention is that class
then start with a capital P,
and you saw that I used a capital P for the word player,
so make sure to use that capital P.
Now, in the Kind of drop down here, if I click on that now,
you want to choose class here.
Now, it's not that important, you can leave it set
to file if you want, but doing it this way,
changing it and selecting class
can save you a bit of typing.
So click on Ok, and you can see
that Android Studios automatically created
the basic declaration for a new class.
Now, there's not a lot of code in there,
so if you changed in file a moment ago,
you wouldn't have a lot of code to type in.
You'd have to just type in what you see on the screen there.
So, the good thing with a class is it can be a container
for lots of variables, and it can also have programme code
that relates just to a player.
Now, we're gonna go through and explore
that a little bit further.
What we'll start off doing is setting up some variables
that would be appropriate if
this was a player class in a game.
Alright, so what are some things
that maybe a player in a game would have?
Well they're gonna need a name to start with.
We're not gonna have any players without names,
so whenever we create new players,
we're gonna have to give them a name.
Now in Kotlin we can specify the name
in what's called the primary constructor.
Now, there are a couple of ways of doing that,
but the most common is to add the variable
to the class declaration.
So, we're gonna have a go at doing
that so at line 5 we've got class Player,
and we're gonna come over here to the end of the word player
add a parentheses and in there we're gonna type
val space name colon and the word string.
So what we've done there is to tell Kotlin
that a name must be supplied
whenever new player objects are created.
And we're going to be seeing how to create them shortly.
Yeah we also need to keep track of
how many lives a player has as well as
their score and the level they've reached in the game.
So we need a few more variables.
So let's go ahead and create those as well.
And we're gonna put those in the class
starting on line 6 as you can see here.
We're gonna type Var space lives equals three.
Var level equals one and Var score equals zero.
Alright so that's the basic class created.
Now, it doesn't do a lot at the moment,
but it'll have a name when we create it
as well as default values for
the number of lives, the level, and the score.
Now, players start off with three lives and they'll start
at level one, and their score will actually be zero.
Notice that I've declared those
three variables using var rather than val.
Now the name won't change after we create the player,
so that's declared to be val.
We expect the score and the level to increase
and they player also may lose lives,
so consequently these variables have to be vars.
Now, another thing to notice is the way
Kotlin can infer the types of variables a lot of the time.
And we have talked about that before.
Now we've assigned integer values to
lives, level, and score, so we don't have
to specify their type.
Now, we haven't assigned any value to name yet,
so we have to tell Kotlin that's going to hold the string.
And that's why we've used the string type here on line five.
Alright, let's create some players
and see what we can do with our basic player class.
So we're gonna switch back to Main.kt,
so we're gonna click back up here in the tab
and if for some reason that's not open
you can double click main.kt over here to open it up.
And what we're gonna do is delete all this other code,
completely right up to, but not including line one.
So, I've just got the first line fun main
and nothing in between the code block
on the block of code from lines two and onwards.
Alright so how do we actually create a player called Tim?
Well we can actually type val tim equals Player,
then double quotes Tim.
And you can type println tim dot name.
Println tim dot lives.
Println tim dot level.
And Println tim dot score.
And by the way, ignore this name colon
that's appeared before tim.
I'll come back to that soon and explain what it is.
It's not part of the code,
so you don't actually have to type that in.
Alright, so on line two we've
created a new instance of the player class.
Now that's an important concept to understand,
the difference between the class
and instances of that class.
Now, one way to look at it is to consider
the classification of life on this planet.
Now all life forms fit into different classifications,
and we're all members of a class called humans.
Now, we weren't all created from a template
that defines a human,
and we're all individual instances of human.
So my name property has the value tim,
but yours probably has a different value.
Now, of course people can share the same name,
but if there's another Tim Buchalka instance out there
they'll have different values for other properties
such as their height or hair colour or whatever.
So the class is called human
and we're all instances of the human class.
And that's really the difference between the two things.
So the class is just a template and the values
for these properties will be assigned
when we create instances of the player class.
So let's actually run this programme and see what happens.
(mouse click)
Okay, so we can see the values of
the properties printed out over here.
Now, we used dot notation to refer
to the various properties that Tim has.
So, on line three Tim dot name refers to the name property.
And then the other ones Tim dot lives
refers to the lives property and so on.
Now as well as properties, classes can also have behaviour.
And we're gonna change some of the properties
and it's gonna get a bit tedious typing all of
those println statements in,
and it'd be really handy if player instances
were able to display these values.
So, one way to do this is to add a
show function to the player class.
So, let's go back to the player class file and do that.
So we're gonna come down here, and we're going
to leave a gap between the
var score line and the start of the function.
Type fun show parentheses, then put a code block,
then we're actually gonna type println,
then we're going to do parentheses,
then we're gonna do three double quotes.
Three like that, then we're gonna press enter.
And you'll make a space there, then you're gonna type
name colon dollar name, lives dollar lives,
the next line level colon dollar level,
and then the last line score colon dollar score.
Then for the ending parentheses
I'm gonna do three double quotes again.
One, two, three.
So the show function is quite simple.
It just prints the values of the properties.
Now, I've used triple speech marks with
double quotes to format the values,
which we haven't seen before.
Now, using these triple double quotes like this
is a way to split a string over several lines,
and you'll see the effect
that it actually has when we run the programme.
So we'll go back to main.kt, and you can now
remove all these println method calls now
because we don't need them anymore.
So let's now move these println statements,
the four of them.
Actually what I'll do is I'll comment them out
and I'm gonna call the Tim show function instead.
So I'm gonna do Tim dot show.
So let's actually run this.
And you can see we've got
name, tim, lives, level, and score.
So we used dot notation to call functions on class instances
as well as to access the properties.
So, here tim dot show that we are using on line seven,
that calls the show function on the variable tim.
And if you recall tim's an instance of the player class,
and all player instances will have a show function.
Now you can see that by creating another player instance.
So I'm gonna call this one Louise.
I'm gonna type val louise equals player
capital P parentheses louise.
Then I'm gonna type louise dot level
equals five then you'll type louise dot show.
So we provide the name louise when we create the new player
instance and I've also set louise's level to five.
So if we run the programme now, we should get
the details of Louise printed out after mine, after tim.
And you can see we've got name tim
lives three level one score zero,
and then louise lives three level five,
this time, and score zero.
So each player will have a different name and their scores
and these other values will also be different.
Now we're storing these values as properties in the class
and each instance of the player class
will have its own values for those properties.
Now the code's slightly untidy because
louise was created with a default level
of one then we changed it on the next line to five.
It'd actually be neater to specify her level
when we created the louise instance of player.
Now, in java you have different constructors for class.
So, you could do something like this.
Go back up to louise and we could put
something like comma five and then delete this line.
Now we've got an error because
that class isn't set up to accept a level when we create it.
Now as I said in Java, you create different constructors
that will accept different arguments
when creating the class.
Now let's also possible on kotlin,
but kotlin allows default values for arguments.
Now, you'll see Java classes with different constructors
when we start using the Android framework classes.
So don't worry too much about them at this stage,
they're just Java's way of doing
what we're about to do in kotlin.
So what we can do is change our player class
to allow the number of lives and the level to be specified
when we create the class instances.
So let's actually go back to player.kt
and up here on the line, line five,
we're going to put a comma after the string,
comma space var level column space int equals one.
Now, because we've now declared the level properly
in the constructor we can
delete its declaration inside the class.
In fact, we have to do that,
you can't declare the same thing twice
and you can see we actually
got an error here because of that, conflicting declarations.
So, let's go ahead and delete that.
And the errors then disappear.
So, now that we've actually done that,
we can provide a value for level when we create louise,
so the error in main should have been fixed.
So if we go back to that now,
you can see the error fixed itself automatically
and if we run the programme now...
We should see that level has been set to five,
which it has for louise
and that's because we set the level to be five.
In the case of tim, level is set to one
because that's the default value.
So now in other words because we haven't provided a value
for tim on line two the default value of one is used.
So in other words tim level stays at level one.
And you may have noticed when I switched
back to the main function that line nine changed
and you saw level actually appear.
So, the value of five now has the word level in front of it.
And as I mentioned previously,
that's not part of the code,
that's Android studio showing us
what the various arguments represent.
So louise is the value we're providing for the name,
and five's the value for the level.
Now, that's quite a neat feature and it's very helpful
to have a reminder of what the arguments are.
It can be confusing when
you've watched the code on a video though.
You don't have to type the name colon and level colon here,
they're actually put on the screen automatically
by Android Studio.
Now, if you're using the default colour scheme,
the argument name's hints appear in a light grey,
with a light background as you can see here.
But, in the dark colour scheme, if you're running that,
they're the same, but the background's a bit harder to see,
against dark color's black background.
So remember that the display by Android studio,
to remind you what the arguments are,
they're not part of the code
and you shouldn't be typing them in.
Alright, before I show you another way
to specify the values for these arguments,
It's actually time for challenge.
So your challenge is to modify the player class
so that its constructor allows different values
for the lives and score properties.
Now the defaults for both will be the defaults
that the class is currently using.
When you've made the changes,
create two new instances of the player class.
And You can call them anything you like.
So, the first instance should
have a level of four and eight lives.
The second one should have a level of two, five lives,
and a score of 1000.
Use the show function to print out the values
for your new instances.
And note, when you add parameters that have default values,
they must be listed after any parameters that don't.
So name must be the first parameter that's declared
in the player class.
And that's why I added level after it.
So, that's the challenge pause the video now,
see how you go, and I'll see you when you get back,
and I'll show you the solution.
Pause the video now.
Alright so to allow the number of lives
and the score to be set when we create new player instances
we need to add two more parameters to the class declaration.
Now, because that results in duplicate declarations
for the two properties,
we also have to delete them from inside the class.
So, let's go have a look at doing that.
Go to the player class, and we're gonna start by
leaving val name string there
and also leave the level as it is.
After the int equals one for level,
we're gonna put a comma var lives colon int equals three
comma and var score colon int equals zero.
Right, now we need to delete these two lines,
the var lives equals three and var score equals zero,
because we've defined them now as arguments
in the class definition on line five.
And that should fix the errors,
which you can see that they have been fixed.
So that's to change to our player class.
Now that we've done that
we can create two new player objects
and specify their lives and score.
Now, I use the term objects there instead of instances.
They are actually the same thing.
You'll hear people talk
about instances of a class or objects.
An object is just an instance of a class,
so the two terms are used interchangeably.
Alright so let's go back to the main.kt
and let's add this extra, two extra instances.
So start with val space gr8 equals player parentheses
gr8 comma space comma four comma eight,
then val one two watch equals player,
and we'll use the name Ace
comma two comma five comma 1000.
Then let's actually show theirs as well,
so gr8 dot show and then one two watch dot show.
So let's now run the code.
And you can see we've got the output now for the other two,
the tim then the louise, but also for the gr8,
lives is eight and levels is four,
and Ace lives five level two score 1000.
And you may have noticed when I was typing the arguments
that Android Studio showed the name
of the argument that corresponds to the position
where I'm typing the numbers.
Now you may have wondered what use these
argument names hints were when I mentioned them before,
but now that we've got three numbers in the row for
one two watch you can see that they're useful.
Without those hints you'd have to remember
what order you defined the parameters in the player class,
but now we can clearly see that five is the number
of lives and 1000 is the score.
This is the code on line 13.
And as I said we ran the programme
and we got the correct results.
Now, by the way, setting the score like this would be useful
if we were storing a saved game for these two players,
and having to create them with the values they had
when they saved the game, in case you're wondering.
Now, I've used modern text speak style names here,
just to show that you have...
You can have numbers in variable names.
So variable names mustn't start with a number,
but you can use numbers within the names of variables.
Alright, so that's been a
quick overview of what classes are.
Now you'll find as you go through the course
that Android has a lot of classes built into it.
A great deal of our programme will be using the functionality
provided by those built in classes.
And we're going to be exploring a lot of
that as we go through,
but for now though let's move on to the next video.