Classes and Constructors8:59 with andi mitre
In this video, we’ll take a look at how to create a Scala class along with primary and auxiliary constructors.
Auxiliary constructors -- In Scala, auxiliary constructors refer to secondary or additional constructors which accept different parameters from the primary constructor. They are defined using ‘def this()’ and always call either the primary constructor or another previously defined auxiliary constructor.
Case classes -- Case classes are standard Scala classes with some boilerplate functionality. When you create a case class the compiler creates a companion object along with an apply method. Constructor parameters become fields and the case class is immutable but contains implementations of hashcode(), toSting() and equals(). Case classes can be instantiated without a new keyword and they are ideal to use with pattern matching.
Hi in this video we'll learn how to implement classes in Scala. 0:00 Like Java, Scala can have multiple constructors which consist of primary and 0:04 auxiliary constructors. 0:09 Constructors in Scala may be a little different from what you may be used to. 0:11 Let's get a better idea on these topics by implementing some examples. 0:15 In Scala the primary constructor is blended within the class definition 0:20 whereas auxiliary constructors have to be defined using the this keyword. 0:24 Unlike Java you don't have to prepend a public key word before defining a class 0:29 as Scala classes have public visibility by default. 0:34 Let's create a new super hero class within our project. 0:37 We'll right click on source, select new Scala class. 0:41 If the parameters passed into the class definition of vars, 0:51 scala automatically turns them into public fields with getters and 0:54 setters which I initialize with a new instance of the class. 0:58 If you need a field with read only properties, use a val similarly 1:01 to Java if you want to make a field private prepend the private key word. 1:06 It is quite straightforward to use these fields in our class, 1:11 let's create a details method within our class, which prints out the name and 1:14 birthplace of our superhero. 1:18 Great, let's create a new incidence of our superhero class in our main object and 2:06 check out the results. 2:10 Awesome we created our first Calla Class. 2:45 Let's continue to create additional constructors for our class. 2:48 We'll implement a constructor that takes just the name of the super hero, 2:52 and another constructor with name and birth place. 2:58 We define additional constructors with the def and this keywords. 3:27 Inside our constructor we make a call to the primary constructor using this. 3:30 And passing in the values for the parameters of the primary constructor. 3:36 Remember an auxillary constructor can either call the primary constructor or 3:40 a previously defined auxillary constructor. 3:45 Let's go back to our superheroes object, and 4:15 make calls to our newly created auxiliary constructors. 4:18 Great as we previously mentioned Scala provides getters and setters for vars. 4:49 We can rewrite these getters and setters if we ever wish to. 4:53 Let's take a look at how to do that. 4:57 Within our superhero class we'll create a new field called hero age. 5:00 Then we'll rewrite the getter and setter methods for heroAge. 5:09 Our getter, which will return heroAge, and our setter. 5:18 We'll take a new integer variable which we can use to update the hero's age. 5:30 Great, we have rewritten the getter and setter for age respectively. 5:48 We would use these functions on any superhero instance. 5:52 Let's go back to our main class, create a new instance of superhero and 5:56 see how we can use our newly created getters and setters for age. 6:00 Well, first, set the Hulk's age to 30. 6:27 And we'll use the getter of method to return the Hulk's age. 6:34 Awesome, we're able to get back the value that we passed in for Hulk's age. 6:43 Scala also has a notion of case classes. 6:47 Case classes are typically used with pattern matching and 6:51 are very easy to use for matching and storing the contents of a class. 6:53 Let's create a case class for power stats of our superheroes. 6:58 Let's see how we can use our case class for 7:10 pattern matching the contents of our class. 7:12 Here we build the findHero function which matches the contents of a class to 8:17 a string of super hero names. 8:21 To use the class, we would simply call the findHero function, 8:23 passing in the PowerStats for the hero that we like to match. 8:27 As expected the value hero should contain thor. 8:46 Awesome. 8:57
You need to sign up for Treehouse in order to download course files.Sign up