All language subtitles for 012 Constructor Functions_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian Download
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,760 --> 00:00:05,710 In the last section, we used our person class to create a new instance of a person which we assigned 2 00:00:05,710 --> 00:00:09,500 to the variable lowercase P person then on the line. 3 00:00:09,520 --> 00:00:15,370 After that, we set a value to the first name property on that person instance. 4 00:00:16,390 --> 00:00:21,910 At the very end of that section, we had said that it sure seems kind of awkward to have to first declare 5 00:00:21,910 --> 00:00:25,590 the new instance of the person and then set the first name property after that. 6 00:00:25,990 --> 00:00:31,090 It sure seems to me like every person who exists inside of our application should always have a first 7 00:00:31,090 --> 00:00:32,140 name associated with them. 8 00:00:32,560 --> 00:00:37,120 So in this section, we're going to add a little bit of code to make sure that anyone who wants to use 9 00:00:37,120 --> 00:00:43,660 this person class right here has to provide a first name property to the class the instant that the 10 00:00:43,660 --> 00:00:44,770 class is created. 11 00:00:44,890 --> 00:00:47,680 So the instant we call a new person right here. 12 00:00:48,820 --> 00:00:55,030 To do this kind of required setup or required configuration of our class, whenever we create a new 13 00:00:55,030 --> 00:00:59,110 instance of it, we're going to add something called a constructor function. 14 00:00:59,890 --> 00:01:05,850 A constructor function is going to be another function or method added on to this class right here. 15 00:01:06,550 --> 00:01:12,730 This constructor function gets executed automatically any time we create a new instance of the person 16 00:01:12,730 --> 00:01:13,290 class. 17 00:01:13,600 --> 00:01:19,990 And so it makes it a perfect location to do a little bit of initial setup or initialization of our class. 18 00:01:20,850 --> 00:01:24,370 So to just kind of put this into diagram format, here's what's going to go on. 19 00:01:25,030 --> 00:01:31,270 Any time you and I create a new instance of a class, the constructor function is going to be called 20 00:01:31,420 --> 00:01:33,430 with any arguments that we provided. 21 00:01:33,430 --> 00:01:39,010 When we try to create a new instance, that constructor function will then be executed and it's going 22 00:01:39,010 --> 00:01:40,840 to do some initial setup for us. 23 00:01:41,230 --> 00:01:46,900 And then after all, that runs our new instance of the class, then gets returned and then assigned 24 00:01:46,930 --> 00:01:48,610 to this person variable over here. 25 00:01:49,210 --> 00:01:50,200 So let's get to it. 26 00:01:50,200 --> 00:01:53,590 Let's define the constructor on our person class. 27 00:01:55,130 --> 00:02:00,520 To define a constructor, I'm going to add a little bit of space here underneath our field of first 28 00:02:00,520 --> 00:02:05,130 name, and then I'm going to define a new function with a very special name. 29 00:02:05,680 --> 00:02:09,490 So the function name is going to be a person with a capital P like. 30 00:02:09,490 --> 00:02:16,870 So so notice how the name of this function right here is absolutely identical to the name of the class, 31 00:02:17,710 --> 00:02:22,240 because these two names are completely identical in case and spelling. 32 00:02:22,250 --> 00:02:23,830 So they both have a capital P. 33 00:02:24,550 --> 00:02:28,270 This person function right here will be used as the constructor function. 34 00:02:28,630 --> 00:02:33,910 And so this function will be called automatically any time we try to create a new instance of a person. 35 00:02:33,940 --> 00:02:39,190 And again, that makes it a perfect location to do some initial configuration of our instance. 36 00:02:40,770 --> 00:02:46,010 Now, as we were saying just a moment ago, we kind of want to add on a requirement to our application. 37 00:02:46,410 --> 00:02:52,470 We want to say that any time you try to create a person instance, you have to immediately provide a 38 00:02:52,470 --> 00:02:53,100 first name. 39 00:02:53,520 --> 00:02:58,140 In other words, we should not have any people inside of our application who do not have a first name. 40 00:02:58,840 --> 00:03:05,280 So to add in that requirement, we can specify an argument inside this constructor functions argument 41 00:03:05,280 --> 00:03:08,070 list or the set of parentheses right here. 42 00:03:09,840 --> 00:03:14,640 So inside the set of parentheses, I'm going to write out a name like so. 43 00:03:16,020 --> 00:03:20,920 When as soon as I type that in, you'll notice that we get an error appearing on the right hand side 44 00:03:20,920 --> 00:03:26,740 over here and if you click on it, it highlights where we are trying to create that new person instance 45 00:03:26,740 --> 00:03:27,190 up here. 46 00:03:28,140 --> 00:03:33,360 So as soon as we start to add in an argument to a function that means that anywhere we are calling that 47 00:03:33,360 --> 00:03:38,760 function from needs to also provide the correct number and type of arguments as well. 48 00:03:39,850 --> 00:03:44,530 So in order to make sure that every person gets created with a first name, we just had to add in the 49 00:03:44,530 --> 00:03:49,030 argument right here and then, poof, everything else inside of our code base now says, hey, if you're 50 00:03:49,030 --> 00:03:52,690 going to try to create a person, you have to pass along some argument as well. 51 00:03:53,440 --> 00:03:57,430 Now, we'll fix up this kind of new instance creation right here in just a moment. 52 00:03:57,430 --> 00:03:59,530 But first, let's finish off our constructor. 53 00:04:00,220 --> 00:04:05,710 So as soon as someone provides a name property to this constructor function, we probably want to take 54 00:04:05,710 --> 00:04:08,860 that provided name in, assign it to the first name property. 55 00:04:09,370 --> 00:04:13,870 So to do so, we can write out first name equals name like so. 56 00:04:15,470 --> 00:04:20,000 Now, one thing I want to point out here is that my choice of variable names is a little bit unclear. 57 00:04:20,600 --> 00:04:25,060 I provided a variable name or an argument name here of simply name. 58 00:04:25,310 --> 00:04:31,490 And so it's not super clear inside my constructor if this is supposed to be like the last name or is 59 00:04:31,490 --> 00:04:33,240 it supposed to be the first name of the person? 60 00:04:33,260 --> 00:04:34,310 It's not super clear. 61 00:04:34,850 --> 00:04:39,110 And so you might think, well, it would be a little bit more clear if we just wrote out first name. 62 00:04:39,950 --> 00:04:45,230 But then that makes this line of code a little bit less clear because this would technically be first 63 00:04:45,230 --> 00:04:49,080 name and now we are in a state where things are super unclear. 64 00:04:49,490 --> 00:04:53,990 We've got an argument name, a first name, but our property name is first name as well. 65 00:04:54,960 --> 00:04:59,850 So Darte understands that this right here is probably a line of code that you're going to want to write 66 00:04:59,850 --> 00:05:07,650 all the time, it's very common to have constructor arguments with names that are identical to the property 67 00:05:07,650 --> 00:05:10,230 names that you're attempting to sign them to assign them to. 68 00:05:10,650 --> 00:05:15,690 So rather than making you write out some very awkward code like this right here, Darte has a built 69 00:05:15,690 --> 00:05:19,520 in shortcut that kind of takes care of this entire situation for us. 70 00:05:19,920 --> 00:05:24,030 So let's write out this shortcut, and I think you'll be somewhat happy with how this turns out. 71 00:05:24,730 --> 00:05:27,770 So I'm going to take the set of curly braces here. 72 00:05:27,780 --> 00:05:29,340 I'm going to highlight everything in between. 73 00:05:29,760 --> 00:05:30,840 I'm going to delete it all. 74 00:05:32,130 --> 00:05:34,050 And then going to place a semicolon. 75 00:05:35,140 --> 00:05:41,770 After that person function and then to first name right here, I'm going to add on this dot like so. 76 00:05:43,060 --> 00:05:44,870 OK, so let's talk about what's going on here. 77 00:05:45,610 --> 00:05:50,010 So this is some pretty crazy syntax, but here's what's going on now. 78 00:05:50,020 --> 00:05:58,240 Any time we create a new instance of person and pass in a first argument to that creation, Dart is 79 00:05:58,240 --> 00:06:04,300 going to automatically take that first argument in, assign it to the first name property of the instance 80 00:06:04,300 --> 00:06:05,410 that is created. 81 00:06:06,920 --> 00:06:11,510 So normally when we see this set of parentheses after a function name, we think of it as the argument 82 00:06:11,510 --> 00:06:17,360 list, but DART is instead taking this argument list and it's kind of doing a little shortcut for you. 83 00:06:17,360 --> 00:06:23,180 And it's saying, oh, well, since you referred to this argument as this first name, I'm just going 84 00:06:23,180 --> 00:06:28,160 to take whatever argument was passed to this new person when it was created and automatically assign 85 00:06:28,160 --> 00:06:30,110 it to the first name property for you. 86 00:06:31,010 --> 00:06:35,450 Now, I'm using a lot of words to describe this, but it might not be super clear, so let's kind of 87 00:06:35,450 --> 00:06:39,170 complete this example and I think it'll get a little bit more obvious as time goes on. 88 00:06:40,720 --> 00:06:42,640 It's now back up inside of my main function. 89 00:06:42,940 --> 00:06:47,980 I'm going to find the person first name property Simon right here, and I'm going to delete the entire 90 00:06:47,980 --> 00:06:48,310 thing. 91 00:06:50,180 --> 00:06:54,770 And then going to find where we try to create a instance of a person right here and I'm going to make 92 00:06:54,770 --> 00:06:59,320 sure that I pass in my first name property directly to this constructor function. 93 00:06:59,990 --> 00:07:03,290 So to the thing I'm going to add a string of Steven like so. 94 00:07:04,170 --> 00:07:08,250 Now, you'll notice that all the air messages go away, and if I try to run this thing. 95 00:07:10,120 --> 00:07:12,970 Over here in the console, I'll see Stephen appear again. 96 00:07:13,770 --> 00:07:17,280 All right, let's take a quick pause right here, and when we come back, the next section, we're going 97 00:07:17,280 --> 00:07:22,500 to review what we've spoken about with Constructor's and we'll review this little shortcut of property 98 00:07:22,500 --> 00:07:23,520 assignment right here. 99 00:07:23,820 --> 00:07:25,850 So quick break and I'll see you in just a minute. 10775

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.