How to implement multiple constructor with different parameters in Scala
2013-11-16 23:28
309 查看
Using scala is just another road, and it just like we fall in love again, but there is some pain you will have to get, and sure we deserve that to achieve the goal we want.
In Scala, there is kind of constructors named auxiliary contructor, which have the special properties:
On the first line of the constructor’s body, call either another auxiliary constructor that has been declared before before it or the primary constructor.
That means if we want to implement the code in Java listed below:
class Test{
private String name;
private int age;
public Test(String name){
this.name = name;
}
public Test(String name, int age){
this.name = name;
this.age = age;
}
}
We will have to write the scala code like this:
class Test(name :String, age :Int) {
def this(name :String) = this(name, 0);
}
And it's OK, but how can we want to implement things like following:
[Java]
class Test{
Test(String information){
//parsing the information, and then set the name and age properties
}
Test(String name, int age){...}
}
OK, sure scala can handle this kind of boring thing, but how? You can have a test in scala, with the auxiliary constructor rule, you can do nothing like works in java. Because classes in scala always have a primary construtor, and it is the root constructor for each auxiliary one.
Answer is using object, the singleton Factory guy. Let's get to solve the problem using this great thing:
object Test {
def apply(information :String) :Test = {
//doing the parsing work, getting name and age
//then return the instance
new Test(name, age)
}
}
With the help of object factory, you can create instance as :
val test = Test(information)
So it works, but then you have another situation, that you want create constructors as in Java:
class Test {
Test(String a){...}
Test(int b){...}
}
how can we handle this??
Just do if you think:
case class Test1(name :String)
case class Test2(age :Int)
object MainObject {
def apply(name :String) :Test1 = new Test1(name)
def apply(age :Int) :Test2 = new Test2(age)
}
then you can create instance like:
val test1 = MainObject(name)
val test2 = MainObject(age)
and you can sure get fields from each variable, e.g: test1.name, or test2.age
"object" in scala is singleton, we should avoid sharing things in this kind of class.
In Scala, there is kind of constructors named auxiliary contructor, which have the special properties:
On the first line of the constructor’s body, call either another auxiliary constructor that has been declared before before it or the primary constructor.
That means if we want to implement the code in Java listed below:
class Test{
private String name;
private int age;
public Test(String name){
this.name = name;
}
public Test(String name, int age){
this.name = name;
this.age = age;
}
}
We will have to write the scala code like this:
class Test(name :String, age :Int) {
def this(name :String) = this(name, 0);
}
And it's OK, but how can we want to implement things like following:
[Java]
class Test{
Test(String information){
//parsing the information, and then set the name and age properties
}
Test(String name, int age){...}
}
OK, sure scala can handle this kind of boring thing, but how? You can have a test in scala, with the auxiliary constructor rule, you can do nothing like works in java. Because classes in scala always have a primary construtor, and it is the root constructor for each auxiliary one.
Answer is using object, the singleton Factory guy. Let's get to solve the problem using this great thing:
object Test {
def apply(information :String) :Test = {
//doing the parsing work, getting name and age
//then return the instance
new Test(name, age)
}
}
With the help of object factory, you can create instance as :
val test = Test(information)
So it works, but then you have another situation, that you want create constructors as in Java:
class Test {
Test(String a){...}
Test(int b){...}
}
how can we handle this??
Just do if you think:
case class Test1(name :String)
case class Test2(age :Int)
object MainObject {
def apply(name :String) :Test1 = new Test1(name)
def apply(age :Int) :Test2 = new Test2(age)
}
then you can create instance like:
val test1 = MainObject(name)
val test2 = MainObject(age)
and you can sure get fields from each variable, e.g: test1.name, or test2.age
"object" in scala is singleton, we should avoid sharing things in this kind of class.
相关文章推荐
- How to run the multiple tests with different weight Groovy way
- How to implement scrolling image with javascript in web page
- how to implement WaitForMultipleObjects in linux
- How to effectively work with multiple files in Vim?
- How to implement multiple channels in wifi module of ns3
- scala: How to write a simple HTTP GET request client in Scala (with a timeout)
- How to sum values in an array with different hash
- How to implement the similar mechanism with Java final in c++?
- How to add multiple static library files with different architectures to Xcode
- how to implement the WaitForMultipleObjects in linux
- How to pass multiple parameters in PowerShell invoke-restmethod
- how to select data in multiple datatables after using join,group by and order by in Linq
- how to interact with java object in cpp
- how to deal with the situation that the language support option disappears in linux ubuntu
- illegal to have multiple occurrences of contentType with different values
- How to define multiple controllers for one view in angularJS?
- How to Implement Ajax in WordPress Themes
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-2)
- EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework