Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last section, we defined a custom to string function on our debt class.
So now any time we try to print out an instance of a deck, this function gets executed.
Right now we are returning a silly string.
It would be a lot better if we could somehow return our list of cards.
So this card's property right here so we could verify that the correct number and suit and rank of all
those cards are being created.
So maybe one way to do this would be to do something like, you know, I don't know, let's just try
returning that list of cards.
Let's just see what happens if we do this.
Well, when I put cards in here, you'll notice I very quickly get an error message.
So the return type list of cards isn't a string.
So when we call or when we define this two string method, the expectation is that we're going to return
a string from it and that string should be able to be used to represent this object.
So we can't just return any type of value that we want.
From this to string function, we have to specifically return a string.
So we need to somehow turn this list of cards right here into a string, one way that we could do that
is by using a built in function on the list object.
So whenever we create an instance of a list, as we are like right here, whenever we create this list
right here, this array or this list has a bunch of functions tied to it.
We already used one function was tied to it.
We use the ADD function inside of our constructor.
The list right here has another function tied to it, called, as you might guess, to string.
So every list that we make use of inside of DHAT has an automatically created to string function that
has been customized to properly print out a list.
So inside of our tutoring function, we're going to kind of delegate the printing of this object.
We're going to say, you know what, I don't know how to print out a deck.
Let's just return the cards list version of two string instead.
So now when we call to string on the list of cards, this right here is going to return a string representation
of that list of cards, and then you and I are going to return that from our two string.
And so we're kind of setting up a little bit of delegation here.
So any time that we call print on a deck, that's going to call our two string and we are going to delegate
the printing to the two string function that belongs to the list of cards.
OK, let's try running this and then seeing what happens.
OK, so click on Ron.
And now over here, you'll notice that I see what looks like a list I see like a whole bunch of records,
but what actually gets printed up is instance of card over and over and over again.
OK, well, you might be able to guess what's going on now.
So we've returned a representation of that list of cards.
However, when we call to string on the list of cards, that in turn calls to string on our card class
as well.
And so we get this huge chain of delegation of like the very top level to string, it's called, that
calls the cards to string and then the cards to string, in turn calls to string on the card class as
well.
And so that's why we're seeing this instance of card.
So to fix this up, we can now define a two string function on the card class itself, and from there
we can return a string representation of the card.
So let's try doing that.
Now, inside the card class, I'll add to string.
And then inside of here, I'll return a string and we'll try printing out something like, I don't know,
let's do something that kind of makes sense here, we'll do rank of suit like.
So remember that any time we use a dollar sign inside of a string, that is string interpolation.
And so this string right here is going to look at the rank property of the card instance and the suit
property of the card instance.
And it's going to insert them into the string.
All right, so let's try running this now.
So run that and now I see printed up much better, so we've got ace of diamonds, two of diamonds,
three of diamonds and so on.
OK, so I know that this two string stuff is a little bit strange, but essentially what's going on
is that any time we want to print up an instance of a class, if we want to have some customized message
there, we can simply define the two string function and then return our own kind of custom message
about what's going on with this particular record.
OK, so this looks pretty good.
Let's continue in the next section.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.