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 ...
随机推荐
- Java-多线程基本
Java-多线程基本 一 相关的概念 进程:是一个正在执行中的程序 每个进程都有一个执行的顺序,该顺序是一个执行路径,或者叫一个控制单元 线程:就是进程中的一个独立的控制单元,线程在控制着进程的执行 ...
- django 增加验证邮箱功能
在user文件夹下新建python包,utils 在包内新建文件email_send.py,其中包括验证字符串随机码的产生,数据库的存储和email的发送 # -*- coding: utf-8 -* ...
- 漫谈DOM 事件流的三个阶段
一丶 流 什么是流? 比如 react 中的单项数据流,Node.js 中的流,或者本文中的 DOM 事件流,都是流的具体体现.专业地讲,流是程序输入或输出的一个连续的字节序列:通俗地讲,流是有方向的 ...
- Linux系统——VMware克隆
克隆VMware 1. 关闭防火墙 2. 关闭selinux 3. 删除UUID和Mac地址 4.清空网卡缓存 5.关机 ===================== 关闭防火墙 #service ip ...
- kafka的javaapi生产者生产消息,消费者获取不到
zookeeper和kafka的日志没有出现什么报错 linux下kafka的命令行能生产并收到消费消息 但是在idea(windows环境下)中,调用api,获取不到数据,也生产不了数据,现象就是没 ...
- head中的title显示在body中
今天遇到一个问题,就是title中的内容会显示在body中 <head> <title>324234</title> </head> 网上搜了一下是说编 ...
- kivy sdl2 - ImportError: DLL load failed: 找不到指定的模块
from kivy.app import App from kivy.uix.button import Button class TestApp(App): def build(self): ret ...
- iOS开发之HelloKitty(移动社交平台项目)
iOS开发之HelloKitty(移动社交平台项目,2015.3,parishe)
- 查看ubuntu 各系统的内核版本
1.查看ubuntu版本号: cat /etc/issue 返回结果: Ubuntu 16.04.2 LTS \n \l 2.查看内核版本号: cat /proc/version 返回结 ...
- FFmpeg 入门(1):截取视频帧
本文转自:FFmpeg 入门(1):截取视频帧 | www.samirchen.com 背景 在 Mac OS 上如果要运行教程中的相关代码需要先安装 FFmpeg,建议使用 brew 来安装: // ...