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. postman 断言

    //断言 pm.test("message等于'操作成功'", function () { var jsonData = pm.response.json(); console.l ...

  2. DNS 知识点

    总结: DNS解析过程:主机查看本地缓存,然后向本地域名服务器进行递归查询,本地域名服务器向根,顶级,权限进行迭代查询   DNS 解析过程: 1.查看浏览器缓存 2.查看os缓存         w ...

  3. xadmin快速搭建一个后台管理系统

    环境:python3.5.2 ,django 2.0 ,xadmin2.0 Firsttly, 到 https://github.com/sshwsfc/xadmin/tree/django2上下载源 ...

  4. 1130 - Host '' is not allowerd to connect to this MySQL server,

    是因为缺少访问权限,在MySQL ->User表里 执行 INSERT INTO `user` VALUES ('%', 'root', '*81F5E21E35407D884A6CD4A731 ...

  5. centOS7下安装laravel + composer

    1.wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer chmod a+x /usr/local/bi ...

  6. shared_ptr的线程安全

    1.9 再论shared_ptr 的线程安全 虽然我们借shared_ptr 来实现线程安全的对象释放,但是shared_ptr 本身不是100% 线程安全的.它的引用计数本身是安全且无锁的,但对象的 ...

  7. POJ - 2699 The Maximum Number of Strong Kings (最大流+枚举)

    题意:有n(n<=10)个选手,两两之间打比赛,共有n*(n-1)/2场比赛,赢一场得1分.给出每个人最后的得分.求有多少个定义如下的strong king:赢了所有得分比自己高的人或本身就是分 ...

  8. motan rpc

    git :  帮助 文档 基本介绍 Motan是一套基于java开发的RPC框架,除了常规的点对点调用外,Motan还提供服务治理功能,包括服务节点的自动发现.摘除.高可用和负载均衡等.Motan具有 ...

  9. NIO 03

    1. 客户端要主动去连接:channel.connect(new InetSocketAddress("localhost",8888)); //用channel.finishCo ...

  10. 本地多张图片采用jmeter上传到ftp服务器的方法和获取服务器日志中某些关键字的基本方法

    测试需求: 本地图片上传到ftp服务器里和另外两台不同算法比对服务器进行比对,得出漏检和误检结果:这实际属于功能测试范畴. 测试思路: 第一种方法:使用实际场景的摄像机抓拍图片上传到服务器,用录屏软件 ...