Demonstrates,
> run-main e11_Classes.a11_101.a15_ConsDemo
[info] Compiling 1 Scala source to /Users/raj/gitws/scalaexamples/target/scala-2.10/classes...
[info] Running e11_Classes.a11_101.a15_ConsDemo
(ClassWithMemberVariable : Members are -> ,val1,, ,101)
(Method arguments are -> ,WrappedArray(val1, val2, val3))
ClassWithoutMemberVariable
[success] Total time: 1 s, completed Apr 29, 2016 9:45:39 PM
Here WrappedArray wraps Java Array
- [] : Parameterized Types
- () : Argument List for Member Variables
- Unit
- class
- object
- and more...
package e11_Classes.a11_101
//** Member variables are given in the argument list...
// ie (mArg1, mArg2)
//** Outermost curly braces {} also acts as Primary Constructor #PS:12
class ClassWithMemberVariable(mArg1: String, mArg2: Int) {
//** Square bracket([]) is used to Parameterize types
//** Equal to (=) Separates Method signature from Method Body
//** String* : Star is appended to denote variable length argument
// list
//** Seq : Is an Abstraction for Collections that can be
// iterated in a fixed order
def demoFunction(arg1: String*): Seq[String] = {
println("ClassWithMemberVariable : Members are -> ", mArg1,
", ", mArg2)
println("Method arguments are -> ", arg1)
arg1
}
}
// **Argument List..ie () not needed in the Class definition
// if we do not have any member variables
class ClassWithoutMemberVariable {
// **When a function do not return anything
// its return type can be denoted by Unit
def demoFunction : Unit = {
println("ClassWithoutMemberVariable")
}
//** Curly braces {} used to enclose the body of the method
// can be omitted if the method has only single expression
def sayHello = "Hello"
}
// ** object : An object will have only one instance and there
// conforms to the Singleton pattern
object a15_ConsDemo {
def main(args: Array[String]) {
val obj1 = new ClassWithMemberVariable("val1", 101)
obj1.demoFunction("val1", "val2", "val3")
//** Note here the absence of Argument list
//...round brackets ie..()
val obj2 = new ClassWithoutMemberVariable
obj2.demoFunction
}
}
> run-main e11_Classes.a11_101.a15_ConsDemo
[info] Compiling 1 Scala source to /Users/raj/gitws/scalaexamples/target/scala-2.10/classes...
[info] Running e11_Classes.a11_101.a15_ConsDemo
(ClassWithMemberVariable : Members are -> ,val1,, ,101)
(Method arguments are -> ,WrappedArray(val1, val2, val3))
ClassWithoutMemberVariable
[success] Total time: 1 s, completed Apr 29, 2016 9:45:39 PM
Here WrappedArray wraps Java Array
No comments:
Post a Comment