Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,960 --> 00:00:04,890
In the last section, we defined a custom to string function on our debt class.
2
00:00:05,160 --> 00:00:09,410
So now any time we try to print out an instance of a deck, this function gets executed.
3
00:00:09,990 --> 00:00:12,320
Right now we are returning a silly string.
4
00:00:12,480 --> 00:00:16,260
It would be a lot better if we could somehow return our list of cards.
5
00:00:16,570 --> 00:00:22,620
So this card's property right here so we could verify that the correct number and suit and rank of all
6
00:00:22,620 --> 00:00:23,940
those cards are being created.
7
00:00:24,760 --> 00:00:28,860
So maybe one way to do this would be to do something like, you know, I don't know, let's just try
8
00:00:28,860 --> 00:00:30,720
returning that list of cards.
9
00:00:31,140 --> 00:00:32,820
Let's just see what happens if we do this.
10
00:00:33,270 --> 00:00:36,960
Well, when I put cards in here, you'll notice I very quickly get an error message.
11
00:00:37,590 --> 00:00:41,600
So the return type list of cards isn't a string.
12
00:00:42,450 --> 00:00:47,460
So when we call or when we define this two string method, the expectation is that we're going to return
13
00:00:47,460 --> 00:00:51,630
a string from it and that string should be able to be used to represent this object.
14
00:00:52,170 --> 00:00:54,900
So we can't just return any type of value that we want.
15
00:00:55,350 --> 00:00:59,340
From this to string function, we have to specifically return a string.
16
00:01:00,380 --> 00:01:05,990
So we need to somehow turn this list of cards right here into a string, one way that we could do that
17
00:01:06,080 --> 00:01:10,000
is by using a built in function on the list object.
18
00:01:10,520 --> 00:01:17,000
So whenever we create an instance of a list, as we are like right here, whenever we create this list
19
00:01:17,000 --> 00:01:21,140
right here, this array or this list has a bunch of functions tied to it.
20
00:01:21,710 --> 00:01:23,720
We already used one function was tied to it.
21
00:01:24,050 --> 00:01:26,690
We use the ADD function inside of our constructor.
22
00:01:28,090 --> 00:01:33,660
The list right here has another function tied to it, called, as you might guess, to string.
23
00:01:34,210 --> 00:01:40,090
So every list that we make use of inside of DHAT has an automatically created to string function that
24
00:01:40,090 --> 00:01:43,060
has been customized to properly print out a list.
25
00:01:44,570 --> 00:01:49,830
So inside of our tutoring function, we're going to kind of delegate the printing of this object.
26
00:01:49,850 --> 00:01:52,490
We're going to say, you know what, I don't know how to print out a deck.
27
00:01:52,640 --> 00:01:58,130
Let's just return the cards list version of two string instead.
28
00:01:59,460 --> 00:02:05,490
So now when we call to string on the list of cards, this right here is going to return a string representation
29
00:02:05,490 --> 00:02:10,350
of that list of cards, and then you and I are going to return that from our two string.
30
00:02:10,690 --> 00:02:13,250
And so we're kind of setting up a little bit of delegation here.
31
00:02:13,770 --> 00:02:19,350
So any time that we call print on a deck, that's going to call our two string and we are going to delegate
32
00:02:19,350 --> 00:02:24,330
the printing to the two string function that belongs to the list of cards.
33
00:02:25,470 --> 00:02:27,900
OK, let's try running this and then seeing what happens.
34
00:02:28,880 --> 00:02:29,990
OK, so click on Ron.
35
00:02:31,370 --> 00:02:36,890
And now over here, you'll notice that I see what looks like a list I see like a whole bunch of records,
36
00:02:37,130 --> 00:02:41,450
but what actually gets printed up is instance of card over and over and over again.
37
00:02:42,230 --> 00:02:44,510
OK, well, you might be able to guess what's going on now.
38
00:02:44,940 --> 00:02:49,160
So we've returned a representation of that list of cards.
39
00:02:49,620 --> 00:02:56,840
However, when we call to string on the list of cards, that in turn calls to string on our card class
40
00:02:56,840 --> 00:02:57,460
as well.
41
00:02:57,470 --> 00:03:03,200
And so we get this huge chain of delegation of like the very top level to string, it's called, that
42
00:03:03,200 --> 00:03:09,620
calls the cards to string and then the cards to string, in turn calls to string on the card class as
43
00:03:09,620 --> 00:03:09,940
well.
44
00:03:10,400 --> 00:03:13,040
And so that's why we're seeing this instance of card.
45
00:03:13,670 --> 00:03:19,310
So to fix this up, we can now define a two string function on the card class itself, and from there
46
00:03:19,310 --> 00:03:22,280
we can return a string representation of the card.
47
00:03:23,070 --> 00:03:23,870
So let's try doing that.
48
00:03:23,870 --> 00:03:27,380
Now, inside the card class, I'll add to string.
49
00:03:28,780 --> 00:03:34,180
And then inside of here, I'll return a string and we'll try printing out something like, I don't know,
50
00:03:34,180 --> 00:03:38,740
let's do something that kind of makes sense here, we'll do rank of suit like.
51
00:03:38,740 --> 00:03:44,650
So remember that any time we use a dollar sign inside of a string, that is string interpolation.
52
00:03:45,220 --> 00:03:50,820
And so this string right here is going to look at the rank property of the card instance and the suit
53
00:03:50,860 --> 00:03:52,660
property of the card instance.
54
00:03:52,660 --> 00:03:54,700
And it's going to insert them into the string.
55
00:03:55,580 --> 00:03:57,080
All right, so let's try running this now.
56
00:03:58,540 --> 00:04:04,030
So run that and now I see printed up much better, so we've got ace of diamonds, two of diamonds,
57
00:04:04,030 --> 00:04:05,470
three of diamonds and so on.
58
00:04:06,190 --> 00:04:11,200
OK, so I know that this two string stuff is a little bit strange, but essentially what's going on
59
00:04:11,200 --> 00:04:17,019
is that any time we want to print up an instance of a class, if we want to have some customized message
60
00:04:17,019 --> 00:04:22,870
there, we can simply define the two string function and then return our own kind of custom message
61
00:04:22,990 --> 00:04:25,420
about what's going on with this particular record.
62
00:04:26,140 --> 00:04:27,220
OK, so this looks pretty good.
63
00:04:27,430 --> 00:04:29,140
Let's continue in the next section.
6537
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.