// In file packages/ex1/Ex1.scala package bobsrockets.navigation class Navigator
// In file packages/ex2/Ex2.scala package bobsrockets { package navigation { // In package bobsrockets.navigation class Navigator package tests { // In package bobsrockets.navigation.tests class NavigatorSuite } } }
// In file packages/ex3/Ex3.scala package bobsrockets.navigation { // In package bobsrockets.navigation class Navigator package tests { // In package bobsrockets.navigation.tests class NavigatorSuite } }
// In file packages/ex4/Ex4.scala package bobsrockets { package navigation { class Navigator } package launch { class Booster { // No need to say bobsrockets.navigation.Navigator val nav = new navigation.Navigator } } }
// In file packages/launch.scala // In file launch.scala package launch { class Booster3 } // In file bobsrockets.scala package bobsrockets { package navigation { package launch { class Booster1 } class MissionControl { val booster1 = new launch.Booster1 val booster2 = new bobsrockets.launch.Booster2 val booster3 = new _root_.launch.Booster3 } } package launch { class Booster2 } }
// In file packages/Fruits.scala package bobsdelights abstract class Fruit( val name: String, val color: String ) object Fruits { object Apple extends Fruit("apple", "red") object Orange extends Fruit("orange", "orange") object Pear extends Fruit("pear", "yellowish") val menu = List(Apple, Orange, Pear) }
// In file packages/Ex5.scala // easy access to Fruit import bobsdelights.Fruit // easy access to all members of bobsdelights import bobsdelights._ // easy access to all members of Fruits import bobsdelights.Fruits._
// In file packages/Ex5.scala def showFruit(fruit: Fruit) { import fruit._ println(name +"s are "+ color) }
// In file packages/Ex6.scala import java.util.regex class AStarB { // Accesses java.util.regex.Pattern val pat = regex.Pattern.compile("a*b") }
// In file packages/Ex7.scala import Notebooks._ import Fruits.{Apple => _, _}
import java.lang._ // everything in the java.lang package import scala._ // everything in the scala package import Predef._ // everything in the Predef object
// In file packages/Ex8.scala.err class Outer { class Inner { private def f() { println("f") } class InnerMost { f() // OK } } (new Inner).f() // error: f is not accessible }
// In file packages/Ex9.scala.err package p { class Super { protected def f() { println("f") } } class Sub extends Super { f() } class Other { (new Super).f() // error: f is not accessible } }
// In file packages/ex10/Ex10.scala package bobsrockets { package navigation { private[bobsrockets] class Navigator { protected[navigation] def useStarChart() {} class LegOfJourney { private[Navigator] val distance = 100 } private[this] var speed = 200 } } package launch { import navigation._ object Vehicle { private[launch] val guide = new Navigator } } }
val other = new Navigator other.speed // this line would not compile
// In file packages/Rocket.scala class Rocket { import Rocket.fuel private def canGoHomeAgain = fuel > 20 } object Rocket { private def fuel = 10 def chooseStrategy(rocket: Rocket) { if (rocket.canGoHomeAgain) goHome() else pickAStar() } def goHome() {} def pickAStar() {} }
For more information about Programming in Scala (the "Stairway Book"), please visit: http://www.artima.com/shop/programming_in_scala and: |
Copyright © 2007-2008 Artima, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |