Presentation is loading. Please wait.

Presentation is loading. Please wait.

10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”

Similar presentations


Presentation on theme: "10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”"— Presentation transcript:

1 10-Sep-15 Classes

2 Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects” Functional means that functions, like numbers, are values We’ll talk more about the functional aspects later Every value, such as 27.3 or "Hello ", is an object You can “talk” to objects, using dot notation: scala> "Hello".toLowerCase res12: String = hello scala> "Hello".startsWith("He") res13: Boolean = true Every program you write starts with an object you define A class (or type) is a description of a set of objects The type of "Hello" is String Most of your programs will use several classes and objects

3 Objects If you want just one object of a certain type (a singleton), you can define it like this: object NameOfObject { …code… } The code is executed the first time you refer to the object Variables ( val and var ) are declared Methods ( def ) are defined “Loose” code (not contained in a method) is executed You can refer to the fields (variables) and methods of the object using dot notation NameOfObject.variable NameOfObject.method ( arg1, …, argN ) Within the object definition, you don’t need to use dot notation to refer to the fields and methods of “this” object

4 Example object object USA { val numberOfContiguousStates = 48 val numberOfStates = numberOfContiguousStates + 2 var president = "George W. Bush" def isContiguous(state: String) = state != "Alaska" && state != "Hawaii" def isNotContiguous(state: String) = !isContiguous(state) } println("The USA has " = USA.numberOfStates + " states.") USA.president = "Barack Hussein Obama" val here = "Pennsylvania“ println(here + (if (isContiguous(here) " is " else " is not ") + " a continental state.")

5 Simple programs The simplest complete program consists of one object containing a main method The main method looks exactly like def main(args: Array[String]) { … code in main method… } A “Hello World” program looks like this: object SimpleProgram { def main(args: Array[String]) = { println("Hello from a Scala program.") } } To run the program, put it on a file with the extension.scala (for example, simple.scala ), then say scala simple.scala

6 Order of execution When you run a Scala program, things happen in approximately this order: 1.Scala figures out the types of everything, and gives default values to variables ( 0 for numbers, false for booleans, null for most other kinds of object) 2.Working from top to bottom, Scala evaluates all the val s and var s and defines all the methods ( def s) 3.If there is a main method, Scala executes (runs) it The detailed rules are quite complicated C and C++ require definitions to be in a certain order Scala gives you a lot of freedom in ordering your definitions and variables

7 About args: Array[String] Array[String] means “an array of Strings” When you run the program, you can give it some Strings as input Arrays are covered elsewhere in this course, so you don’t need to worry about them right now Just for completeness, here’s a simple example object SimpleProgramWithArguments { def main(args: Array[String]) { println("Here are my arguments:") for (word <- args) { println(word) } } }

8 Classes When you want more than one object of a given kind, you use a class Because these objects will differ in some respects, a class is usually defined with parameters: class NameOfClass ( parameters ) { …code… } The code is evaluated each time you create a new object Variables ( val and var ) are declared Methods ( def ) are defined (not called) “Loose” code (not contained in a method) is executed The type of each parameter must be specified Each parameter may be marked as val or var val means you can read the value from outside the class definition var means you can read or write the value from outside neither means you can only access the variable from inside the class

9 Example class class Planet(val name: String, val orderFromSun: Int, populationIn2011: Long) { def population(year: Int) = { require(year >= 2011) var pop = populationIn2011.toDouble for (y <- 2012 to year) { pop = 1.008 * pop } pop.toLong } } val earth = new Planet("Earth", 3, 7000000000L) val mars = new Planet("Mars", 4, 0) val planet = earth println("Population of " + planet.name + " in 2050 will be " + planet.population(2050))

10 Example class with object class Planet is exactly as defined on previous slide Add this object: object SolarSystem { def main(args: Array[String]) { val earth = new Planet("Earth", 3, 7000000000L) val mars = new Planet("Mars", 4, 0) val planet = earth println("Population of " + planet.name + " in 2050 will be " + planet.population(2050)) } } Now we have to run the main method explicitly: SolarSystem.main(Array())

11 Running from the command line If your program consists of just one object, you can run it by specifying the name of the file it is on: scala SolarSystem.scala It is conventional to give the file the same name as the object (plus the.scala extension), but this is not required If you just say scala without a file name, this starts the REPL If your program contains classes or additional objects, you have to compile it (with scalac ) and then run it (with scala ) dave$ scalac SolarSystem.scala dave$ scala SolarSystem Population of Earth in 2050 will be 9551218761 Macintosh:Scala_programs When you compile a program, you get some number of.class files, many with a $ in their name.class files are binary files, runnable on the Java Virtual Machine (JVM)

12 Arguments to a program Here’s a program from an earlier slide: object SimpleProgramWithArguments { def main(args: Array[String]) { println("Here are my arguments:") for (word <- args) { println(word) } } } When you run it, everything after the file name is treated as a space-separated array of Strings dave$ scala SimpleProgramWithArguments.scala This is CIS 591 Here are my arguments: This is CIS 591

13 13 The End


Download ppt "10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”"

Similar presentations


Ads by Google