Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,000 --> 00:00:04,520
In the last section, we created a new class called the Person Class.
2
00:00:05,020 --> 00:00:09,520
We're now going to use this class to create a new instance of it and then somehow call the print name
3
00:00:09,520 --> 00:00:14,320
method assigned to that class to create a new instance out of this class.
4
00:00:14,440 --> 00:00:16,740
We need to first define the main function.
5
00:00:17,140 --> 00:00:22,210
Remember, any time darte runs, it's going to look for a function called main and execute that first.
6
00:00:22,580 --> 00:00:27,310
So if we want to create a new instance of this class, we need to do it inside of that main function.
7
00:00:28,250 --> 00:00:32,479
It's going to go up to the top of the file and give myself a little bit space and then I'll place the
8
00:00:32,780 --> 00:00:36,890
main function, so right out void, main set of parentheses.
9
00:00:36,890 --> 00:00:42,010
And this set of curly braces, by the way, we never really spoke about the void word right here.
10
00:00:42,440 --> 00:00:47,420
Remember that any time we define a function to the left of the function name, we place a type that
11
00:00:47,420 --> 00:00:50,950
indicates what type of value we are returning from this function.
12
00:00:51,380 --> 00:00:56,090
And so before we had something like string over here, when we did that print name function previously,
13
00:00:56,780 --> 00:01:01,700
if we have the word void right here, it means that we are returning nothing from this function at all.
14
00:01:02,970 --> 00:01:07,950
So now that we have the main function put together, we can use this person class to create a new instance
15
00:01:07,950 --> 00:01:08,280
of it.
16
00:01:08,810 --> 00:01:16,530
So to create a new instance of a person of first declare a new variable, which I will say var and then
17
00:01:16,530 --> 00:01:17,560
lowercase person.
18
00:01:17,970 --> 00:01:20,220
So this right here is the variable declaration.
19
00:01:20,700 --> 00:01:27,210
And then on the right hand side we will create a new instance of the class person by writing out new
20
00:01:27,360 --> 00:01:29,310
person like so.
21
00:01:31,060 --> 00:01:37,090
So that gives us a reference to a new person object that has been created inside of our application.
22
00:01:38,830 --> 00:01:44,830
On the line underneath that, we can now attempt to set the first name property on the person instance,
23
00:01:45,190 --> 00:01:51,340
so to access this first named property directly and set a new value on it, we can write out person
24
00:01:51,340 --> 00:01:56,740
dot first name equals Steven, like sell or whatever your name might be.
25
00:01:57,920 --> 00:02:02,840
Notice that we were able to set the value of first name right here as a string, specifically because
26
00:02:02,840 --> 00:02:05,840
we labeled its type inside of the class definition.
27
00:02:07,060 --> 00:02:12,070
If we had instead tried to assign an integer to this thing like one, two, three, we would end up
28
00:02:12,070 --> 00:02:13,280
seeing an error message.
29
00:02:13,870 --> 00:02:14,800
Well, we should anyways.
30
00:02:14,800 --> 00:02:15,360
Let's try.
31
00:02:15,370 --> 00:02:19,120
Sometimes you have to, like, kind of do a little bit of stuff to kind of get the thing to do.
32
00:02:19,120 --> 00:02:19,570
Right.
33
00:02:20,170 --> 00:02:20,620
Come on.
34
00:02:20,620 --> 00:02:21,810
I know you think that's in there.
35
00:02:21,820 --> 00:02:23,320
Let's try running this code.
36
00:02:23,800 --> 00:02:24,190
There we go.
37
00:02:24,190 --> 00:02:24,700
That's better.
38
00:02:25,030 --> 00:02:28,990
So now, when I tried to assign an integer to the first name property, it's saying, hey, you can't
39
00:02:28,990 --> 00:02:29,400
do that.
40
00:02:29,560 --> 00:02:31,690
You're trying to assign an integer to a string.
41
00:02:32,990 --> 00:02:35,390
All right, so I'm going to flip it back to Stephen like so.
42
00:02:36,620 --> 00:02:42,560
So now that we have a defined first name property on our person object right here, we can call the
43
00:02:42,950 --> 00:02:47,300
name method on it and I'll expect to see the name printed out on the top right inside.
44
00:02:47,930 --> 00:02:52,720
So I'll do person dot print name like so.
45
00:02:52,880 --> 00:02:54,830
And don't forget the semicolon on the end.
46
00:02:55,550 --> 00:02:59,540
Now, if I run this code, I should see Stephen appear on the top.
47
00:02:59,540 --> 00:02:59,910
Right.
48
00:03:00,050 --> 00:03:01,130
And sure enough, I do.
49
00:03:02,000 --> 00:03:08,120
OK, so this is a very quick example of how we work with classes, but I want to kind of point out something
50
00:03:08,120 --> 00:03:08,780
inside of here.
51
00:03:09,140 --> 00:03:14,180
Notice how creating this person object right here and then setting the first named property on it.
52
00:03:14,630 --> 00:03:17,090
To do so, we had to write two separate lines of code.
53
00:03:17,750 --> 00:03:22,070
Well, I don't know about you, but it sure seems like every person that we create inside of our application
54
00:03:22,220 --> 00:03:24,900
is probably always going to want to have a first name.
55
00:03:25,520 --> 00:03:29,150
In other words, how often do you meet a person in real life who doesn't have a name?
56
00:03:29,840 --> 00:03:32,980
I think everybody has a name or most everyone does.
57
00:03:33,500 --> 00:03:38,210
So I think that in the next section we should figure out some way of making sure that every person who
58
00:03:38,210 --> 00:03:44,600
gets created inside this application always instantly gets a first name property assigned to them rather
59
00:03:44,600 --> 00:03:49,580
than creating the person and then having to define that first name property on the next line.
60
00:03:50,070 --> 00:03:54,490
So let's come back to the next section and we'll figure out how to do this all in one step right here.
61
00:03:54,770 --> 00:03:56,770
It's a quick break and I'll see you in just a minute.
6084
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.