Scala for Large Data Learning Notes: Chapter 9 Packages and Introduction

Keywords: Scala Java

Articles Catalogue

9.1 Pack

Packets, like packages in Java, are designed to organize programs, and packages in Scala do not require associations between directories and packages. The package that defines a class is as follows:

package com {
  package horstmann {
package impatient {
class Employee(id: Int) {
def description = "An employee with id " + id }
}
 
} }

It also supports contributing content to multiple packages in the same file:

package com {
  package horstmann {
package impatient {
class Employee(id: Int) {
def description = "An employee with id " + id
} 
}
}
 }
package net {
  package bigjava {
class Counter {
private var value = 0
def increment() { value += 1 }
def description = "A counter with value " + value
} 
}
}

Serial package statements:

package com.horstmann.impatient { package people {
class Person(val name: String) {
val friends = new collection.mutable.ArrayBuffer[Person] // Doesn't pick up collection from com.horstmann.collection def description = name + " with friends " +
        friends.map(_.name).mkString(", ")
    }
} }

Members of COM and com. horstmann are invisible, requiring full paths or import s
Document top marking:
package com.horstmann.impatient.people
class Person
...
Equivalent to
package com.horstmann.impatient
package people
class Person
...
Equivalent to
package com.horstmann.impatient{
package people{
class Person
...
} }
Document Top Markup: In this form, there can be no more than one different package in a file.

9.2 Scope

The scope of the package supports nesting. The package name can be relative.

package com {
  package horstmann {
object Utils {
def percentOf(value: Double, rate: Double) = value * rate / 100
    }
    package impatient {
class Employee(initialSalary: Double) {
private var salary = initialSalary
def description = "An employee with salary " + salary def giveRaise(rate: scala.Double) {
          salary += Utils.percentOf(salary, rate)
          // Ok to access Utils from parent package
} }
} }
}
  //If you encounter referenced classes and scala Conflicts in internal classes, absolute package names can be used(with _root_start):
package com {
  package horstmann {
package impatient { class Manager {
// The following doesn't work (look into Other.scala)
// val subordinates = new collection.mutable.ArrayBuffer[Employee]
val subordinates = new _root_.scala.collection.mutable.ArrayBuffer[Employee] def description = "A manager with " + subordinates.length + " subordinates"
} }
} }

Note: The package object name should be the same as the package name.

9.3 Package Objects

Packages can contain classes, objects, attributes, but not definitions of functions and variables, which is a limitation of Java virtual machines.
To solve this problem, each package has a package object. These functions and constants can be defined in the package object.

package com.sec03
// Defined in the parent package
package object people {
val defaultName = "John Q. Public"
}
package people{
  class Person {
    var name = defaultName // A constant from the package
def description = "A person with name " + name }
}
object Sec03Main extends App {
val john = new com.sec03.people.Person println(john.description)
}

9.4 Packet Visibility

You can set the visibility of packages by setting private.

package com.sec04
package object people {
val defaultName = "John Q. Public"
}
object Sec04Main extends App {
val john = new com.sec04.people.Person 
println(john.description)
}
package people{
private[sec04] class Person {
var name = defaultName // A constant from the package private[sec04] def description = "A person with name " + name private[people] def description2 = "A person with name " + name
} }

9.5 Introduction

Introducing packages through import, similar to that in java*

package com.sec05
import java.awt._
class Manager {
import collection.mutable._
val subordinates = new ArrayBuffer[Employee]
def description = "A manager with " + subordinates.length + " subordinates" + Font.BOLD
}
class Employee(val name: String)
object Sec05Main extends App {
val wilma = new Manager
val employees = collection.mutable.HashSet(
    // import collection.mutable._ doesn't extend until here
new Employee("Fred"), new Employee("Barney")) wilma.subordinates ++= employees
println(wilma + ": " + wilma.description)
}

Note: Introduced statements in Scala can appear anywhere, not just at the top of the file.

These packages are introduced automatically.
65

9.6 Rename and Hide Method

Select a single class through the package selector.

import java.awt.{Color, Font}
Color.RED
Font.BOLD
//rename
import java.util.{HashMap => JavaHashMap} 
import scala.collection.mutable._
new JavaHashMap[String, Int]
new HashMap[String, Int]
//Hide a member
import java.util.{HashSet => _, _} 
import scala.collection.mutable._
new HashSet[String]

Posted by hoffmeister on Sun, 04 Aug 2019 22:58:40 -0700