Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Just like fields, properties can also have values set upon object creation.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
There's one more thing we need
to add to the invader class.
0:00
Invaders can't be invincible.
0:03
As towers shoot and hit them,
they need to be disabled somehow.
0:04
Often times, opponents in games
have a certain amount of health
0:09
that decreases as
the opponent sustains damage.
0:12
We can implement this same
idea in the invader class.
0:15
You know everything you need to know
in order to add this feature to
0:19
the invader class.
0:21
Think about what we'll need to add to
this class in order to implement this.
0:23
You'll probably need some
sort of variable to store
0:27
how much health the invader has remaining.
0:30
Invaders also need to start out
with a certain level of health.
0:33
We'll need to provide some way for
other classes, such as the tower class
0:37
to decrease the health of the invader
if they successfully hit it.
0:41
I suggest you pause the video and
take a moment to think about this and
0:44
even code up your own solution.
0:48
When you come back,
I'll show you how I would do it.
0:50
All right, here in our code
let's work through a solution.
0:56
First.
0:59
I think we should add
a member to the class for
1:00
storing how much health
the invader has remaining.
1:02
We need to decide if this should
be a property or a field and
1:05
what sort of accessibility it should have.
1:08
Maybe we should answer the question
of accessibility first.
1:11
Is it reasonable for
1:15
other classes to be able to see how
much health the invader has remaining?
1:16
I think so.
1:20
So, that means we should make it public.
1:21
Making it public answers our question of
whether to make it a property or a field.
1:24
I mentioned earlier that for a number
of reasons it's best to make public
1:28
member variables properties,
so we'll make this a property.
1:32
I'll call it Health.
1:36
Now, we need to decide what sort
of accessibility the getter and
1:38
the setter of the property should have.
1:41
I already mentioned the other classes
will want to see what the health of
1:43
the invader is.
1:47
So, we should make the getter public.
1:49
Since getters and setters of
a property are public by default,
1:51
we don't need to type public here.
1:55
What about the setter?
1:58
Should other classes be able to
change the health of the invader?
1:59
Well, that's sort of the point of
adding this in the first place, so
2:03
let's keep it public as well.
2:06
Now we need to give the health property
a starting value, otherwise it will
2:09
have a default value of zero, which would
make for a very short lived invader.
2:13
Now there are a couple of ways we
could give it an initial value.
2:18
We could assign it a value here
in the constructor like so and
2:21
give it a starting value of two.
2:25
Or we can just assign it
a value where we declared it.
2:28
In a case like this where the value is
a literal number that isn't passed into
2:32
the constructor, I prefer to
assign it here at the property.
2:36
Now we could actually improve
this interface a little more, and
2:41
encapsulate the health concept even more.
2:44
Let's provide a method
called decrease health.
2:48
That decreases the health
by a passed in factor.
2:50
School type public, void, decrease health.
2:54
And then the parameter is the factor
by which to decrease the health.
2:57
So make that an integer.
3:03
And we'll just say health equals factor.
3:08
Now it's obvious to all
users of this class
3:14
how to decrease the health of an invader.
3:16
Instead of a coder having to assume that
they should alter the health property
3:19
directly, there's a method
called DecreaseHealth.
3:23
This is an example of
self documenting code.
3:27
Self documenting code is code that's so
clear we don't have to write many comments
3:30
or other documentation that
describes how to use it.
3:35
This is usually accomplished
by a well thought out design,
3:39
good encapsulation practices, and
giving things clear descriptive names.
3:42
Now that we're providing
a method to change the health,
3:47
we should make the health
property's setter private
3:50
to make it clear to other coders
that they should use the method.
3:53
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up