Scala:object (single case)

Source: Internet
Author: User

Scala Opening (directory)

With Java, you should be familiar with the singleton pattern, declaring a private static object of its own type inside the class, and then returning it with a static method. There is no static in Scala, but it also gives us a way to implement a singleton pattern, which is object (don't mix, not the root of everything)
In Scala, when using singleton mode, in addition to the defined class, you define an object with the same name, and the difference between it and the class is that object object cannot take arguments, first see implementation code

/** This is what we define as a class constructor that we define as private, preventing direct invocation of the class's constructs to create objects * / class statictest private {  Private defAdd_ (X:int, y:int): Int = {returnx + y}Private defSub_ (X:int, y:int): Int = {returnx + y}}/** This is the definition of a singleton pattern, with the same name as the class, with no parameters */ Object statictest{//Internal declaration of a Statictest class instance object  ValSingleobj =NewStatictest//applay method, executed when Statictest () is executed  defApply () ={println ("-------Apply--------")  }defAdd (x:int,y:int): int={//Call the method of the Statictest class    returnSingleobj.add_ (x, y)}//Call the method of the Statictest class  defSub (x:int,y:int): Int ={returnSingleobj.sub_ (x, y)}}

Use

    //定义一个单例对象    val test = StaticTest    //调用add方法    println(test.add(2,3))           //  5

After this class is compiled, two files are generated Statictest.class and Statictest$. Class,class and object are compiled together
Let's look at the anti-compilation results of the Statictest$.class class, which is very similar to our Java singleton pattern.

 Public Final  class statictest${//point to yourself, externally via module$ access interface   Public Static Finalmodule$;//internal static variable of type  Private FinalStatictest Singleobj;Static{New(); }//This is an external interface, returning a singleton object, much like the getinstance we often use   PublicStatictestSingleobj()  {return  This. singleobj; } Public int Apply() {predef: Module$.println ("-------Apply--------");returnAdd3,4); } Public int Add(intXintY) {returnSingleobj (). scala$test$statictest$ $add _ (x, y); } Public int Sub(intXintY) {returnSingleobj (). scala$test$statictest$ $sub _ (x, y); }//Private construction  Privatestatictest$ () {//point to yourselfmodule$ = This;//Create Objects     This. Singleobj =NewStatictest (); }}

And look at Statictest.class's anti-compilation results.

 Public classstatictest{ Public Static int Sub(intParamInt1,intParamInt2) {returnStatictest.  Module$.sub (ParamInt1, ParamInt2); } Public Static int Add(intParamInt1,intParamInt2) {returnStatictest.  Module$.add (ParamInt1, ParamInt2); } Public Static void Apply() {statictest:  Module$.apply (); } Public StaticStatictestSingleobj()  {returnStatictest.  Module$.singleobj (); } Public intscala$test$statictest$$add_(intXintY) {returnx + y; } Public intscala$test$statictest$$sub_(intXintY) {returnx + y; }}

Here are the static methods, in which the interface is accessed through the module$.

The above is a method that accesses a class instance of the same name in a singleton, and if there is a class in the singleton, what should be used in the outer class?

class StaticTest private {  def getInner() : Inner = {    new Inner  }}object StaticTest{  class Inner{  }}

We want to use the inner class in the class statictest, according to the above code, will prompt the error, you are not able to find the inner class, to use it, you can follow the following way:
Way One:

//类的外部引入import scala.test.StaticTest.Innerclass StaticTest private {  def getInner() : Inner = {    new Inner  }}object StaticTest{  class Inner{  }}

Way two:

class StaticTest private {//在类的内部引用  import StaticTest._  def getInner() : Inner = {    new Inner  }}object StaticTest{  class Inner{  }}

Scala:object (single case)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.