Tuesday, October 25, 2016

Programming in Scala Note 4/5/6

1.

The way you make members public in Scala is by not explicitly specifying any access modifier.
Public is Scala's default access level.
2.
one way in which Scala is more object-oriented than Java is that classes in
Scala cannot have static members. Instead, Scala has singleton objects. A singleton object definition
looks like a class definition, except instead of the keyword class you use the keyword object.
A singleton object is more than a holder of static methods, however. It is a first-class object.
One difference between classes and singleton objects is that singleton objects cannot take parameters,
whereas classes can. Because you can't instantiate a singleton object with the newkeyword, you have
no way to pass parameters to it.
3.
in Scala, you
can name .scala files anything you want, no matter what Scala classes or code you put in them.
4. ==First check the left side for null. If it is not null,

call the equals method.
scala> 1==1.0

res8: Boolean = true
5. 
In Java, classes have
constructors, which can take parameters; whereas in Scala, classes can take parameters directly. The

Scala notation is more concise—class parameters can be used directly in the body of the class
6.
 class Rational(n: Int, d: Int) {
require(d != 0)
override def toString = n + "/" + d
}
7. 
In Scala, every auxiliary constructor must invoke another constructor of the same class as its first
action. In a Scala class, only the primary
constructor can invoke a superclass constructor.
8.
 scala> implicit def intToRational(x: Int) = new Rational(x)

No comments:

Post a Comment