-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_Packaging_and_Imports.sc
170 lines (139 loc) · 3.52 KB
/
07_Packaging_and_Imports.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 7.1 Packaging with the Curly Braces Style Notation
package com.acme.store {
class Foo { override def toString = "I am com.acme.store.Foo" }
}
package com.acme.store
class Foo { override def toString = "I am com.acme.store.Foo" }
package foo.bar.baz
class Foo {
override def toString = "I'm foo.bar.baz.Foo"
}
// a package containing a class named Foo
package orderentry {
class Foo { override def toString = "I am orderentry.Foo" }
}
// one package nested inside the other
package customers {
class Foo { override def toString = "I am customers.Foo" }
package database {
// this Foo is different than customers.Foo or orderentry.Foo
class Foo { override def toString = "I am customers.database.Foo" }
}
}
// a simple object to test the packages and classes
object PackageTests extends App {
println(new orderentry.Foo)
println(new customers.Foo)
println(new customers.database.Foo)
}
package com.alvinalexander.foo {
class Foo { override def toString = "I am com.alvinalexander.foo.Foo" }
}
// like java
package foo.bar.baz
class Foo {
override def toString = "I'm foo.bar.baz.Foo"
}
// 7.2 Importing One or More Members
import java.io.{File, IOException, FileNotFoundException}
import java.io._
package foo
import java.io.File
import java.io.PrintWriter
class Foo {
import javax.swing.JFrame // only visible in this class
// ...
}
class Bar {
import scala.util.Random // only visible in this class
// ...
}
class Bar {
def doBar = {
import scala.util.Random
println("")
}
}
// 7.3 Renaming Members on Import
import java.util.{ArrayList => JavaList}
val list = new JavaList[String]
import java.util.{Date => JDate, HashMap => JHashMap}
// error: this won't compile because HashMap was renamed
// during the import process
val map = new HashMap[String, String]
import java.util.{HashMap => JavaHashMap}
import scala.collection.mutable.{Map => ScalaMutableMap}
import scala.collection.mutable.Map
import System.out.{println => p}
p("hello")
// 7.4 Hiding a Class During the Import Process
import java.util.{Random => _, _}
val r = new Random
new ArrayList
import java.util.{List => _, Map => _, Set => _, _}
// 7.5 Using Static Imports
import java.lang.Math._
val a = sin(0)
val a = cos(PI)
import java.awt.Color._
println(RED)
val currentColor = BLUE
{
// Java
package com.alvinalexander.dates;
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
import com.alvinalexander.dates.Day._
// somewhere after the import statement
if (date == SUNDAY || date == SATURDAY) println("It's the weekend.")
if (date == Day.SUNDAY || date == Day.SATURDAY) {
println("It's the weekend.")
}
}
// 7.6 Using Import Statements Anywhere
package foo
import scala.util.Random
class ImportTests {
def printRandom {
val r = new Random
}
}
def getRandomWaitTimeInMinutes: Int = {
import com.alvinalexander.pandorasbox._
val p = new Pandora
p.release
}
def printRandom {
{
import scala.util.Random
val r1 = new Random // this is fine
}
val r2 = new Random // error: not found: type Random
}
package orderentry {
import foo._
// more code here ...
}
package customers {
import bar._
// more code here ...
package database {
import baz._
// more code here ...
}
}
package foo
// available to all classes defined below
import java.io.File
import java.io.PrintWriter
class Foo {
// only available inside this class
import javax.swing.JFrame
// ...
}
class Bar {
// only available inside this class
import scala.util.Random
// ...
}