Created
November 8, 2011 11:27
-
-
Save jackywyz/1347538 to your computer and use it in GitHub Desktop.
scala private[this] , spawn方法
This file contains hidden or 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
package test | |
object Simon_test { | |
class My_1{ | |
private[this] val name = "Simon" | |
def showName(my:My_1){ | |
println(my.name) //Error | |
println(this.name) //OK | |
} | |
} | |
class My_2{ | |
private val name = "Simon" | |
def showName(my:My_2){ | |
println(my.name) //OK | |
println(this.name) //OK | |
} | |
} | |
class My_3{ | |
private[test] val name = "Simon" | |
protected val age = 2 | |
def showName(my:My_3){ | |
println(my.name) //OK | |
println(this.name) //OK | |
} | |
} | |
class My_4 private(a:Int){ | |
} | |
object T extends My_3{ | |
val a =age +2//ok | |
val b = new My_3().age //Error | |
} | |
object My_4{ | |
new My_4(2) //ok | |
} | |
def main(args : Array[String]) : Unit = { | |
val my1 = new My_1() | |
val my2 = new My_2() | |
val my3 = new My_3() | |
println(my1.name) //Error | |
println(my2.name) //Error | |
println(my3.name) //OK | |
val my4 = new My_4() //Error | |
} | |
} |
This file contains hidden or 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
def spawn(p: => Unit) = { | |
val t = new Thread() { override def run() = p } | |
t.start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
private[test], protected[test] 中的【x】 是控制x包是否可以访问 该成员。