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.

  • 相关阅读:
    机器视觉会议以及牛人
    转图像偏微分方程不适定问题
    浅析点对点(End-to-End)的场景文字识别(图片文字)
    图像处理数学方法
    计算机视觉代码合集
    计算机视觉与模式识别代码合集第二版one
    计算机视觉与模式识别代码合集第二版two
    计算机视觉与模式识别代码合集第二版three
    计算机视觉、模式识别方面稍微容易中的期刊
    C#6.0 VS2015
  • 原文地址:https://www.cnblogs.com/ericchen/p/3427333.html
Copyright © 2011-2022 走看看