1 00:00:00,000 --> 00:00:03,360 ‫-: Hi. Within this lecture, we are going to see how 2 00:00:03,360 --> 00:00:06,450 ‫we can leverage this init function in order to 3 00:00:06,450 --> 00:00:10,470 ‫create these properties without initializing them. 4 00:00:10,470 --> 00:00:12,870 ‫So I'm deleting these initializers 5 00:00:12,870 --> 00:00:16,260 ‫and I'm going to do this inside of my init function. 6 00:00:16,260 --> 00:00:17,910 ‫Okay? So rather 7 00:00:17,910 --> 00:00:21,420 ‫than printing musician created I'm just gonna ask 8 00:00:21,420 --> 00:00:25,050 ‫for some input in the init function. 9 00:00:25,050 --> 00:00:30,050 ‫So I'm going to ask for name init, it'll be a string, okay? 10 00:00:30,180 --> 00:00:32,400 ‫And again, an age init. 11 00:00:32,400 --> 00:00:36,690 ‫This will be an integer and an instrument init. 12 00:00:36,690 --> 00:00:38,973 ‫This will be, again, a string. 13 00:00:39,870 --> 00:00:41,460 ‫So what I'm going to do, 14 00:00:41,460 --> 00:00:43,290 ‫I'm going to take those values 15 00:00:43,290 --> 00:00:47,700 ‫and I'm going to say name is now name init. 16 00:00:47,700 --> 00:00:51,180 ‫Age is now age init, Okay? 17 00:00:51,180 --> 00:00:55,950 ‫So you're gonna have to say name is equal to name init. 18 00:00:55,950 --> 00:00:58,293 ‫Age is now age init. 19 00:00:59,130 --> 00:01:04,130 ‫And of course the instrument is again, instrument init. 20 00:01:04,530 --> 00:01:06,270 ‫You don't have to say init. 21 00:01:06,270 --> 00:01:08,250 ‫You can just go for name. 22 00:01:08,250 --> 00:01:11,220 ‫You can just go for whatever variable name you want. 23 00:01:11,220 --> 00:01:15,180 ‫I'm just trying to differentiate them a little bit 24 00:01:15,180 --> 00:01:17,670 ‫so you don't get confused. 25 00:01:17,670 --> 00:01:21,300 ‫Okay? So whenever I create an instance of this class 26 00:01:21,300 --> 00:01:25,620 ‫it will ask me for a name and age and instrument. 27 00:01:25,620 --> 00:01:28,380 ‫So rather than defining those values 28 00:01:28,380 --> 00:01:30,900 ‫after we create the object, 29 00:01:30,900 --> 00:01:34,860 ‫now we can do this in the same line. 30 00:01:34,860 --> 00:01:36,420 ‫So let me do an example. 31 00:01:36,420 --> 00:01:41,420 ‫So if I say Let James and James is a musician 32 00:01:41,790 --> 00:01:43,650 ‫it should have asked me there. 33 00:01:43,650 --> 00:01:45,930 ‫So let me do comment B. 34 00:01:45,930 --> 00:01:48,630 ‫Yep. This is asking right now. 35 00:01:48,630 --> 00:01:51,510 ‫So let me do this from the beginning. 36 00:01:51,510 --> 00:01:55,650 ‫Okay? So if I do command B, it'll get synchronized 37 00:01:55,650 --> 00:01:56,970 ‫as I said before. 38 00:01:56,970 --> 00:01:58,650 ‫Now it's giving me an error. 39 00:01:58,650 --> 00:02:02,460 ‫It says that you have a missing argument in here. 40 00:02:02,460 --> 00:02:06,930 ‫Now if you open parenthesis, you will see that it asks you 41 00:02:06,930 --> 00:02:11,490 ‫for a name init, age init, and instrument init. 42 00:02:11,490 --> 00:02:15,210 ‫So for name, you can go for James, okay? 43 00:02:15,210 --> 00:02:17,550 ‫For age, you can go for 50. 44 00:02:17,550 --> 00:02:21,690 ‫And for instrument, you can go for guitar. 45 00:02:21,690 --> 00:02:26,353 ‫So now if I print James dot age, it'll give me 50. 46 00:02:27,510 --> 00:02:32,250 ‫So that's how you create objects with initializers. 47 00:02:32,250 --> 00:02:36,120 ‫Okay? And while we are added, 48 00:02:36,120 --> 00:02:40,110 ‫I believe we should learn about enums as well. 49 00:02:40,110 --> 00:02:42,870 ‫So enums make our life easier 50 00:02:42,870 --> 00:02:45,630 ‫when we try to categorize things. 51 00:02:45,630 --> 00:02:47,400 ‫What do I mean? 52 00:02:47,400 --> 00:02:52,400 ‫For example, if I want to add a type of musician in here 53 00:02:52,560 --> 00:02:55,920 ‫as a property, I can just go and do 54 00:02:55,920 --> 00:02:58,860 ‫that with a string right? But rather 55 00:02:58,860 --> 00:03:02,910 ‫than doing a string, I'm just going to go for enum 56 00:03:02,910 --> 00:03:07,910 ‫and you will see why, why I'm doing that in a clear way. 57 00:03:08,490 --> 00:03:13,490 ‫So generally you see enums enumerated type declarations 58 00:03:13,920 --> 00:03:17,970 ‫in the top of this project files, okay? 59 00:03:17,970 --> 00:03:21,360 ‫So you have to give a name here 60 00:03:21,360 --> 00:03:24,570 ‫an enum name, like musician type. 61 00:03:24,570 --> 00:03:27,960 ‫And in here you write some categories. 62 00:03:27,960 --> 00:03:32,640 ‫For example, our musician can be a vocalist, a guitarist 63 00:03:32,640 --> 00:03:34,920 ‫or lead guitarist, right? 64 00:03:34,920 --> 00:03:36,270 ‫So write them all. 65 00:03:36,270 --> 00:03:40,320 ‫So lead guitar, it would be a vocalist, 66 00:03:40,320 --> 00:03:43,470 ‫it would be a drummer, right? 67 00:03:43,470 --> 00:03:46,950 ‫And it would be a bassist as well. 68 00:03:46,950 --> 00:03:49,140 ‫So maybe you can think something 69 00:03:49,140 --> 00:03:52,203 ‫like violinist or something. Okay? 70 00:03:53,310 --> 00:03:56,700 ‫So why are we creating this types? 71 00:03:56,700 --> 00:04:00,660 ‫We can have done this with strings as well. 72 00:04:00,660 --> 00:04:02,820 ‫I'm going to show you why. 73 00:04:02,820 --> 00:04:05,940 ‫If I come over here to my attributes 74 00:04:05,940 --> 00:04:08,040 ‫I can easily add a new type. 75 00:04:08,040 --> 00:04:11,100 ‫I can just say type, okay? 76 00:04:11,100 --> 00:04:14,970 ‫And this would be a string, but if I do that 77 00:04:14,970 --> 00:04:16,920 ‫my other developer working 78 00:04:16,920 --> 00:04:20,970 ‫on this project can write vocal, okay? 79 00:04:20,970 --> 00:04:25,713 ‫And the other developer using this class can write vocalist. 80 00:04:26,610 --> 00:04:30,270 ‫And even if you are the only one working on a project 81 00:04:30,270 --> 00:04:33,930 ‫like this, you can do the same mistake as well, right? 82 00:04:33,930 --> 00:04:37,140 ‫You can go for vocal, you can go for vocalist. 83 00:04:37,140 --> 00:04:38,730 ‫In some other cases 84 00:04:38,730 --> 00:04:42,510 ‫maybe you want to standardize those types. 85 00:04:42,510 --> 00:04:45,240 ‫So this is where enums come to play. 86 00:04:45,240 --> 00:04:48,000 ‫You kind of standardize the category. 87 00:04:48,000 --> 00:04:52,743 ‫So that will leave no room for mistake at all. 88 00:04:53,610 --> 00:04:57,390 ‫So rather than saying this is going to be a string 89 00:04:57,390 --> 00:05:01,860 ‫I'm going to say this will be a musician type. Okay? 90 00:05:01,860 --> 00:05:06,150 ‫As you can see, musician type is a musician type type. 91 00:05:06,150 --> 00:05:09,030 ‫So this is an enum, this is not a class 92 00:05:09,030 --> 00:05:13,320 ‫but we can actually use this to choose a category. 93 00:05:13,320 --> 00:05:15,960 ‫And now we have an error inside of 94 00:05:15,960 --> 00:05:20,960 ‫init because we have to ask for a type init, right? 95 00:05:21,450 --> 00:05:24,150 ‫So I'm going to go for type init 96 00:05:24,150 --> 00:05:29,150 ‫and this will be a musician type rather than string. 97 00:05:29,340 --> 00:05:31,020 ‫Now I can come over here 98 00:05:31,020 --> 00:05:36,020 ‫and I can easily say my type, this type, okay? 99 00:05:36,240 --> 00:05:38,580 ‫is going to be a type init. 100 00:05:39,900 --> 00:05:44,900 ‫So right now we have created a category for ourselves 101 00:05:44,970 --> 00:05:48,630 ‫and we have assigned it to be a property in our class. 102 00:05:48,630 --> 00:05:49,860 ‫So let me do a build. 103 00:05:49,860 --> 00:05:51,660 ‫So it'll get synchronized, 104 00:05:51,660 --> 00:05:55,110 ‫and once it gets synchronized, as you can see 105 00:05:55,110 --> 00:05:57,450 ‫we got another error in here. 106 00:05:57,450 --> 00:05:58,283 ‫So it says 107 00:05:58,283 --> 00:06:02,640 ‫that you have to add a new parameter here called type init. 108 00:06:02,640 --> 00:06:07,110 ‫If you hit fixed, this will be added automatically. 109 00:06:07,110 --> 00:06:09,540 ‫So I can hit that here 110 00:06:09,540 --> 00:06:13,530 ‫and I can choose one of the categories that we have created. 111 00:06:13,530 --> 00:06:15,660 ‫So I can easily come here. 112 00:06:15,660 --> 00:06:19,830 ‫I can say, James is a vocalist, for example. 113 00:06:19,830 --> 00:06:23,040 ‫And now I can reach this value as well, right? 114 00:06:23,040 --> 00:06:26,850 ‫So let me try to print this James dot type. 115 00:06:26,850 --> 00:06:31,170 ‫Now I will see the type in the logs as well. 116 00:06:31,170 --> 00:06:33,870 ‫So this is how you use enums. 117 00:06:33,870 --> 00:06:37,410 ‫This is how you leverage enums in your classes. 118 00:06:37,410 --> 00:06:41,160 ‫If you're looking for a way to create categories, 119 00:06:41,160 --> 00:06:43,560 ‫rather than doing this with a string, 120 00:06:43,560 --> 00:06:45,190 ‫you can do this with enums, 121 00:06:45,190 --> 00:06:48,480 ‫and this would be much more efficient 122 00:06:48,480 --> 00:06:52,893 ‫much more safer than doing this with a string. 123 00:06:54,300 --> 00:06:56,820 ‫So that's all for right now. 124 00:06:56,820 --> 00:06:59,340 ‫We are going to stop here actually 125 00:06:59,340 --> 00:07:00,780 ‫and within the next lecture 126 00:07:00,780 --> 00:07:03,960 ‫we are going to see how we can add some actions 127 00:07:03,960 --> 00:07:07,443 ‫some functions to our class as well.