Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 Now in previous lessons we already started seeing Dart constructors and these are the bits of code that 1
we write inside our Dart classes in order to be able to create a object from the class. 2
So if our classes are the blueprints for our objects where we plan out what properties the eventual 3
object will have, what methods it will be able to perform, 4
then the constructor is the part of the code that actually turns this blueprint into the actual object. 5
So if it was the plans for a house, then the constructor would be the builders who actually take that 6
and turn it into the actual house. 7
Now we already saw with our previous code when we created the human class, that our class can have properties. 8
And these are simply just the instance variables that live inside the class. 9
And then we've got our methods which are essentially another word for functions that are inside a class. 10
And when I say inside a class, it basically means that the eventual object that gets created from this 11
class, 12
so in this case it would be a human object, 13
it would have these properties and it would be able to perform these methods. 14
So these methods and properties are actually associated with an object rather than just free-floating 15
around. 16
Now the third thing that we've already seen as well is the constructor. And this has a special bit of 17
syntax, but essentially it tells the class what we want to initialize the values of the properties such 18
as in this case, the height of the human object to be. 19
So when it actually comes to creating the object from this class blueprint, then effectively we're going 20
to be using that constructor to do it. 21
So we'll write some code that looks a bit like this. 22
We're creating an object that has the type of this class human, and the object is called Jenny and it's 23
created. 24
So the right hand side of that equation is using this particular constructor here where we start out 25
with the word human and then we give it an input of a starting height, which it's going to assign to 26
that height property. When we create the constructor, 27
it's simply just a bit of preplanning to tell so that when we eventually create the object, we can specify 28
which properties we want to initialize with a value at the point of creating the object. 29
Now this part of how constructors work 30
we've already seen. And we created our Jenny object or our James object using the Human class. We provided 31
a initial value for our height 32
property of this particular object. And then later on we could tap into that height property or change 33
that height property if we needed to. But this height property is associated with this object and that's 34
why we're able to get hold of it by writing the dot notation. But this height is different from this 35
height as we can see here because they're associated with different objects. 36
And the reason why they have these starting values is not because we gave it a starting value in our 37
class, because we could have done this right? And they would all start out with the value 20. But instead 38
we specified the value only when we constructed that object using the constructor here. And this makes 39
a lot of sense in a lot of cases not just because babies don't tend to be born with a default height, 40
but in a game for example, the user might not have a username value right? 41
We only have access to that username value 42
once the user actually inputs one. 43
So very often, you'll create classes that start out without a value. So height currently equals null. 44
And only when it initialize or when it's constructed, do we actually give it a value to assign to that 45
property. Now by default, even if we didn't create a constructor let's say I commented that out, 46
you also have a default constructor. So I could in fact say something like this right? 47
I could say that I'm creating a new Jenny object from the human class 48
and Jenny will still have a height property as will James, but that height property will be equal to whatever 49
this value will be, which currently is null. 50
So that's what we get printed here and we would have to modify that value using the object. 51
So we would have to say jenny.height is now equal to 15, in order to achieve the same thing. 52
And you can imagine if we had a lot of properties which we wanted an initial value for, then it would 53
take a lot of time to do this using these two steps. 54
That's why constructors are useful. 55
And this is also why by default, Dart will give us a free constructor. 56
So at the moment in my human class, you can see there are no constructors. But by default, Dart will give 57
you a constructor that looks something like this is. It's basically an empty constructor which doesn't take 58
any values, any input. 59
And it also doesn't do anything other than just create the human object from the human class. We don't 60
have to type that because it's always going to be there for us. Now at the moment, 61
we have this constructor so let's just uncomment it to put it back into the code. And we're using 62
this parameter called startingHeight, to be able to pass it in when we construct a new object from this class 63
and we set our property or height to the value of that input, that's startingHeight. 64
So that's why we're able to write startingHeight, and give it a value. 65
And this just reverts our code to what we had before. 66
And so Jenny has a starting height of 15 and James' starting height of 20, based on these constructors. 67
But what if we didn't want to call it startingHeight? 68
What if it made more sense to just call it height? 69
The input is called height and the property is called height. 70
So then we would set height to equal height. 71
And when we initialize the human Jenny, we say that we're creating a new human with a height of 15. 72
So this part of the code reads more naturally 73
right? 74
We're creating a new object of type Human, 75
it's called Jenny and it's created from the human class with a height of 15, instead of this starting 76
height business. 77
But now down here, it gets kind of confusing because well, which height is this one that you're setting 78
and which height is this one that you're setting it to 79
right? 80
Because we're trying to get the value of this one and assigning it to this one. 81
But is that this height or is that this height? 82
It's really confusing. 83
And in fact when you run it, the code gets confused too. Because we end up with null for both Jenny's 84
height and James' height because the code thinks that this height is actually talking about this one. 85
And so is this one. 86
So that means that this input, this variable here, is now equal to 15. 87
It's basically saying 15 should be equal to 15. 88
And you can confirm that by simply just printing height over here. 89
So that when Jenny and James get constructed, we should be seeing 15 and 20. 90
So let's hit run and you can see that we're getting 15 printed when we create our Jenny object, and 91
we're getting 20 printed when we're creating our James object. 92
But this particular height doesn't actually get any values assigned to it. 93
So this is not what we want. 94
And it also makes your code really hard to read. 95
So what's the way around this? 96
Well in Dart, we can use a keyword code this. So we can say this.height equals height. And if you come 97
from other programming languages such as JavaScript or Java, you might have seen this as well. 98
But basically when we start writing the word this, it refers to the eventual object that will be created 99
from this class. 100
So in this case it'll be Jenny 101
and in this case this will be James. And we're saying that set this eventual object height to this property, 102
to the height that comes through as an input. 103
So if we delete this print statement here and we go back to our height equals height, height is gonna 104
be set to itself. 105
So 15 will equal 15 which does nothing to affect this one. 106
And we know that when we run it, we get null for Jenny's height and null for James' height. 107
But as soon as we change this to this.height is equal to height, 108
it means that this human class's height property is going to be set to the height that gets passed in 109
from the constructor, which is right here and here. Then, 110
now if we had run, you can see it suddenly works and the code now knows what we're referring to. 111
When we're talking about this.height versus when we're just talking about height. So what if we also wanted 112
to give every human object a starting weight property as well? 113
So let's add another property in called 114
weight. 115
And it also has no starting values, 116
so we're not setting it equal to anything. 117
And then inside the human constructor, we're also adding the double that's called weight. 118
And then we're going to write this.weight equals weight. 119
So Jenny will have a starting weight of maybe 3.5 Kg let's say, and then we'll give James 120
a starting weight as well. 121
Maybe he'll be 4.2 Kg. And this works, 122
but you saw how much work I had to do in my constructor for this to be after to work. 123
I had to add it as an input parameter. 124
Then I had to write this.weight is equal to the weight, to specify that this property is going to 125
be equal to whatever gets passed in here. 126
And you can imagine if I had 10 properties to initialize or 20, then this is going to be really 127
time consuming. 128
So because this is such a common behavior, Dart actually has a little bit of syntactic sugar. 129
So something that they created to make this process easier for you. 130
So instead of doing all of this, we can simply delete everything that's in the curly braces including 131
the curly braces. And instead of writing 132
this is an input called height, 133
this is an input code weight, 134
we can simply write this.height and this.weight. 135
And we cap it off with a semicolon. 136
Now the same thing still applies when we're talking about these curly braces. 137
So if you have them there around these parameters, then it will be named when you construct human. 138
But if you delete these curly braces, then you can omit these named parameters and just initialize it 139
in the order that it was defined down here. 140
So I prefer it looking like this because otherwise it gets confusing when we have too many input parameters. 141
But this dramatically reduces the amount of code that you have to write in order to initialize some 142
of the properties of the class. 143
So this line of code does exactly the same thing as what all of this code does, 144
but it just cuts it down by quite a lot. And it looks a lot simpler. 145
Now it's time for your challenge. If you remember in our question class, we still have the same format 146
that we saw earlier in our DartPad. 147
So using what you learned just now, reformat this code 148
so that uses that syntactic sugar that Dart gives you to make the constructor for our question class 149
a lot shorter and a lot simpler. 150
So pause the video and try to complete this challenge. All right. 151
So this should be super simple. 152
Our inputs for our question class come in here and we're setting it to equal our properties up here 153
inside the constructor. 154
So instead of having all of this code, we can delete the entire body of the constructor and add a semicolon. 155
And inside the parentheses instead of having input and having the data type of the inputs, all we need 156
to write is this. and then the name of the property that we're going to initialize first, which is 157
questionText. And then it's going to be the second one that we're going to initialize this.question 158
Answer. And this will mean that your code in your QuizBrain when you're writing initialize a new question 159
object passing in the question text and the question answer, will still work exactly the same. 160
But now, we've just vastly simplified this constructor by using something from Dart that makes this 161
process a little bit less wordy. If you want to learn more about Dart constructors, then of course the 162
Dart language towards the place to go, and they talk about everything that we learned in this lesson 163
and even more things that you can do with it. 164
And of course there will be a link to this in the course resources.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.