Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Surface Is Fine and Powdery5:23 with Ben Deitch
In this video we'll learn about one of my favorite aspects of Kotlin!
The music is from Kevin MacLeod. If you're ever looking for excellent freely available music check out his website; it's awesome!
Fanfare for Space Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 3.0 License http://creativecommons.org/licenses/by/3.0/
Changes were made.
We've finished up our foundation object, but 0:00 rather than move right on to our tableau object, let's see how we can stop 0:02 typing out the entire string every time we want to refer to a suit. 0:06 To do this, we'll create a string property for each suit and 0:10 then use this property in both our deck and game model classes. 0:14 And since the suit is a property of the card, let's declare these new properties, 0:18 and our card class. 0:23 In Java, we would do this by using a static variable. 0:24 But in Collin there's no static anything. 0:28 Instead, Collin has companion objects. 0:31 Check it out. 0:35 Let's first add back in the brackets for our class. 0:36 Then inside the brackets, 0:40 let's declare a companion object by typing, companion object. 0:42 And then, adding brackets. 0:47 Each class can only have one companion object and 0:50 the companion object is basically just a place where we put 0:54 anything that would have been static in Java so, 0:57 inside this companion object is where will be creating the properties for our suits. 1:00 Let's start with the clubs property, 1:05 val clubs = Clubs then we'll do the diamonds property. 1:08 = Diamonds. 1:14 And the hearts property. 1:17 And finally, the spades property. 1:22 Now that we have our properties, 1:27 we can use them to replace the string literals in our game model and our deck. 1:29 Let's head over to our game model class and 1:33 replace these string literals with the properties we just created. 1:35 Card.clubs instead of diamonds. 1:43 It will be Card.diamonds. 1:48 Card.hearts and Card.spades. 1:55 And let's do the same thing for a Deck class. 2:03 So instead of Clubs, We'll use Card.clubs. 2:06 And Card.diamonds. 2:15 Card.hearts. 2:21 And Card.spades. 2:23 Nice. 2:26 It's looking pretty good, right? 2:27 But there is one thing I don't like about it, and 2:29 that's all these little card dot parts. 2:31 This project, and cards in general, are only ever going to have one clubs object. 2:35 Wouldn't it be nice if we could omit the class it belongs to? 2:42 Maybe, but we can't do that to access a property and 2:45 a companion object you need to start with the class it's in. 2:49 So instead let's head over to the card class and 2:53 see one small step we can take to make it better. 2:57 Let's start by cutting out all of our properties from the companion object. 3:00 And then, deleting the companion object. 3:06 Next, I want you to take a deep breath and 3:09 prepare to go where no Java developer has gone before. 3:12 Into a world outside of classes, where functions and 3:17 properties can exist without the shackles of a class. 3:21 Let us boldly go beyond the bounds of our card class, 3:25 as we please back in our problem not the citizens of the current class but 3:30 as citizens of our entire apps package. 3:36 Now, let's jump back to our deck class and 3:40 get rid of those card dots once and for all. 3:42 And let's do the same thing for our game model class. 3:49 And cotland whenever we declare a property or 3:57 a function outside of the class, it gets scoped to the entire package. 4:00 Meaning we can just use it from anywhere within that package. 4:06 It also means that there's nothing special about this card class, 4:09 we could declare these properties in the App class instead. 4:13 And that would be just fine. 4:17 And look, the main function's not part of a class either. 4:19 That said, it does make more sense to store these properties in the Card File. 4:23 But before we add them back, notice the icon next to our card class. 4:28 It's the class icon. 4:33 Now, let's cut, and paste our properties back into our card class. 4:35 And look at that! 4:43 The icon changed to the file icon. 4:44 Since card.kt contains more than just the card class 4:47 IntelliJ decides to show us the file icon, instead of the class icon. 4:53 It's not a big deal, but at least now you know why it's happening. 4:57 Being able to declare properties and 5:01 functions with package level scope is a huge boon to our development. 5:03 It lets us write less code and opens entirely new doors for 5:08 how we can structure our projects. 5:11 It might take a little getting used to but trust me it's well worth it. 5:13 In the next video, we'll pick up where we left off with creating an object 5:18 to represent a tableau pile. 5:21
You need to sign up for Treehouse in order to download course files.Sign up