WEBVTT

1
00:00:00.400 --> 00:00:05.070
When it comes to fields, the rule to code
by is to make all fields private, and

2
00:00:05.070 --> 00:00:09.330
then write public getters and
setters that access it indirectly.

3
00:00:09.330 --> 00:00:13.100
We just learned how to write accessor
methods in the previous video.

4
00:00:14.110 --> 00:00:17.910
You can imagine this forces you to
write a lot of additional code.

5
00:00:17.910 --> 00:00:20.030
Two methods for every field.

6
00:00:20.030 --> 00:00:24.700
Fortunately, C# has some syntactic sugar
that makes it much easier to write

7
00:00:24.700 --> 00:00:26.090
accessors like these.

8
00:00:26.090 --> 00:00:27.070
It's called a property.

9
00:00:28.430 --> 00:00:30.790
We can replace these two
methods with the property.

10
00:00:30.790 --> 00:00:33.440
I'll write the property down here so

11
00:00:33.440 --> 00:00:35.799
we can see the differences
between methods and properties.

12
00:00:37.130 --> 00:00:39.500
First, we give the property
an access modifier.

13
00:00:39.500 --> 00:00:41.110
I'll make it public.

14
00:00:41.110 --> 00:00:42.220
Then we give it it a type.

15
00:00:42.220 --> 00:00:44.234
The type should match
the type of the field.

16
00:00:48.462 --> 00:00:50.200
Now we give it the name.

17
00:00:50.200 --> 00:00:51.220
I'll name it location.

18
00:00:52.970 --> 00:00:57.570
Inside the curly braces,
we'll write our getter and our setter.

19
00:00:57.570 --> 00:01:01.336
The getter will do exactly what
the get location method up here did,

20
00:01:01.336 --> 00:01:03.067
it will just return the field.

21
00:01:06.867 --> 00:01:10.064
The setter will do exactly
what the set location did,

22
00:01:10.064 --> 00:01:11.990
it will set the location field.

23
00:01:14.660 --> 00:01:19.000
Properties have the same benefits that
we got from having these two methods.

24
00:01:19.000 --> 00:01:21.150
They have a getter and a setter.

25
00:01:22.720 --> 00:01:24.440
They correspond to these two methods.

26
00:01:25.560 --> 00:01:29.150
In the case of the setter,
the value parameter is implied.

27
00:01:29.150 --> 00:01:33.150
Value is just a variable that stores
the value being assigned to the field.

28
00:01:34.520 --> 00:01:37.750
You can think of these
as two separate methods.

29
00:01:37.750 --> 00:01:39.500
In fact, behind the scenes,

30
00:01:39.500 --> 00:01:43.130
properties look identical to these
two methods we wrote up here.

31
00:01:43.130 --> 00:01:44.860
They're used differently, though.

32
00:01:44.860 --> 00:01:48.163
Let's go to main and compare using
methods with using properties.

33
00:01:49.624 --> 00:01:51.437
Let's clear out this code first.

34
00:01:53.722 --> 00:01:55.450
Now let's make an invader.

35
00:01:55.450 --> 00:01:56.420
So let's say Invader.

36
00:01:58.664 --> 00:02:04.120
Equals Invader.

37
00:02:04.120 --> 00:02:05.590
Let's also make a new map location.

38
00:02:06.610 --> 00:02:13.410
So map location, I just call it location,
equals new map location

39
00:02:14.410 --> 00:02:19.140
and pass in that would be at 0,
0 and pass in the map.

40
00:02:22.030 --> 00:02:22.555
There we go.

41
00:02:22.555 --> 00:02:31.900
[BLANK] So

42
00:02:31.900 --> 00:02:35.740
to use the invaders Set Location Method
to set the location,

43
00:02:35.740 --> 00:02:39.140
we call the Set Location Method and
pass in this location.

44
00:02:41.260 --> 00:02:45.900
When using a property instead,
we can just assign location a new value.

45
00:02:47.550 --> 00:02:53.390
So we can just say
invader.location = location.

46
00:02:53.390 --> 00:02:56.780
This is not setting the location
field directly, though.

47
00:02:56.780 --> 00:03:01.890
Instead, it's calling the setter inside
the location property, right here.

48
00:03:03.330 --> 00:03:05.510
We can even add additional code here.

49
00:03:05.510 --> 00:03:10.560
So say we wanted to print something
every time the location changed.

50
00:03:10.560 --> 00:03:16.608
So we could say, system.console.rightline.

51
00:03:16.608 --> 00:03:19.772
Location changed.

52
00:03:19.772 --> 00:03:22.350
So the same goes for getters.

53
00:03:22.350 --> 00:03:25.730
We can call the property getter in
the same way we would read a field.

54
00:03:27.540 --> 00:03:31.750
So we could say location
= invader.location.

55
00:03:31.750 --> 00:03:37.620
So this gets the location
from the invader and

56
00:03:37.620 --> 00:03:40.200
sets it back into the local
variable location.

57
00:03:42.180 --> 00:03:46.060
Not very practical, but this is just for
demonstration purposes.

58
00:03:46.060 --> 00:03:49.480
So now that we have a property we
can delete these two methods here.

59
00:03:51.470 --> 00:03:54.610
And we don't need to do anything in the
setter other than just set the location.

60
00:03:57.130 --> 00:03:58.670
Pretty nifty.

61
00:03:58.670 --> 00:03:59.490
As you can see,

62
00:03:59.490 --> 00:04:04.130
properties support the encapsulation
principles of object oriented programming.

63
00:04:04.130 --> 00:04:07.590
They allow us to hide how
an invader's location is represented

64
00:04:07.590 --> 00:04:09.440
in the class internally.

65
00:04:09.440 --> 00:04:12.170
This allows us to make
changes to the class without

66
00:04:12.170 --> 00:04:13.330
affecting the users of the class.

67
00:04:14.460 --> 00:04:18.610
We'll see many more examples of how
to use properties as we go forward.

68
00:04:18.610 --> 00:04:22.170
We'll also learn how to use properties
to do even better encapsulation.
