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. 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件

    本文背景: 在编程中,很多Windows或C++的内存函数不知道有什么区别,更别谈有效使用:根本的原因是,没有清楚的理解操作系统的内存管理机制,本文企图通过简单的总结描述,结合实例来阐明这个机制. 本 ...

  2. git学习------>从SVN迁移到Git之后,项目开发代码继续在SVN提交,如何同步迁移之后继续在SVN提交的代码到Git?

    最近逐步逐步的将公司的项目都从SVN往Git迁移了,但是想团队成员都能够一步到位就迁移到Git是不可能的,因为还有大部分人都还不会Git,所以整个过渡过程估计得大半年. 因此导致虽然项目迁移过来了,但 ...

  3. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  4. JMS术语

    Provider(MessageProvider):生产者Consumer(MessageConsumer):消费者PTP:Point to Point,即点对点的消息模型Pub/Sub:Publis ...

  5. Cloudflare发布全球最快的DNS

    宣布1.1.1.1:速度最快,隐私优先的消费者DNS服务   Cloudflare的使命是帮助建立更好的互联网.今天我们很高兴能够在推出1.1.1.1--互联网最快,首先保护隐私的消费者DNS服务的同 ...

  6. Oracle TRCA 工具(转)

    本篇文章主要介绍了"Oracle TRCA 工具 说明 ",主要涉及到Oracle TRCA 工具 说明 方面的内容,对于Oracle TRCA 工具 说明 感兴趣的同学可以参考一 ...

  7. python2和python3中range的区别

    参考自 python2和python3中的range区别 - CSDN博客 http://blog.csdn.net/xiexingshishu/article/details/48581379 py ...

  8. Java并发编程实战4章

    第4章主要介绍如何构造线程安全类. 在设计线程安全类的过程中,需要包含以下三个基本要素: 找出构成对象状态的所有变量. 找出约束状态变量的不变性条件. 建立对象状态的并发访问管理策略. 构造线程安全类 ...

  9. 爬虫——请求库之requests

    阅读目录 一 介绍 二 基于GET请求 三 基于POST请求 四 响应Response 五 高级用法 一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,reque ...

  10. Android ExpandableListActivity

    ======MainActivity.java===================================== package com.zys.myexpandablelistactivit ...