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 ...
随机推荐
- Notepad++ 更换主题+字体
Notepad++ 更换主题 https://blog.csdn.net/haluoluo211/article/details/51922666 延伸: 挑选主题 https://blog.csdn ...
- odoo继承父类中的函数(方法)
使用_inherit继承父类重新设计新类时,可以调用父类中的函数,具体为: 第一步:获得某个模型('model.name')的数据集并进行某种集合操作(model_function),从而获得想要的数 ...
- 执行Java脚本firefox启动成功,不运行test方法,且提示NullPointerException
在ideal中新建maven项目,将录制好的Java脚本文件,直接复制到项目中,添加相关的依赖脚本. 代码不报错之后,运行录制好的Java脚本,启动了firefox之后,不执行test方法,报错Nul ...
- sql中表级约束和列级约束
sql中表级约束和列级约束,在SQL SERVER中, (1) 对于基本表的约束分为列约束和表约束约束是限制用户输入到表中的数据的值的范围,一般分为列级约束与表级约束.列级约束有六种:主键Primar ...
- python 之操作mysql 数据库实例
对于python操作mysql 数据库,具体的步骤应为: 1. 连接上mysql host 端口号 数据库 账号 密码2. 建立游标3. 执行sql(注意,如果是update,insert,delet ...
- Element 中表单非必填数据项 必须为数字的验证问题
Element-ui 的el-form组建中,自带基本的验证功能,比如某些项必填的验证,直接加入rules 规则中即可,如下实例: 在页面中书写如下: <el-form-item label=& ...
- MySQL中exists与in的使用
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录, ...
- JS答辩习题
php高中班javascript答辩题目 1 什么是javascript? 答:Javascript是一种专门设计用来增强网页交互性的脚本语言,它也是一种解释型语言. 2 Javascript与C语 ...
- C#中 哪些是值类型 哪些是引用类型
DateTime属于 结构类型,所以是 值类型 在 C#中 简单类型,结构类型,枚举类型是值类型:其余的:接口,类,字符串,数组,委托都是引用类型
- WCF使用安全证书验证消息加密
首先安装 服务端安全证书 代码如下: // 下面第一行是安装证书,第二行是将证书列入信任 makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN=lo ...