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 />

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

var x: Int = xc<br />

var y: Int = yc<br />

}<br />

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

}<br />

x = x + dx<br />

y = y + dy<br />

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

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

As mentioned earlier in this chapter, you can create objects using a keyword new and then<br />

you can access class fields and methods as shown below in the example:<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 />

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

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

}<br />

}<br />

object Demo {<br />

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

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

// Move to a new location<br />

pt.move(10, 10);<br />

}<br />

23

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

Saved successfully!

Ooh no, something went wrong!