-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yennan.liu
committed
Dec 15, 2021
1 parent
b281b2f
commit 45c7797
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ScalaAdvance.PackageDemo | ||
|
||
/** | ||
* Scala Package object demo 1 | ||
* | ||
* ref : https://github.com/yennanliu/til#20211215 | ||
* | ||
*/ | ||
|
||
import ScalaAdvance.PackageDemo.service.myService._ | ||
|
||
object demo1 extends App { | ||
|
||
// demo 1 : package attr, method | ||
println("planted " + planted) | ||
printDefault("123") | ||
|
||
println("=====================") | ||
|
||
// demo 2 : package constructor's attr, method | ||
val myService1 = new myService("m") | ||
println("myService1 = " + myService1.toString) | ||
myService1.print("hiiii") | ||
|
||
val res1 = myService1.dummyHttpCall("hitttt") | ||
println("res1 = " + res1) | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/scala/ScalaAdvance/PackageDemo/service/myService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ScalaAdvance.PackageDemo.service | ||
|
||
// https://docs.scala-lang.org/zh-cn/tour/package-objects.html | ||
|
||
package object myService { | ||
|
||
// attr | ||
case class Fruit(name: String, color: String) | ||
object Apple extends Fruit("Apple", "green") | ||
object Plum extends Fruit("Plum", "blue") | ||
object Banana extends Fruit("Banana", "yellow") | ||
|
||
val planted = List(Apple, Plum, Banana) | ||
|
||
// constructor | ||
class myService(host:String){ | ||
|
||
// method | ||
def print(input:String):Unit = { | ||
println(">>>> input = " + input) | ||
} | ||
|
||
def dummyHttpCall(httpRequest:String):String = { | ||
s"this is my request : $httpRequest" | ||
} | ||
} | ||
|
||
// method | ||
def printDefault(input:String):Unit = { | ||
println(">>>> printDefault = " + input) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ScalaDev | ||
|
||
object test1 extends App { | ||
|
||
val x = List(1,2,3) | ||
|
||
val x_1 = x.map( (i:Int) => i+1) | ||
|
||
println (x_1.toString) | ||
|
||
List((1,2),(2,3),(3,4),(2,5)).map(x => x.toString) | ||
} | ||
|