Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs.

Similar presentations


Presentation on theme: "More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs."— Presentation transcript:

1 More about Classes and Objects

2 Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs (but you shouldn’t use dollar signs) scala> val simpleName = 5 simpleName: Int = 5 Names composed of “punctuation marks” (not including parentheses, brackets, braces, or the period) scala> val @#%^&* = 10 @#%^&*: Int = 10 Names composed of letters and underscores, then an underscore, then punctuation marks scala> val letters_#^&* = 15 letters_#^&*: Int = 15 2

3 Use of names Suppose you wanted to implement your own Fraction class This works: scala> case class Fraction(num: Int, denom: Int) { | def multiply(that: Fraction) = | new Fraction(this.num * that.num, this.denom * that.denom) | } defined class Fraction scala> val f = new Fraction(1, 3) f: Fraction = Fraction(1,3) scala> f.multiply(f) res0: Fraction = Fraction(1,9) But this is nicer: scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(this.num * that.num, this.denom * that.denom) | } defined class Fraction scala> val f = new Fraction(1, 3) f: Fraction = Fraction(1,3) scala> f * f res2: Fraction = Fraction(1,9) 3

4 Misuse of names A lot of methods in Scala have names composed of “punctuation marks” For example, ==, >=, +: Used carefully, such names can increase the readability of your program But a name like @#%^&* is probably never good! “Just because you can do something, doesn’t mean you should!” 4

5 What’s this ? Objects contain variables ( val s and var s) and methods You can explicitly say “I want the variable in this object” You can do so with the keyword this : scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(this.num * that.num, | this.denom * that.denom) | } But you can also implicitly refer to variables in “this” object Unqualified variables are first looked for in the local scope, then among the variables of “this” object scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(num * that.num, denom * that.denom) | } defined class Fraction 5

6 More about this An instance method is a method that belongs to an object To “call” an instance method, you are asking the object to execute the method for you The keyword this acts like a variable whose value is the object executing the method There are times you want your method to “call” some other method, and tell the method who is “calling” it Use the keyword this as an argument to the other method 6

7 Companion objects When you declare an object, you are actually doing two things: You are creating a class You are creating the one and only one object of that class When you declare a class, you are making a template from which to create zero or more objects Every object has its very own copies of the variables declared in that class Sometimes, however, you want some variables whose value is the same for every object of that class This is the purpose of a companion object A companion object is an object with the same name as a class, and defined on the same file as the class 7

8 Example companion object You want to describe a number of janitors Every janitor gets the same salary Solution: object Janitor { var salary: Double = 15.00 } class Janitor(name: String) 8

9 Inheritance When one class extends (inherits from) another class, it gets all the variables and methods defined in that class class Mammal { val hasBackbone = true val bloodTemperature = "warm" def nurse { println("Giving milk") } } class Dog(name: String) extends Mammal { def makeSound { println("Arf! Arf!") } A dog: Has a backbone, has warm blood, can nurse, and can make a sound 9

10 Overriding methods If your class inherits a method that you don’t like, you can “override” it (keyword override ) with another version class GirlDog extends Dog class BoyDog extends Dog { override def nurse { } // Nothing happens! Every class implicitly extends Any, or explicitly extends a class that (sooner or later) extends Any Every class inherits a toString method from Any print and println use toString when printing objects Therefore, any object can be printed—using the inherited toString method If you don’t like the results, you can override toString (Within the Dog class) override def toString = name 10

11 Base class initialization Whales are mammals, but they don’t have legs We might want to add a legCount variable to our consructor for Mammals class Mammal(legCount: Int) { … } Now we cannot do this: class Dog(name: String) extends Mammal { … } class Whale extends Mammal { … } We have to do this instead: class Dog(name: String) extends Mammal(4) { … } class Whale extends Mammal(0) { … } Whenever we create an object, what really happens is: We create an object of the superclass type We extend the object with anything declared in our class 11

12 The End 12

13 For each “clicker” question, I’ve been giving you 20 seconds to answer A. That isn’t enough time B. That’s usually about right C. That’s too long, in general 13


Download ppt "More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs."

Similar presentations


Ads by Google