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. django中实现微信消息推送

    -公众号(不能主动给用户发消息) -认证的公众号:需要营业执照,需要交钱,可以发多篇文章 -未认证的公众号:一天只能发一篇文章 -服务号(微信推送) -需要申请,需要认证 -可以主动给用户推送消息 - ...

  2. LocalActivityManager与ActivityGroup

    Helper class for managing multiple running embedded activities in the same process. This class is no ...

  3. 程序猿Web面试之jQuery

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/powertoolsteam/article/details/32325013  又到了一年一度的 ...

  4. 查看pc ip地址

    1.使用Windows+R键打开“运行”窗口,然后输入CMD进入命令提示窗口 2.进入命令窗口之后,输入:ipconfig/all 回车即可看到整个电脑的详细的IP配置信息

  5. 创建Java不可变类

    不可变(immutable)类的意思是创建该类的实例后,该实例的Field是不可改变的,Java提供的8个包装类和java.lang.String类都是不可变类. 如果需要创建自定义的不可变类,可遵守 ...

  6. SQL各种连接——自连接、内连接、外连接、交叉连接的使用

    首先准备了两个表 (Student 和 Course),其中 Student 表中的 C_S_Id 字段为外键列,关联的是 Course 表的 C_Id 主键列. 内连接(inner join):满足 ...

  7. Java设计原则—接口隔离原则(转)

    接口隔离原则 Interface Segregation Principle    定义: 客户端不应该依赖它不需要的接口 类间的依赖关系应该建立在最小的接口上 我们可以把这两个定义概括为一句话:建立 ...

  8. hu3613 Best Reward

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目: Best Reward Time Limit: 2000/1000 MS (Java/Oth ...

  9. CCTableView

    今天用到TableView, 我就来记录一下....这些都是在网上找到了资料 //首先 继承 : public cocos2d::extension::CCTableViewDelegate,publ ...

  10. QML Image Element

    QML Image Element The Image element displays an image in a declarative user interface More... Image元 ...