Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,390 --> 00:00:07,170
Now in previous lessons we already started seeing Dart constructors and these are the bits of code that
1
2
00:00:07,170 --> 00:00:13,950
we write inside our Dart classes in order to be able to create a object from the class.
2
3
00:00:14,370 --> 00:00:20,370
So if our classes are the blueprints for our objects where we plan out what properties the eventual
3
4
00:00:20,370 --> 00:00:24,460
object will have, what methods it will be able to perform,
4
5
00:00:24,600 --> 00:00:32,690
then the constructor is the part of the code that actually turns this blueprint into the actual object.
5
6
00:00:32,700 --> 00:00:38,490
So if it was the plans for a house, then the constructor would be the builders who actually take that
6
7
00:00:38,790 --> 00:00:42,450
and turn it into the actual house.
7
8
00:00:42,930 --> 00:00:50,220
Now we already saw with our previous code when we created the human class, that our class can have properties.
8
9
00:00:50,610 --> 00:00:55,950
And these are simply just the instance variables that live inside the class.
9
10
00:00:55,950 --> 00:01:02,150
And then we've got our methods which are essentially another word for functions that are inside a class.
10
11
00:01:02,220 --> 00:01:08,520
And when I say inside a class, it basically means that the eventual object that gets created from this
11
12
00:01:08,520 --> 00:01:08,880
class,
12
13
00:01:09,420 --> 00:01:12,040
so in this case it would be a human object,
13
14
00:01:12,150 --> 00:01:16,890
it would have these properties and it would be able to perform these methods.
14
15
00:01:16,890 --> 00:01:23,910
So these methods and properties are actually associated with an object rather than just free-floating
15
16
00:01:23,910 --> 00:01:25,560
around.
16
17
00:01:25,560 --> 00:01:31,740
Now the third thing that we've already seen as well is the constructor. And this has a special bit of
17
18
00:01:31,740 --> 00:01:38,190
syntax, but essentially it tells the class what we want to initialize the values of the properties such
18
19
00:01:38,190 --> 00:01:41,840
as in this case, the height of the human object to be.
19
20
00:01:42,120 --> 00:01:49,430
So when it actually comes to creating the object from this class blueprint, then effectively we're going
20
21
00:01:49,430 --> 00:01:52,590
to be using that constructor to do it.
21
22
00:01:52,590 --> 00:01:56,010
So we'll write some code that looks a bit like this.
22
23
00:01:56,230 --> 00:02:03,500
We're creating an object that has the type of this class human, and the object is called Jenny and it's
23
24
00:02:03,510 --> 00:02:04,350
created.
24
25
00:02:04,350 --> 00:02:10,770
So the right hand side of that equation is using this particular constructor here where we start out
25
26
00:02:10,770 --> 00:02:17,160
with the word human and then we give it an input of a starting height, which it's going to assign to
26
27
00:02:17,190 --> 00:02:21,460
that height property. When we create the constructor,
27
28
00:02:21,460 --> 00:02:27,910
it's simply just a bit of preplanning to tell so that when we eventually create the object, we can specify
28
29
00:02:28,060 --> 00:02:36,190
which properties we want to initialize with a value at the point of creating the object.
29
30
00:02:36,190 --> 00:02:38,770
Now this part of how constructors work
30
31
00:02:38,770 --> 00:02:47,110
we've already seen. And we created our Jenny object or our James object using the Human class. We provided
31
32
00:02:47,200 --> 00:02:49,480
a initial value for our height
32
33
00:02:49,480 --> 00:02:56,410
property of this particular object. And then later on we could tap into that height property or change
33
34
00:02:56,410 --> 00:03:04,750
that height property if we needed to. But this height property is associated with this object and that's
34
35
00:03:04,750 --> 00:03:11,410
why we're able to get hold of it by writing the dot notation. But this height is different from this
35
36
00:03:11,410 --> 00:03:16,360
height as we can see here because they're associated with different objects.
36
37
00:03:16,600 --> 00:03:23,170
And the reason why they have these starting values is not because we gave it a starting value in our
37
38
00:03:23,170 --> 00:03:30,730
class, because we could have done this right? And they would all start out with the value 20. But instead
38
39
00:03:30,970 --> 00:03:39,160
we specified the value only when we constructed that object using the constructor here. And this makes
39
40
00:03:39,160 --> 00:03:44,890
a lot of sense in a lot of cases not just because babies don't tend to be born with a default height,
40
41
00:03:45,190 --> 00:03:50,630
but in a game for example, the user might not have a username value right?
41
42
00:03:50,700 --> 00:03:53,350
We only have access to that username value
42
43
00:03:53,350 --> 00:03:55,990
once the user actually inputs one.
43
44
00:03:55,990 --> 00:04:02,450
So very often, you'll create classes that start out without a value. So height currently equals null.
44
45
00:04:02,560 --> 00:04:09,250
And only when it initialize or when it's constructed, do we actually give it a value to assign to that
45
46
00:04:09,250 --> 00:04:16,720
property. Now by default, even if we didn't create a constructor let's say I commented that out,
46
47
00:04:16,810 --> 00:04:23,580
you also have a default constructor. So I could in fact say something like this right?
47
48
00:04:23,590 --> 00:04:28,600
I could say that I'm creating a new Jenny object from the human class
48
49
00:04:28,690 --> 00:04:35,620
and Jenny will still have a height property as will James, but that height property will be equal to whatever
49
50
00:04:35,620 --> 00:04:39,650
this value will be, which currently is null.
50
51
00:04:39,700 --> 00:04:46,430
So that's what we get printed here and we would have to modify that value using the object.
51
52
00:04:46,430 --> 00:04:54,340
So we would have to say jenny.height is now equal to 15, in order to achieve the same thing.
52
53
00:04:54,460 --> 00:05:00,010
And you can imagine if we had a lot of properties which we wanted an initial value for, then it would
53
54
00:05:00,010 --> 00:05:03,700
take a lot of time to do this using these two steps.
54
55
00:05:03,700 --> 00:05:05,950
That's why constructors are useful.
55
56
00:05:05,950 --> 00:05:11,720
And this is also why by default, Dart will give us a free constructor.
56
57
00:05:11,770 --> 00:05:18,910
So at the moment in my human class, you can see there are no constructors. But by default, Dart will give
57
58
00:05:18,910 --> 00:05:25,300
you a constructor that looks something like this is. It's basically an empty constructor which doesn't take
58
59
00:05:25,360 --> 00:05:27,370
any values, any input.
59
60
00:05:27,370 --> 00:05:34,060
And it also doesn't do anything other than just create the human object from the human class. We don't
60
61
00:05:34,060 --> 00:05:40,470
have to type that because it's always going to be there for us. Now at the moment,
61
62
00:05:40,540 --> 00:05:46,840
we have this constructor so let's just uncomment it to put it back into the code. And we're using
62
63
00:05:46,870 --> 00:05:53,650
this parameter called startingHeight, to be able to pass it in when we construct a new object from this class
63
64
00:05:54,070 --> 00:06:00,340
and we set our property or height to the value of that input, that's startingHeight.
64
65
00:06:00,730 --> 00:06:08,380
So that's why we're able to write startingHeight, and give it a value.
65
66
00:06:08,500 --> 00:06:11,790
And this just reverts our code to what we had before.
66
67
00:06:11,800 --> 00:06:19,240
And so Jenny has a starting height of 15 and James' starting height of 20, based on these constructors.
67
68
00:06:19,240 --> 00:06:22,110
But what if we didn't want to call it startingHeight?
68
69
00:06:22,120 --> 00:06:24,860
What if it made more sense to just call it height?
69
70
00:06:25,030 --> 00:06:27,790
The input is called height and the property is called height.
70
71
00:06:27,790 --> 00:06:30,490
So then we would set height to equal height.
71
72
00:06:30,490 --> 00:06:37,380
And when we initialize the human Jenny, we say that we're creating a new human with a height of 15.
72
73
00:06:37,420 --> 00:06:40,510
So this part of the code reads more naturally
73
74
00:06:40,510 --> 00:06:41,110
right?
74
75
00:06:41,140 --> 00:06:43,840
We're creating a new object of type Human,
75
76
00:06:43,840 --> 00:06:48,910
it's called Jenny and it's created from the human class with a height of 15, instead of this starting
76
77
00:06:48,910 --> 00:06:50,080
height business.
77
78
00:06:50,080 --> 00:06:56,770
But now down here, it gets kind of confusing because well, which height is this one that you're setting
78
79
00:06:56,860 --> 00:07:00,190
and which height is this one that you're setting it to
79
80
00:07:00,220 --> 00:07:00,490
right?
80
81
00:07:00,490 --> 00:07:04,900
Because we're trying to get the value of this one and assigning it to this one.
81
82
00:07:04,900 --> 00:07:08,200
But is that this height or is that this height?
82
83
00:07:08,230 --> 00:07:09,940
It's really confusing.
83
84
00:07:09,940 --> 00:07:17,110
And in fact when you run it, the code gets confused too. Because we end up with null for both Jenny's
84
85
00:07:17,110 --> 00:07:23,200
height and James' height because the code thinks that this height is actually talking about this one.
85
86
00:07:23,200 --> 00:07:24,640
And so is this one.
86
87
00:07:25,000 --> 00:07:31,840
So that means that this input, this variable here, is now equal to 15.
87
88
00:07:31,840 --> 00:07:34,670
It's basically saying 15 should be equal to 15.
88
89
00:07:34,870 --> 00:07:39,190
And you can confirm that by simply just printing height over here.
89
90
00:07:40,000 --> 00:07:47,100
So that when Jenny and James get constructed, we should be seeing 15 and 20.
90
91
00:07:47,200 --> 00:07:56,100
So let's hit run and you can see that we're getting 15 printed when we create our Jenny object, and
91
92
00:07:56,100 --> 00:08:00,010
we're getting 20 printed when we're creating our James object.
92
93
00:08:00,120 --> 00:08:05,400
But this particular height doesn't actually get any values assigned to it.
93
94
00:08:05,820 --> 00:08:07,560
So this is not what we want.
94
95
00:08:07,650 --> 00:08:10,990
And it also makes your code really hard to read.
95
96
00:08:11,010 --> 00:08:13,030
So what's the way around this?
96
97
00:08:13,050 --> 00:08:20,940
Well in Dart, we can use a keyword code this. So we can say this.height equals height. And if you come
97
98
00:08:20,940 --> 00:08:26,190
from other programming languages such as JavaScript or Java, you might have seen this as well.
98
99
00:08:26,730 --> 00:08:33,990
But basically when we start writing the word this, it refers to the eventual object that will be created
99
100
00:08:33,990 --> 00:08:34,860
from this class.
100
101
00:08:35,190 --> 00:08:37,440
So in this case it'll be Jenny
101
102
00:08:37,440 --> 00:08:46,200
and in this case this will be James. And we're saying that set this eventual object height to this property,
102
103
00:08:46,620 --> 00:08:50,310
to the height that comes through as an input.
103
104
00:08:50,310 --> 00:08:58,670
So if we delete this print statement here and we go back to our height equals height, height is gonna
104
105
00:08:58,680 --> 00:09:00,330
be set to itself.
105
106
00:09:00,330 --> 00:09:04,700
So 15 will equal 15 which does nothing to affect this one.
106
107
00:09:04,810 --> 00:09:10,500
And we know that when we run it, we get null for Jenny's height and null for James' height.
107
108
00:09:10,500 --> 00:09:14,970
But as soon as we change this to this.height is equal to height,
108
109
00:09:15,030 --> 00:09:22,110
it means that this human class's height property is going to be set to the height that gets passed in
109
110
00:09:22,380 --> 00:09:27,230
from the constructor, which is right here and here. Then,
110
111
00:09:27,240 --> 00:09:32,640
now if we had run, you can see it suddenly works and the code now knows what we're referring to.
111
112
00:09:32,640 --> 00:09:40,070
When we're talking about this.height versus when we're just talking about height. So what if we also wanted
112
113
00:09:40,130 --> 00:09:46,380
to give every human object a starting weight property as well?
113
114
00:09:46,520 --> 00:09:49,550
So let's add another property in called
114
115
00:09:49,580 --> 00:09:50,440
weight.
115
116
00:09:50,480 --> 00:09:52,630
And it also has no starting values,
116
117
00:09:52,640 --> 00:09:54,530
so we're not setting it equal to anything.
117
118
00:09:55,040 --> 00:10:01,030
And then inside the human constructor, we're also adding the double that's called weight.
118
119
00:10:01,100 --> 00:10:06,440
And then we're going to write this.weight equals weight.
119
120
00:10:06,440 --> 00:10:14,150
So Jenny will have a starting weight of maybe 3.5 Kg let's say, and then we'll give James
120
121
00:10:14,210 --> 00:10:16,720
a starting weight as well.
121
122
00:10:16,880 --> 00:10:22,490
Maybe he'll be 4.2 Kg. And this works,
122
123
00:10:22,490 --> 00:10:27,600
but you saw how much work I had to do in my constructor for this to be after to work.
123
124
00:10:27,620 --> 00:10:31,400
I had to add it as an input parameter.
124
125
00:10:31,610 --> 00:10:38,360
Then I had to write this.weight is equal to the weight, to specify that this property is going to
125
126
00:10:38,360 --> 00:10:40,630
be equal to whatever gets passed in here.
126
127
00:10:40,790 --> 00:10:47,690
And you can imagine if I had 10 properties to initialize or 20, then this is going to be really
127
128
00:10:47,750 --> 00:10:49,200
time consuming.
128
129
00:10:49,250 --> 00:10:55,370
So because this is such a common behavior, Dart actually has a little bit of syntactic sugar.
129
130
00:10:55,390 --> 00:10:59,510
So something that they created to make this process easier for you.
130
131
00:10:59,960 --> 00:11:08,300
So instead of doing all of this, we can simply delete everything that's in the curly braces including
131
132
00:11:08,300 --> 00:11:11,720
the curly braces. And instead of writing
132
133
00:11:11,720 --> 00:11:13,360
this is an input called height,
133
134
00:11:13,550 --> 00:11:15,050
this is an input code weight,
134
135
00:11:15,140 --> 00:11:20,060
we can simply write this.height and this.weight.
135
136
00:11:24,520 --> 00:11:27,730
And we cap it off with a semicolon.
136
137
00:11:27,730 --> 00:11:31,510
Now the same thing still applies when we're talking about these curly braces.
137
138
00:11:31,570 --> 00:11:38,110
So if you have them there around these parameters, then it will be named when you construct human.
138
139
00:11:38,110 --> 00:11:45,520
But if you delete these curly braces, then you can omit these named parameters and just initialize it
139
140
00:11:45,580 --> 00:11:48,170
in the order that it was defined down here.
140
141
00:11:48,580 --> 00:11:54,540
So I prefer it looking like this because otherwise it gets confusing when we have too many input parameters.
141
142
00:11:54,970 --> 00:12:01,030
But this dramatically reduces the amount of code that you have to write in order to initialize some
142
143
00:12:01,030 --> 00:12:02,910
of the properties of the class.
143
144
00:12:02,950 --> 00:12:10,510
So this line of code does exactly the same thing as what all of this code does,
144
145
00:12:10,510 --> 00:12:15,070
but it just cuts it down by quite a lot. And it looks a lot simpler.
145
146
00:12:16,500 --> 00:12:24,090
Now it's time for your challenge. If you remember in our question class, we still have the same format
146
147
00:12:24,120 --> 00:12:26,370
that we saw earlier in our DartPad.
147
148
00:12:26,400 --> 00:12:29,850
So using what you learned just now, reformat this code
148
149
00:12:29,880 --> 00:12:36,600
so that uses that syntactic sugar that Dart gives you to make the constructor for our question class
149
150
00:12:36,900 --> 00:12:38,850
a lot shorter and a lot simpler.
150
151
00:12:38,850 --> 00:12:43,510
So pause the video and try to complete this challenge. All right.
151
152
00:12:43,540 --> 00:12:45,760
So this should be super simple.
152
153
00:12:45,760 --> 00:12:52,630
Our inputs for our question class come in here and we're setting it to equal our properties up here
153
154
00:12:52,990 --> 00:12:54,940
inside the constructor.
154
155
00:12:54,940 --> 00:13:02,350
So instead of having all of this code, we can delete the entire body of the constructor and add a semicolon.
155
156
00:13:02,680 --> 00:13:09,040
And inside the parentheses instead of having input and having the data type of the inputs, all we need
156
157
00:13:09,040 --> 00:13:15,250
to write is this. and then the name of the property that we're going to initialize first, which is
157
158
00:13:15,250 --> 00:13:21,190
questionText. And then it's going to be the second one that we're going to initialize this.question
158
159
00:13:21,190 --> 00:13:28,570
Answer. And this will mean that your code in your QuizBrain when you're writing initialize a new question
159
160
00:13:28,570 --> 00:13:35,430
object passing in the question text and the question answer, will still work exactly the same.
160
161
00:13:35,530 --> 00:13:42,370
But now, we've just vastly simplified this constructor by using something from Dart that makes this
161
162
00:13:42,370 --> 00:13:49,110
process a little bit less wordy. If you want to learn more about Dart constructors, then of course the
162
163
00:13:49,110 --> 00:13:54,720
Dart language towards the place to go, and they talk about everything that we learned in this lesson
163
164
00:13:55,050 --> 00:13:58,100
and even more things that you can do with it.
164
165
00:13:58,290 --> 00:14:01,110
And of course there will be a link to this in the course resources.
18924
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.