Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,650 --> 00:00:03,740
In this lesson, you will add setters to the car class.
2
00:00:06,510 --> 00:00:13,200
Currently we have no way of updating an object as field because they were made to be private to the
3
00:00:13,200 --> 00:00:14,940
class that they were defined in.
4
00:00:14,940 --> 00:00:18,600
So that's why we need to add some public setters.
5
00:00:19,140 --> 00:00:20,820
So here we say public.
6
00:00:21,590 --> 00:00:22,400
Void.
7
00:00:23,030 --> 00:00:27,770
And a setter is always going to be called set followed by the field name.
8
00:00:28,860 --> 00:00:34,740
A Sutter does expect to receive a parameter whose type depends on the field itself.
9
00:00:34,740 --> 00:00:37,590
So this is going to receive a make value.
10
00:00:38,250 --> 00:00:39,720
And it's going to set the field.
11
00:00:39,720 --> 00:00:45,570
And the current object that's calling this setter, this dot make equal to the make value that gets
12
00:00:45,570 --> 00:00:46,170
passed in.
13
00:00:47,280 --> 00:00:53,850
So notice here we do need the this keyword to distinguish between the field and the parameter that gets
14
00:00:53,850 --> 00:00:54,580
passed in.
15
00:00:54,600 --> 00:00:57,150
Whereas in the getter there are no parameters.
16
00:00:57,150 --> 00:01:00,630
So make will automatically refer to the object as field.
17
00:01:01,560 --> 00:01:02,240
All right.
18
00:01:02,240 --> 00:01:06,260
So now we will add another setter that is available to the public.
19
00:01:06,290 --> 00:01:08,540
This one will be called set price.
20
00:01:08,810 --> 00:01:14,150
It's going to receive a double price, the same type as your price field.
21
00:01:14,180 --> 00:01:20,990
And when this setter gets called, it's going to set the price field of the current object equal to
22
00:01:20,990 --> 00:01:23,000
the price value that gets passed in.
23
00:01:23,450 --> 00:01:23,980
All right.
24
00:01:23,990 --> 00:01:27,380
Now we're going to need another setter, public void.
25
00:01:27,410 --> 00:01:29,630
This one will be called set year.
26
00:01:30,080 --> 00:01:32,540
And it's going to receive a year value.
27
00:01:33,500 --> 00:01:39,590
We can reference the current object that calls this method using the this keyword, and we can set its
28
00:01:39,590 --> 00:01:43,370
year field equal to the year value that gets passed in.
29
00:01:43,820 --> 00:01:45,920
Finally, we need one more setter.
30
00:01:45,950 --> 00:01:47,970
Here we'll say public void.
31
00:01:47,990 --> 00:01:55,700
This setter will be called set color and its responsibility will be to update the color field with whatever
32
00:01:55,700 --> 00:01:57,680
color value gets passed in.
33
00:01:59,520 --> 00:02:02,970
And the current object that calls this setter.
34
00:02:03,000 --> 00:02:08,910
We're going to set its color field equal to whatever color value gets passed in.
35
00:02:08,910 --> 00:02:10,500
And that should be all.
36
00:02:11,850 --> 00:02:17,190
And now, even though our fields are private, we have public gutters to return the fields as value
37
00:02:17,190 --> 00:02:19,680
and we have public setters to update them.
38
00:02:20,780 --> 00:02:26,510
So here what I'll do before printing the objects is Fields will update the color of this object.
39
00:02:26,510 --> 00:02:33,140
So here we'll say Nissan set color, and we're going to update its color to jet black.
40
00:02:33,650 --> 00:02:36,470
And we'll do the same thing for this object.
41
00:02:36,470 --> 00:02:43,100
We will say Dodge dot, set color, and we're going to give it a color of jet black as well.
42
00:02:44,030 --> 00:02:44,800
All right.
43
00:02:44,810 --> 00:02:45,620
And you know what?
44
00:02:45,620 --> 00:02:48,740
Let's just say the dealership is having a 50% off sale.
45
00:02:48,770 --> 00:02:56,120
What we'll do is we'll say Nissan DOT set price and we're going to set its price field equal to the
46
00:02:56,120 --> 00:02:57,380
current price.
47
00:02:58,430 --> 00:02:59,780
Divided by two.
48
00:03:00,510 --> 00:03:01,680
You can do the same thing here.
49
00:03:01,710 --> 00:03:03,960
Dodge Dot Set price.
50
00:03:04,820 --> 00:03:10,040
That's going to equal the current price of the Dodge Object divided by two.
51
00:03:11,140 --> 00:03:12,010
All right.
52
00:03:12,020 --> 00:03:16,550
I'm going to need break points in set color, set price and get price.
53
00:03:25,750 --> 00:03:29,140
I'll put four breakpoints here and we're good to go.
54
00:03:32,810 --> 00:03:35,710
So at this point we have two car objects.
55
00:03:35,720 --> 00:03:40,490
The first thing we'll do is from this object, we're going to call set color.
56
00:03:41,900 --> 00:03:46,640
So here this points to the current object that called this method.
57
00:03:46,640 --> 00:03:53,540
And here we're setting this color equal to the color value that was passed in jet black.
58
00:03:58,640 --> 00:03:59,590
All right.
59
00:03:59,600 --> 00:04:05,450
Now we're calling set color from this object, the object with a reference of 11.
60
00:04:05,660 --> 00:04:07,100
We can step inside.
61
00:04:07,990 --> 00:04:11,860
This points to the current object called this method.
62
00:04:12,370 --> 00:04:14,770
And here we're setting this color.
63
00:04:15,680 --> 00:04:19,490
Equal to the color value that was passed in jet black.
64
00:04:21,029 --> 00:04:21,990
All right.
65
00:04:22,700 --> 00:04:26,930
So at this point, we've updated both of our objects as color fields.
66
00:04:26,960 --> 00:04:32,480
And now here, Nissan is going to update its price field by first getting its current price.
67
00:04:33,050 --> 00:04:37,490
So get price is going to return the price field of the current object.
68
00:04:38,830 --> 00:04:44,290
So 10,000 divided by two means we're passing 5000 into set price.
69
00:04:46,760 --> 00:04:55,010
This points to the current object that's calling set price, and we're setting this price equal to 5000.
70
00:04:56,960 --> 00:04:57,800
All right.
71
00:04:57,810 --> 00:04:59,610
Going to the next breakpoint.
72
00:05:01,250 --> 00:05:01,640
Here.
73
00:05:01,640 --> 00:05:06,350
We're setting the price for this object by getting its current price divided by two.
74
00:05:06,380 --> 00:05:08,600
So first, we're going to go to get price.
75
00:05:09,080 --> 00:05:14,210
Get price is going to return the price value of the current object that's calling it 11,000.
76
00:05:15,800 --> 00:05:18,080
The value returned is 11,000.
77
00:05:18,080 --> 00:05:21,170
That divided by two is 5500.
78
00:05:21,200 --> 00:05:26,960
That's what's going to get passed into set price when an object with a reference of 11 calls it.
79
00:05:28,720 --> 00:05:32,830
This points to the current object that called set price.
80
00:05:32,830 --> 00:05:35,300
And here we're setting this price.
81
00:05:35,320 --> 00:05:39,910
The current object is price field equal to 5500.
82
00:05:42,810 --> 00:05:48,090
So at this point, we have fully updated both objects, the object with a reference of ten.
83
00:05:48,120 --> 00:05:50,200
The object with a reference of 11.
84
00:05:50,220 --> 00:05:52,220
And here we're just printing all of the fields.
85
00:05:52,230 --> 00:05:56,910
I don't want to step inside of get price again, so let me just remove all of my breakpoints.
86
00:05:58,260 --> 00:05:59,700
And we are done.
87
00:06:02,120 --> 00:06:04,220
So this recap is fairly simple.
88
00:06:04,220 --> 00:06:07,670
You should use setters to update your objects as fields.
7952
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.