diff --git a/_ba/tour/package-objects.md b/_ba/tour/package-objects.md new file mode 100644 index 0000000000..e3a020e0f7 --- /dev/null +++ b/_ba/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: ba + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_ba/tour/packages-and-imports.md b/_ba/tour/packages-and-imports.md index b466c5946a..dcc73e2d73 100644 --- a/_ba/tour/packages-and-imports.md +++ b/_ba/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_es/tour/package-objects.md b/_es/tour/package-objects.md new file mode 100644 index 0000000000..600e428523 --- /dev/null +++ b/_es/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: es + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_es/tour/packages-and-imports.md b/_es/tour/packages-and-imports.md index 6f1e51d951..3d1c4989d2 100644 --- a/_es/tour/packages-and-imports.md +++ b/_es/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_ko/tour/package-objects.md b/_ko/tour/package-objects.md new file mode 100644 index 0000000000..99e15aa8ef --- /dev/null +++ b/_ko/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: ko + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_ko/tour/packages-and-imports.md b/_ko/tour/packages-and-imports.md index b1a226f711..6b77e31189 100644 --- a/_ko/tour/packages-and-imports.md +++ b/_ko/tour/packages-and-imports.md @@ -10,6 +10,7 @@ num: 35 language: ko previous-page: named-arguments +next-page: package-objects --- # 패키지와 임포트 diff --git a/_pl/tour/package-objects.md b/_pl/tour/package-objects.md new file mode 100644 index 0000000000..05b11f78c1 --- /dev/null +++ b/_pl/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: pl + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pl/tour/packages-and-imports.md b/_pl/tour/packages-and-imports.md index 724704030b..5d5b61c2af 100644 --- a/_pl/tour/packages-and-imports.md +++ b/_pl/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_pt-br/tour/package-objects.md b/_pt-br/tour/package-objects.md new file mode 100644 index 0000000000..09db5aee84 --- /dev/null +++ b/_pt-br/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: pt-br + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pt-br/tour/packages-and-imports.md b/_pt-br/tour/packages-and-imports.md index abc74a9f2a..797e58a2fd 100644 --- a/_pt-br/tour/packages-and-imports.md +++ b/_pt-br/tour/packages-and-imports.md @@ -9,7 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments -next-page: type-inference +next-page: package-objects --- # Packages and Imports diff --git a/_th/tour/package-objects.md b/_th/tour/package-objects.md new file mode 100644 index 0000000000..337a18cf41 --- /dev/null +++ b/_th/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: th + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_th/tour/packages-and-imports.md b/_th/tour/packages-and-imports.md index 4f6d714a25..da591b618a 100644 --- a/_th/tour/packages-and-imports.md +++ b/_th/tour/packages-and-imports.md @@ -11,4 +11,5 @@ num: 33 language: th previous-page: named-arguments +next-page: package-objects --- diff --git a/_tour/package-objects.md b/_tour/package-objects.md index f787e3cc0f..9a59550b4a 100644 --- a/_tour/package-objects.md +++ b/_tour/package-objects.md @@ -6,17 +6,20 @@ discourse: true partof: scala-tour -num: 35 +num: 36 previous-page: packages-and-imports --- -# Package Objects -Scala provides you package object as convenient container shared across the package. Package objects -can contain arbitrary definitions, not just variable and method definitions. For instance, they frequently +# Package objects + +Scala provides package objects as a convenient container shared across an entire package. + +Package objects +can contain arbitrary definitions, not just variable and method definitions. For instance, they are frequently used to hold package-wide type aliases and implicit conversions. Package objects can even inherit Scala classes and traits. -Package objects usually created as separate scala file in the package lavel - `package.scala` +By convention, the source code for a package object is usually put in a source file named `package.scala`. Each package is allowed to have one package object. Any definitions placed in a package object are considered members of the package itself. @@ -29,46 +32,46 @@ See example below. Assume first a class `Fruit` and three `Fruit` objects in a p package gardening.fruits 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") +object Apple extends Fruit("Apple", "green") +object Plum extends Fruit("Plum", "blue") +object Banana extends Fruit("Banana", "yellow") ``` -Now assume you want to place a variable planted and a method `showFruit` directly into package `gardening`. + +Now assume you want to place a variable `planted` and a method `showFruit` directly into package `gardening`. Here's how this is done: + ``` // in file gardening/fruits/package.scala package gardening package object fruits { - val planted = List(apple, plum, banana) - def showFruit(fruit: Fruit) { - println(fruit.name +"s are "+ fruit.color) + val planted = List(Apple, Plum, Banana) + def showFruit(fruit: Fruit): Unit = { + println(s"${fruit.name}s are ${fruit.color}") } } ``` -Having the package Object above, any other code in the same package can import the method just like it would import -a class. For example, the following object `PrintPlanted` imports `planted` and `showFruit` in exactly the same +As an example of how the use site looks, the following object `PrintPlanted` imports `planted` and `showFruit` in exactly the same way it imports class `Fruit`, using a wildcard import on package gardening.fruits: ``` // in file PrintPlanted.scala import gardening.fruits._ object PrintPlanted { - def main(args: Array[String]) { - for (fruit: Fruit <- fruits.planted) { + def main(args: Array[String]): Unit = { + for (fruit <- fruits.planted) { showFruit(fruit) } } } ``` -Having package object also helps reducing number of imports on client use. -Package objects are usual objects. That means you can use inheritance for building them (for exmple from traits): +Package objects are like other objects, which means you can use inheritance for building them. For example, one might mix in a couple of traits: + ``` -package object fruits extends FruitAliases - with FruitHelpers { -// helpers and variables follows here +package object fruits extends FruitAliases with FruitHelpers { + // helpers and variables follows here } ``` -Note that method overloading doesn't work in package objects. +Note that method overloading doesn't work in package objects. diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index 35c9b198b2..68efdd8083 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -8,6 +8,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: package-objects --- # Packages and Imports diff --git a/_zh-cn/tour/package-objects.md b/_zh-cn/tour/package-objects.md new file mode 100644 index 0000000000..ee95910149 --- /dev/null +++ b/_zh-cn/tour/package-objects.md @@ -0,0 +1,17 @@ +--- +layout: tour +title: Package Objects +language: zh-cn + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_zh-cn/tour/packages-and-imports.md b/_zh-cn/tour/packages-and-imports.md index a4a1713b1b..56f4de1371 100644 --- a/_zh-cn/tour/packages-and-imports.md +++ b/_zh-cn/tour/packages-and-imports.md @@ -11,6 +11,7 @@ num: 33 language: zh-cn previous-page: named-arguments +next-page: package-objects --- # 包和导入