zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    oracle 当行函数 日期
    veridata实验举例(1)验证TCUSTMER与TCUSTORD两节点同步情况
    sdut1730 数字三角形问题(dp入门题)
    Android4.0 Design之UI设计易犯的错误2
    怎样提高团队管理能力6
    Effective C++ 29-33
    内存补齐序列一:关于内存对齐和填充
    【 D3.js 入门系列 --- 10.1 】 简化 GeoJSON 文件
    Android TrafficStats类的使用
    新手上路:Laravel-控制器基础
  • 原文地址:https://www.cnblogs.com/ericchen/p/3427333.html
Copyright © 2011-2022 走看看