Skip to content

Commit

Permalink
add /Operator_demo1.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Apr 27, 2021
1 parent 32e8757 commit df13652
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@
```
- [SuperConstruct](./src/main/scala/ScalaBasic/SuperConstruct.scala), [SuperConstruct1 java](./src/main/scala/ScalaBasic/SuperConstruct1_java.java), [SuperConstruct2](./src/main/scala/ScalaBasic/SuperConstruct_2.scala), [SuperConstruct3](./src/main/scala/ScalaBasic/SuperConstruct_3.scala)- Scala/Java Super constructor examples

- Operator
- [Operator_demo1](./src/main/scala/ScalaBasic/Operator_demo1.scala)


5. Scala class, trait, abstract class..
- Class
- Method create `class instance` in Scala
Expand Down
63 changes: 63 additions & 0 deletions src/main/scala/ScalaBasic/Operator_demo1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ScalaBasic

// https://www.youtube.com/watch?v=oSq9JlaAelk&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=143

object Operator_demo1 extends App {

// 1) if want to use "reserved" word, we need to add single comma : `xxx`
var `var` = 10
println ("var = " + `var`)

println("================")

// 2) "middle" operator
val n1 = 1
val n2 = 2
var r1 = n1 + n2
var r2 = n1.+(n2) // n1 + n2

println("r1 = " + r1 + ", r2 = " + r2)

println("================")

// 3) "middle" operator example
val dog = new Dog
println(dog.age)
dog.+(30)
println(dog.age)

dog + 30
println(dog.age)

println("================")

// 4) "post" operator example
val oper = new Operate
println(oper++) // same as run def ++()
println(oper.++) // same as run def ++()

println("================")

// 5) "pre" operator example
val oper2 = new Operate2
!oper2 // def unary_!
}


class Dog { // "middle" operator example
var age:Int = 10
// let's define our "+" operator
def +(n:Int): Unit = {
age += n
}
}

class Operate { // "post" operator example
// this method has no inout, so we can neglect the () here
def ++() = "123"
}

class Operate2 { // "pre" operator example
// this method has no inout, so we can neglect the () here
def unary_! = println("!!!!!!!!!!!")
}

0 comments on commit df13652

Please sign in to comment.