Skip to content

Commit 543fa5c

Browse files
committed
Add ObjectOrientedPrograming section
1 parent f491691 commit 543fa5c

File tree

9 files changed

+730
-6
lines changed

9 files changed

+730
-6
lines changed
119 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scalatutorial.aux
2+
3+
abstract class IntSet {
4+
def incl(x: Int): IntSet
5+
def contains(x: Int): Boolean
6+
}
7+
8+
object Empty extends IntSet {
9+
def contains(x: Int): Boolean = false
10+
def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty)
11+
}
12+
class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet {
13+
14+
def contains(x: Int): Boolean =
15+
if (x < elem) left contains x
16+
else if (x > elem) right contains x
17+
else true
18+
19+
def incl(x: Int): IntSet =
20+
if (x < elem) new NonEmpty(elem, left incl x, right)
21+
else if (x > elem) new NonEmpty(elem, left, right incl x)
22+
else this
23+
}

src/main/scala/scalatutorial/sections/LexicalScopes.scala

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ object LexicalScopes extends ScalaTutorialSection {
175175
*
176176
* = Packages and Imports =
177177
*
178-
* Top-level definitions can be organized in ''packages'':
178+
* Top-level definitions can be organized in ''packages''.
179+
* To place a class or object inside a package, use a package clause
180+
* at the top of your source file:
179181
*
180182
* {{{
181183
* // file foo/Bar.scala
@@ -223,6 +225,48 @@ object LexicalScopes extends ScalaTutorialSection {
223225
* Bar.someMethod
224226
* }
225227
* }}}
228+
*
229+
* = Automatic Imports =
230+
*
231+
* Some entities are automatically imported in any Scala program.
232+
*
233+
* These are:
234+
*
235+
* - All members of package `scala`
236+
* - All members of package `java.lang`
237+
* - All members of the singleton object `scala.Predef`.
238+
*
239+
* Here are the fully qualified names of some types and functions
240+
* which you have seen so far:
241+
*
242+
* {{{
243+
* Int scala.Int
244+
* Boolean scala.Boolean
245+
* Object java.lang.Object
246+
* String java.lang.String
247+
* }}}
248+
*
249+
* = Writing Executable Programs =
250+
*
251+
* So far our examples of code were executed from your Web
252+
* browser, but it is also possible to create standalone
253+
* applications in Scala.
254+
*
255+
* Each such application contains an object with a `main` method.
256+
*
257+
* For instance, here is the "Hello World!" program in Scala:
258+
*
259+
* {{{
260+
* object Hello {
261+
* def main(args: Array[String]) = println("hello world!")
262+
* }
263+
* }}}
264+
*
265+
* Once this program is compiled, you can start it from the command line with
266+
*
267+
* {{{
268+
* $ scala Hello
269+
* }}}
226270
*/
227271
def nothing(): Unit = ()
228272
}

0 commit comments

Comments
 (0)