How to implement multiple constructor with different parameters in Scala
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.
How to implement multiple constructor with different parameters in Scala的更多相关文章
- Constructor Overloading in Java with examples 构造方法重载 Default constructor 默认构造器 缺省构造器 创建对象 类实例化
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Cl ...
- Reads sequentially from multiple sources
/* * Copyright (C) 2016 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utili ...
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- 【反射】Reflect Class Field Method Constructor
关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...
- 如何实现多个接口Implementing Multiple Interface
4.实现多个接口Implementing Multiple Interface 接口的优势:马克-to-win:类可以实现多个接口.与之相反,类只能继承一个超类(抽象类或其他类). A class c ...
- scala2.10.x case classes cannot have more than 22 parameters
问题 这个错误出现在case class参数超出22个的时候. case classes cannot have more than 22 parameters 在scala 2.11.x版本以下时c ...
- Beginning Scala study note(7) Trait
A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
随机推荐
- python定义函数时默认参数注意事项
如果在调用一个函数时,没有传递默认参数,则函数内的默认参数是对函数的默认参数属性__defaults__的引用, 如 def func(arg1=[]): arg1.append(2) 调用func时 ...
- python迟邦定
1.绑定 将函数体和函数调用关联起来,就叫绑定 2.迟绑定 在程序运行之前(也就是编译和链接时)执行的绑定是早绑定,迟绑定(late binding)是发生在运行时. 3.实例说明 def outer ...
- 【IPC进程间通讯之中的一个】邮槽MailSlot
IPC进程间通信+邮槽MailSlot IPC(Inter-Process Communication.进程间通信). 现代计算机採用虚拟内存机制,为进程提 ...
- 小tip: 使用SVG寥寥数行实现圆环loading进度效果
二.正文 设计师设计了一个图片上传圆环loading进度效果.如下截图: 首先,CSS3是可以实现的,以前写过一篇转大饼的文章:“CSS3实现鸡蛋饼饼状图loading等待转转转”.原理跟这个一模一样 ...
- python学习笔记(二十二)实例变量、实例方法、类变量、类方法、属性方法、静态方法
实例变量:在类的声明中,属性是用变量来表示的.这种变量就称为实例变量,也就是成员变量. 实例方法:在类中声明的方法,例如:my(self),必须实例化之后才可以使用,否则会报错. 类变量:公共的变量, ...
- 使用Robomongo连接数据库不成功:没有启动MongoDB
启动MongoDB ➜ ~ mongod --07T16:: I CONTROL [initandlisten] MongoDB starting : pid= port= dbpath=/data/ ...
- Spark机器学习系列之13: 支持向量机SVM
Spark 优缺点分析 以下翻译自Scikit. The advantages of support vector machines are: (1)Effective in high dimensi ...
- Uboot mmc命令解析&NAND flash uboot命令详解
转载:http://blog.csdn.net/simonjay2007/article/details/43198353 一:mmc的命令如下: 1:对mmc读操作 mmc read addr bl ...
- 创建发布Webservice以及wsimport工具
一. 通过wsimport生成本地代理调用WebService 1.推荐的访问服务方式 WebService已纳入w3c规范,其他的平台都支持该规范 :J2EE\Php\.NET都支持wsimport ...
- Magento 本地搬家至网络服务器步骤
1.将本地的Magento的数据库备份下来. 2.将本地的Magento网站资料做成ZIP资料. 3.将Magento网站 ZIP资料上传到服务器的域名指向的资料夹内. 4.将ZIP解压出来,移动到域 ...