Beginner Scala
Folder structure and part of the code
Package.scala's Code
Package COMX Package object mypkg { def myprint = println ("Hello Ji2 on Package object") } Object Myp kg2{ def myprint = println ("Hello Ji2 in Object")
Command line
>scalac Mypkg/package.scala Newt.scala
>scala Test2
Results
Hello Ji2 in Package object () Hello Ji2 in Package object () Hello Ji2 in Object ()
======================
Analysis:
Code beginner Good time, src below only a file Newt.scala and a folder mypkg, connotation Package.scala,
After the implementation of the SCALAC, SRC below a few more classes, more than a COMx folder
COMx folder
Mypkg folder
Visible
1 Scala's package statement specifies where the class file is generated, even if it does not already exist
2 package Object definition file Package.scala should be in Mypkg folder
3 The class file for object is under the folder specified by the package
The 4.package object has no corresponding class, and in Package.class, it becomes the package comx.mypkg, which becomes the first layer of the pakage structure.
5. Newt.scala, the name of object is Test2, so the parameters of the Scala command are Test2, not newt, otherwise the error
PostScript, for the 2nd I did another experiment
Put Src/mypkg-MYPKG2, the documents Package.scala->package_n.scala,
Package_n.scala adds a Package object definition
Package Object Mypkg3 { def myprint = println ("Hello Ji3 in Package Object") }
The difference between the results and the last
COMx below became two folder:mypkg & Mypkg3
Conclusion:
The name of the package object definition file is arbitrary, the folder name is also arbitrary,
Instead, the name of each package object generates a folder with its own definition of Class:package.class & Package$.class
The atypical Scala program and its compiled results