28.08.2016 Views

scala_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scala<br />

<br />

Implicit classes may not be any method, member or object in scope with the same<br />

name as the implicit class.<br />

Singleton Objects<br />

Scala is more object-oriented than Java because in Scala, we cannot have static members.<br />

Instead, Scala has singleton objects. A singleton is a class that can have only one instance,<br />

i.e., Object. You create singleton using the keyword object instead of class keyword. Since<br />

you can't instantiate a singleton object, you can't pass parameters to the primary constructor.<br />

You already have seen all the examples using singleton objects where you called Scala's main<br />

method.<br />

Following is the same example program to implement singleton.<br />

import java.io._<br />

class Point(val xc: Int, val yc: Int) {<br />

var x: Int = xc<br />

var y: Int = yc<br />

def move(dx: Int, dy: Int) {<br />

x = x + dx<br />

y = y + dy<br />

}<br />

}<br />

object Demo {<br />

def main(args: Array[String]) {<br />

val point = new Point(10, 20)<br />

printPoint<br />

def printPoint{<br />

println ("Point x location : " + point.x);<br />

println ("Point y location : " + point.y);<br />

}<br />

}<br />

}<br />

28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!