Fancy Parameters. Class parameters When you define a class, you typically give it parameters scala> class Point(val x: Double, val y: Double) defined.

Slides:



Advertisements
Similar presentations
Recursion in Scala. 2 Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a.
Advertisements

Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
COMP More About Classes Yi Hong May 22, 2015.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
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”
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.
The Scala API Application Programmer’s Interface.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
ECE122 Feb. 22, Any question on Vehicle sample code?
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
CSC 142 Computer Science II Zhen Jiang West Chester University
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Cases and Classes and Case Classes And Other Miscellany 16-Dec-15.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Getting from Scripts to Applications Donald J. Sipe | Refresh Jacksonville | November 11 th 2008.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
9.1 Class (static) Variables and Methods
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Classes and Objects and Traits
The dirty secrets of objects
Using local variable without initialization is an error.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Class Structure 16-Nov-18.
Creating Objects in a Few Simple Steps
Class Structure 28-Nov-18.
Week 6 Object-Oriented Programming (2): Polymorphism
CS1316: Representing Structure and Behavior
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Passing Parameters by value
Class Structure 7-Dec-18.
Cases and Classes and Case Classes
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Barb Ericson Georgia Institute of Technology Oct 2005
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Classes and Objects and Traits
Method Overriding and method Overloading
Chapter (3) - Procedures
Common pattern/tool: Accumulator loops
Class Circle { Float xc, yc, radius;
Presentation transcript:

Fancy Parameters

Class parameters When you define a class, you typically give it parameters scala> class Point(val x: Double, val y: Double) defined class Point scala> val p = new Point(3.7, 4.2) p: Point = scala> p.x res11: Double = 3.7 Declaring a parameter with val makes the value accessible from outside the class Declaring a parameter with var makes it accessible and changeable from outside the class Use this with extreme care—basically, only when changing the value to some garbage value cannot cause an error Declaring a parameter with neither val nor var makes it private to the class (inaccessible from outside) Note: Objects created with object (rather than from a class) don’t take parameters 2

Arbitrary number of parameters A class can take an arbitrary number of parameters scala> class Stuff(val things: Int*) defined class Stuff scala> val s = new Stuff(3, 5, 7) s: Stuff = scala> s.things res15: Seq[Int] = WrappedArray(3, 5, 7) scala> for (n <- s.things) print(n + " ") The * notation can be used only on the last parameter Here’s an example of a nontrivial use: scala> var sum = 0; for (n <- s.things) sum += n sum: Int = 15 Later we’ll learn about better ways to do things like this 3

Methods can have an arbitrary number of parameters scala> def vocabulary(words: String*) { | println("The class of \"words\" is " + words.getClass) | for (word <- words) println(word) | } vocabulary: (words: String*)Unit scala> vocabulary("one", "two", "three") The class of "words" is class scala.collection.mutable.WrappedArray$ofRef one two three The type you get inside the method is a “WrappedArray,” but you can treat it as a list (or use.toList on it) 4

case classes When you declare a class as a case class, you get extra features You don’t have to use new to create one It has a better toString method (though maybe still not what you want) Its objects can be used in match expressions scala> case class Point(val x: Double, val y: Double) defined class Point scala> val p = Point(3.7, 4.2) p: Point = Point(3.7,4.2) scala> p match { | case Point(a, b) => s"It's a Point at $a, $b" | case _ => "It's not a Point" | } res21: String = It's a Point at 3.7, 4.2 You probably should only use case classes when you want the extra features, but there seems to be little harm in overusing them 5

Named arguments to a class scala> case class Card(suit: String, value: Int) defined class Card Notice that the above is a complete definition of a class To add features to a class, you need a { on the same line scala> val card = Card(value = 2, suit = "Clubs") card: Card = Card(Clubs,2) Notice that when using parameter names, the order of parameters does not matter 6

Named arguments to a method scala> def number(hundreds: Int, tens: Int, ones: Int) = | 100 * hundreds + 10 * tens + ones number: (hundreds: Int, tens: Int, ones: Int)Int scala> number(2, 3, 5) res2: Int = 235 scala> number(tens = 3, hundreds = 2, ones = 5) res3: Int = 235 scala> number(2, ones = 5, tens = 3) res4: Int = 235 When you use names for only some of the arguments, the named arguments must come after the ones in the correct position 7

Constructors When you define a class, you are writing a constructor A constructor is used to create new objects of a class Create a new object (or “instance”) with the word new You can omit the word new for a case class When you create an object, all the “loose” code in the constructor is executed scala> class Person(name: String) { | println("Creating person " + name) | def getName = name | } defined class Person scala> val jane = new Person("Jane") Creating person Jane jane: Person = scala> jane.getName res1: String = Jane 8

Auxiliary constructors You can have additional constructors defined within the “primary” constructor Use the word this as the name of the constructor The constructors must be different in the number or types of arguments The first thing any auxiliary constructor must do is call another constructor (again, using the word this ) scala> case class Person(val name: String, val age: Int) { | override def toString = s"$name is $age years old" | def this(name: String) { | this(name, 0) | println("It's a baby!") | } | } defined class Person scala> new Person("Jane", 23) res0: Person = Jane is 23 years old scala> new Person("Frank") It's a baby! res1: Person = Frank is 0 years old 9

The End