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的更多相关文章

  1. Constructor Overloading in Java with examples 构造方法重载 Default constructor 默认构造器 缺省构造器 创建对象 类实例化

    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Cl ...

  2. Reads sequentially from multiple sources

    /* * Copyright (C) 2016 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utili ...

  3. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  4. 【反射】Reflect Class Field Method Constructor

    关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...

  5. 如何实现多个接口Implementing Multiple Interface

    4.实现多个接口Implementing Multiple Interface 接口的优势:马克-to-win:类可以实现多个接口.与之相反,类只能继承一个超类(抽象类或其他类). A class c ...

  6. 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 ...

  7. Beginning Scala study note(7) Trait

    A trait provides code reusability in Scala by encapsulating method and state and then offing possibi ...

  8. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  9. Dart 基础重点截取 Dart 2 20180417

    官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...

随机推荐

  1. myeclipse自动设置类和方法的注释(快捷键)

    类的注释 第一步:找到Window→Preferences→Java→Code Style→Code Templates→Comments→Types 第二步:编辑Pattern,我一般用的模板 /* ...

  2. 消息编解码Nanopb - protocol buffers

    Google Protocol Buffer 有各种版本的代码包,Python C/C++.JAVA.C.OBJ-C..NET等,嵌入式设备中使用的protobuf版本,我们选择的是nanoprobu ...

  3. python学习笔记(二十四)继承与封装

    继承(extends)就是把多个类中相同的成员给提取出来定义到一个独立的类中,然后让这多个类和该独立的类产生一个关系,这多个类就具备了这些类容,这个关系就叫做继承. 实现继承的类称为子类,也叫派生类, ...

  4. CentOS7.2.1511安装docker

    一.使用之前的安装方法不好使,删除的安装包第一次安装的时候直接使用epel中的源,安装完成后服务无法启动然后删除重新安装,参考文档:https://www.cnblogs.com/yufeng218/ ...

  5. 用tophat和cufflinks分析RNAseq数据[转载]

    转自:http://blog.sciencenet.cn/home.php?mod=space&uid=635619&do=blog&id=884213 //今天看到一篇非常好 ...

  6. 记录:python读取excel文件

    由于最近老是用到python读取excel文件,所以特意记录一下python读取excel文件的大体框架. 库:xlrd(读),直接pip安装即可.想要写excel文件的话,安装xlwd库即可,也是直 ...

  7. 20165324 Java实验一

    20165324 实验一 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 1 ...

  8. UVALive - 7740 Coding Contest 2016 青岛区域赛 (费用流)

    题意:每个点i有\(s_i\)个人和\(b_i\)份食物,每个人都要找到一份食物.现在有M条有向边,从点i到点j,容量为c,第一次走过不要紧,从第二次开始就要承担\(p(0<p<1)\)的 ...

  9. Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)

    题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...

  10. java反射 - getXXX 与 getDeclaredXXX

    1.getXXX 和 getDeclaredXXX java 里 Class<?> 有下面这些方法: 类似的方法有: 2.getMethod(s) 和 getDeclaredMethod( ...