All language subtitles for 9. [Dart] Classes and Objects

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt-PT Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,500 --> 00:00:00,740 All right. 1 2 00:00:00,780 --> 00:00:07,260 So now that we've seen classes and objects in action and we've used them a little bit, it's time to do 2 3 00:00:07,260 --> 00:00:13,140 a deep dive and understand exactly how classes and objects work in Dart. 3 4 00:00:13,200 --> 00:00:20,190 So firstly a class is simply just a blueprint and it's used to create all of the things that we're going 4 5 00:00:20,190 --> 00:00:26,010 to be showing in our app and the user is going to be interacting with. So essentially, everything that 5 6 00:00:26,010 --> 00:00:31,750 makes up our app is going to be created using a blueprint that's called a class. 6 7 00:00:31,740 --> 00:00:34,250 Now what exactly is an object then? 7 8 00:00:34,320 --> 00:00:41,940 Well when we take that blueprint and we take it to the factory and the actual car, in this instance, gets 8 9 00:00:41,940 --> 00:00:48,030 built, well that's the object. That's the actual thing that's going to be doing the driving, or in our 9 10 00:00:48,030 --> 00:00:54,350 case of an app, it'll be the actual button that can receive taps and the images that actually change. 10 11 00:00:54,390 --> 00:01:01,320 Those are the actual objects. But because we can't see the objects until the app is running, we have to 11 12 00:01:01,320 --> 00:01:07,000 plan out how they will function and how they will work by creating these classes. 12 13 00:01:07,050 --> 00:01:12,330 A class has two really important things. One are its properties. 13 14 00:01:12,330 --> 00:01:18,840 So if we're building out a car, then that will be its color or the number of seats it has or the number 14 15 00:01:18,840 --> 00:01:25,200 wheels it has or the number of doors it has or all sorts of things that are things to do with the 15 16 00:01:25,200 --> 00:01:32,430 characteristics of the object. And in Dart, these things that we've been referring to as variables when 16 17 00:01:32,430 --> 00:01:34,320 they're inside a class, 17 18 00:01:34,350 --> 00:01:37,190 they're now called properties or fields. 18 19 00:01:37,200 --> 00:01:42,150 Now the other important part of a class that we need to define are its methods. 19 20 00:01:42,180 --> 00:01:48,300 So the things that it can do. The actions that the object that will eventually get created, will be able 20 21 00:01:48,300 --> 00:01:49,660 to perform. 21 22 00:01:49,890 --> 00:01:57,410 In the case of our car object, that might be to drive, or to brake, or to turn around. 22 23 00:01:57,600 --> 00:02:04,460 And these methods are actions that the object will be able to do that we're going to define in the blueprint 23 24 00:02:04,830 --> 00:02:07,260 even before the object gets built. 24 25 00:02:07,260 --> 00:02:12,160 So for example, you might see a class defined like this. 25 26 00:02:12,210 --> 00:02:17,670 Firstly you have that class keyword which shows that everything else that's coming afterwards is going 26 27 00:02:17,670 --> 00:02:19,240 to be a blueprint. 27 28 00:02:19,500 --> 00:02:21,270 And then you have the name of the class. 28 29 00:02:21,270 --> 00:02:28,620 So in this case we're building a car blueprint. And by convention in Dart as with many other languages, 29 30 00:02:28,920 --> 00:02:36,210 when you're creating a variable say here we've got numberOfDoors, or when you're creating a function, 30 31 00:02:36,270 --> 00:02:38,610 say here we've got drive, 31 32 00:02:38,610 --> 00:02:40,490 those are all camel case 32 33 00:02:40,530 --> 00:02:43,020 starting with a lowercase word. 33 34 00:02:43,170 --> 00:02:49,430 But when you're creating a class to show that it's different, we always start-off with a capital letter. 34 35 00:02:49,500 --> 00:02:54,410 So all the words in your class names are capitalized. 35 36 00:02:54,450 --> 00:02:56,270 Now here's our Car class 36 37 00:02:56,310 --> 00:03:02,370 and inside these curly braces are the definitions for this car class. 37 38 00:03:02,370 --> 00:03:10,370 So this is the blueprint of exactly how our car will look and also how it will behave. 38 39 00:03:10,560 --> 00:03:15,650 And between the looks and the behavior, we end up with a class. 39 40 00:03:15,660 --> 00:03:20,940 So in this case we have just a single property, which is called numberOfDoors. 40 41 00:03:20,940 --> 00:03:25,500 And this defines a characteristic of the car namely, how many doors it has. 41 42 00:03:25,560 --> 00:03:26,930 This car has five. 42 43 00:03:27,150 --> 00:03:30,540 This car also has a function called drive, 43 44 00:03:30,720 --> 00:03:34,780 and this defines a little bit of functionality for the car. 44 45 00:03:34,800 --> 00:03:42,530 So namely this car, when we call this thing called drive, it's going to start turning the wheels for example. 45 46 00:03:42,540 --> 00:03:50,400 Now notice how inside a class, our variables are no longer called variables and they're called properties 46 47 00:03:50,430 --> 00:03:51,090 or fields. 47 48 00:03:51,510 --> 00:03:59,130 Similarly, a function when it's defined inside the curly braces of a class, it's now called a method and 48 49 00:03:59,130 --> 00:04:00,890 not a function anymore. 49 50 00:04:00,930 --> 00:04:06,630 So if you're new to programming there's a lot of new words and it's a good idea to build your own glossary 50 51 00:04:06,630 --> 00:04:09,530 of words because it's just like learning a new language. 51 52 00:04:09,570 --> 00:04:15,410 When people refer to a method, you can think of them as just a function that's associated with the class. 52 53 00:04:15,450 --> 00:04:20,020 And when people are talking about a property, it's simply a variable that's associated with the class. 53 54 00:04:20,040 --> 00:04:24,070 And once you hear it often enough, then it'll start becoming second nature. 54 55 00:04:24,090 --> 00:04:31,560 Now once we have our object, it's the object that actually performed those pre-planned actions in the 55 56 00:04:31,560 --> 00:04:34,690 class such as moving, right? 56 57 00:04:34,710 --> 00:04:41,250 Or it actually has those characteristics which we defined in our class such as its color property or 57 58 00:04:41,460 --> 00:04:48,660 the numberOfDoors property. The two paths of creating a object are first, we have to create the class 58 59 00:04:49,140 --> 00:04:54,750 and this is gonna be the blueprint that's going to define everything about the eventual object that 59 60 00:04:54,750 --> 00:04:55,740 will get created. 60 61 00:04:56,190 --> 00:05:04,380 So we use the class keyword, we give the class a capitalized name and we define variables in it which 61 62 00:05:04,440 --> 00:05:06,780 are properties for that class. 62 63 00:05:06,780 --> 00:05:12,510 And we also give it functions which become the methods that the class can do. 63 64 00:05:12,510 --> 00:05:19,350 And when we actually create the object from this class, then our code looks something like this. 64 65 00:05:19,350 --> 00:05:26,430 We would define the type, which is in this case, the name of the class and then we would give our object 65 66 00:05:26,520 --> 00:05:27,380 a name. 66 67 00:05:27,570 --> 00:05:36,900 And finally we would set it to equal a new version of that class. When we have the word car and these 67 68 00:05:36,900 --> 00:05:38,520 empty parentheses, 68 69 00:05:38,520 --> 00:05:42,300 it's basically creating a new object from this blueprint. 69 70 00:05:42,300 --> 00:05:48,150 It's like sending that blueprint to a factory and out comes our actual car object. 70 71 00:05:48,150 --> 00:05:52,190 Now this is all very abstract and we're having to imagine a lot of it in our head. 71 72 00:05:52,290 --> 00:05:58,260 So let's put it down onto code and head over to our trusty DartPad to be up to see this in action and 72 73 00:05:58,290 --> 00:06:00,620 understand a little bit better. 73 74 00:06:00,660 --> 00:06:07,760 So I've got my empty main method up here, and I'm going to create a new class right below it. 74 75 00:06:07,770 --> 00:06:09,960 I'm going to call that class Human. 75 76 00:06:09,960 --> 00:06:12,280 So remember it should be capitalized. 76 77 00:06:12,720 --> 00:06:19,330 And this class of humans are going to have some properties, so some characteristics. 77 78 00:06:19,350 --> 00:06:22,650 So what kind of characteristics might a human have? 78 79 00:06:22,650 --> 00:06:25,170 Well they might have a height right? 79 80 00:06:25,200 --> 00:06:34,110 We could create a height property and maybe it'll have an age property. And when we create a new class, 80 81 00:06:34,260 --> 00:06:42,950 we can either create these properties as empty or null such as here, or we can say that by default when 81 82 00:06:42,950 --> 00:06:45,810 we create a new human, it should always have a height. 82 83 00:06:45,870 --> 00:06:53,520 Let's say that when a default human gets created, it starts out being, I don't know ,maybe 15 centimetres 83 84 00:06:53,520 --> 00:06:59,120 in height, and maybe it will have a default age of zero. 84 85 00:06:59,130 --> 00:07:07,920 So now if I go into my main method and I create a new object from this human, let's say that we create 85 86 00:07:07,980 --> 00:07:09,230 a new human, 86 87 00:07:09,330 --> 00:07:15,240 so that's gonna be the type of this object. And our actual human, let's call her 87 88 00:07:15,270 --> 00:07:23,790 Jenny, is going to be created using our human class. And we do that of course by writing human and then 88 89 00:07:23,820 --> 00:07:26,430 we have a set of empty parentheses. 89 90 00:07:26,430 --> 00:07:29,630 And this builds out our new human. 90 91 00:07:29,670 --> 00:07:33,420 So human is the blueprint for all humans. 91 92 00:07:33,450 --> 00:07:36,050 They all should have a height and they all should have an age. 92 93 00:07:36,210 --> 00:07:42,300 And we have some default values for how humans should be when they first get built. 93 94 00:07:42,360 --> 00:07:49,620 And Jenny is our actual object who's built from this blueprint. And we're doing things in our program, 94 95 00:07:49,650 --> 00:07:57,200 we're actually interacting with Jenny rather than the sort of abstract blueprint kind of for human. So 95 96 00:07:57,230 --> 00:08:03,170 we might say some things such as, let's print Jenny's height. So we have our print statement and then 96 97 00:08:03,170 --> 00:08:09,410 we're going to tap into our Jenny object and then we're gonna use the dot notation to tap into its property 97 98 00:08:09,710 --> 00:08:11,000 namely the height property. 98 99 00:08:11,000 --> 00:08:19,070 So we'll say jenny.height and let's print that into the console. And you can see that because we've 99 100 00:08:19,070 --> 00:08:23,570 given all humans a default starting height of 15, 100 101 00:08:23,570 --> 00:08:26,620 Jenny also has a height of 15. 101 102 00:08:26,620 --> 00:08:30,440 Now a little bit later on, I could maybe change that. 102 103 00:08:30,530 --> 00:08:36,490 Maybe Jenny grows a bit and lets say that her height is now 20cm. 103 104 00:08:36,980 --> 00:08:45,050 So I can change all of these properties and I can tap into them simply by using that dot notation. By 104 105 00:08:45,110 --> 00:08:50,660 saying that this is the object that I'm concerned about and it finds the object which we know gets created 105 106 00:08:50,660 --> 00:08:54,980 from the human class, and by default, all of the properties of a human 106 107 00:08:54,980 --> 00:09:03,400 start out with these values. Then I'm saying get me Jenny's height. And that's what that dot says. 107 108 00:09:03,410 --> 00:09:09,260 It's just saying, 'this is the property associated with Jenny that I want to know about.' 108 109 00:09:09,360 --> 00:09:09,780 Or 109 110 00:09:09,800 --> 00:09:14,720 later on when our human get some functions associated with it, you'll see that the way that we tap into 110 111 00:09:14,720 --> 00:09:16,970 it is still with the dot notation. 111 112 00:09:17,390 --> 00:09:23,060 So when you see something.something, it usually means that this is an object and we're tapping into 112 113 00:09:23,090 --> 00:09:26,030 one of its properties or methods. 113 114 00:09:26,120 --> 00:09:32,670 Now at the moment, every single new human that we create starts out with a height of 15cm. 114 115 00:09:32,690 --> 00:09:34,850 Now that might not be true 115 116 00:09:34,850 --> 00:09:35,460 right? 116 117 00:09:35,480 --> 00:09:40,020 So let's say that we created another human and this is James. 117 118 00:09:40,130 --> 00:09:47,210 And he's also created from the human class, which is what this part means. We're going to the human factory 118 119 00:09:47,240 --> 00:09:49,660 and we're building a new human. 119 120 00:09:49,790 --> 00:09:54,890 And if I was to print his height, you'll see that it's exactly the same. 120 121 00:09:54,890 --> 00:10:00,950 He's also 15 centimetres tall. But that's probably not really true 121 122 00:10:00,950 --> 00:10:01,250 right? 122 123 00:10:01,250 --> 00:10:07,410 Why should it be that when a new human gets created, they all have the same default height? 123 124 00:10:07,520 --> 00:10:15,410 What if we wanted to initialize each object with a different value for some of these properties? 124 125 00:10:15,440 --> 00:10:16,520 How would we do that? 125 126 00:10:16,520 --> 00:10:22,460 Well we already saw that in the last lesson that we can use something called a constructor for our class. 126 127 00:10:23,000 --> 00:10:24,500 And to create a constructor, 127 128 00:10:24,500 --> 00:10:31,370 we would write the name of the class, so it's human, and then we would create a set of empty parentheses. 128 129 00:10:31,370 --> 00:10:37,580 And just as when we create a function we provide some inputs into these parentheses, when we create a 129 130 00:10:37,580 --> 00:10:42,330 constructor for a class we can provide some inputs to our class. 130 131 00:10:42,440 --> 00:10:50,840 So we could say that maybe the starting height property which is going to be a double, because our height 131 132 00:10:50,840 --> 00:10:52,120 property is a double. 132 133 00:10:52,200 --> 00:10:55,190 We'll give it a name of startingHeight. 133 134 00:10:55,190 --> 00:11:02,840 And now inside the curly braces, we can assign these startingHeight to the height, and height can start 134 135 00:11:02,840 --> 00:11:05,860 out without a value. It'll start out being null. 135 136 00:11:05,990 --> 00:11:14,270 But when we create our new human, we have to provide a starting height and then we can say that the humans 136 137 00:11:14,540 --> 00:11:21,560 height property will be set to whatever the starting height is that we provided. 137 138 00:11:21,560 --> 00:11:27,180 So now we get some errors here because it needs a starting height and we haven't given it one. 138 139 00:11:27,230 --> 00:11:35,480 So let's add that in. So we could say that Jenny is a human who starts out being 15 centimeters but James 139 140 00:11:35,540 --> 00:11:40,760 starts out being a human who is, maybe he's a bit taller. Maybe he's 20 centimeters. 140 141 00:11:40,760 --> 00:11:46,730 So now when we click run, you can see that Jenny's height starts out 15cm and James's height starts out 141 142 00:11:46,790 --> 00:11:48,060 at 20cm. 142 143 00:11:48,140 --> 00:11:54,370 And our code now makes a little bit more sense because why should a human have a default height 143 144 00:11:54,380 --> 00:11:54,720 right? 144 145 00:11:54,720 --> 00:12:04,100 They they don't all get created with the same height. And just as we could make the input or the parameter 145 146 00:12:04,520 --> 00:12:11,360 a named parameter, in our functions we can do the same in our class constructor. So we can add a set of 146 147 00:12:11,360 --> 00:12:19,850 curly braces around these inputs to say that when we use it when we create our new human object, we have 147 148 00:12:19,850 --> 00:12:21,710 to specify that name. 148 149 00:12:21,710 --> 00:12:25,290 So startingHeight and then a colon. 149 150 00:12:25,610 --> 00:12:31,700 So this way it's a bit easier on the eye and you can see that I'm creating a new human and I'm providing 150 151 00:12:31,820 --> 00:12:39,870 a starting height for that human. As I said earlier, our classes can have properties and it can also have 151 152 00:12:39,960 --> 00:12:46,590 methods. Our methods are simply functions that are defined inside a class. 152 153 00:12:46,590 --> 00:12:56,670 If we were to create a method let's say, talk to get a little human to say something, we can tell 153 154 00:12:56,670 --> 00:12:57,710 it what to say. 154 155 00:12:57,780 --> 00:13:06,840 So we'll create a string input called whatToSay, and then inside the talk function or in this case the 155 156 00:13:06,840 --> 00:13:12,720 talk method because we're talking about a function associated with the class, we can say that we'll just 156 157 00:13:12,720 --> 00:13:16,550 print the string that it's told to say. 157 158 00:13:17,730 --> 00:13:25,560 So that means we can tell our new humans, say for example, let's get rid of James. Let's just say Jenny 158 159 00:13:26,850 --> 00:13:34,910 .talk and the thing that you should say is ,Why is this sky blue?' 159 160 00:13:37,060 --> 00:13:45,730 Now when we run our code, we print Jenny's height and we get Jenny to talk. And you can see that by calling 160 161 00:13:45,790 --> 00:13:53,920 this method talk, then this object actually performs that action and it prints out the thing that we 161 162 00:13:53,920 --> 00:13:56,600 want it to say into the console. 162 163 00:13:56,620 --> 00:14:00,920 This is now a method that's associated with the human class. 163 164 00:14:01,240 --> 00:14:07,630 And just as we use the dot notation to access the property height, we're also using the dot notation 164 165 00:14:07,930 --> 00:14:10,740 to trigger the talk method. 165 166 00:14:10,900 --> 00:14:18,310 So when we're working with objects you see the dot all over the place and it just refers to saying, 'this 166 167 00:14:18,310 --> 00:14:23,500 object do this or this object get me it's property.' 167 168 00:14:23,500 --> 00:14:34,240 So we now have a pretty simple class. And this class is our blueprint for how a human object should behave 168 169 00:14:34,660 --> 00:14:37,540 and what characteristics it should have. 169 170 00:14:37,540 --> 00:14:45,250 So all of the things that a human object, once it gets created, should be able to do and what it should 170 171 00:14:45,250 --> 00:14:53,550 be. You can of course make this as complex as you need it to be. But in essence, we're simply creating 171 172 00:14:53,580 --> 00:15:03,300 a plan for how this object should behave or how it should appear. And we use it by creating it as a new 172 173 00:15:03,300 --> 00:15:10,320 object and we can create as many of these as we need to and get them to interact with each other or 173 174 00:15:10,320 --> 00:15:13,260 get them to do specific tasks. 174 175 00:15:13,260 --> 00:15:17,000 And so this is how we would create our classes and objects. 175 176 00:15:17,010 --> 00:15:23,440 But why exactly do we need these classes and objects? To find out about that, 176 177 00:15:23,460 --> 00:15:24,670 I'll see you on the next lesson. 20257

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