[JAVA] I touched Scala ~ [Object] ~

Introduction

image.png

It's a direct migration of the dwango tutorial, where you'll study, edit it, and replace it with your own terms.

object

In Scala, all values are objects. Also, all methods belong to some object. Therefore, it is not possible to create static fields and static methods that belong to a class like Java does. Instead, the object keyword allows you to define one singleton object with the same name under the current namespace. For singleton objects defined by the object keyword, you can define methods and fields that are specific to that object.

The main use of the object syntax is

Location of utility methods and global states (static methods and fields in Java) Factory method for objects of the same name class Can be mentioned.

The basic syntax of object is almost the same as class,

object <Object name> extends <name of the class> (with <Trait name>)* {
  (<Field definition> | <Method definition>)*
}

It will be. In Scala, an object called Predef is defined and imported by default, and this applies to the first usage. The method I used casually with println ("Hello") is actually a Predef method. You can inherit classes with extends and mix-in traits with with because you may want the object name to behave as a subclass of an existing class. In Scala's standard library, there is an object called Nil, which inherits from List because we want it to behave as a type of List. On the other hand, objects rarely mix-in traits, which seems to be due to syntactic compatibility with classes and traits.

On the other hand, let's think about the second usage. If you try to create a factory of the Point class that represents a point with an object, it will look like this: The method named apply is treated specially by the Scala processor, and if there is a description like Point (x) and a method named apply is defined in the Point object, then Point.apply (x) Is interpreted as. By using this to create an object with the apply method of Point object, you can create an object with a description like Point (3, 5).


scala> class Point(val x:Int, val y:Int)
defined class Point

scala> object Point {
     |   def apply(x: Int, y: Int): Point = new Point(x, y)
     | }
defined object Point
warning: previously defined class Point is not a companion to object Point.
Companions must be defined together; you may wish to use :paste mode for this.

This is compared to creating a Point object directly with new Point ().

The implementation details of the class (Point) can be hidden inside (only the interface is exposed to the outside) Can return an instance of that subclass instead of Point There are merits such as. Note that the above description is easier using the case class.


scala> case class Point(x: Int, y: Int)
defined class Point

Can be written. Case classes also appear in pattern matching, which will be described later, but I won't touch on how to use them here. Simply put, a case class exposes all the fields of the primary constructor of the class to which it is attached, and creates a class that overrides the basic methods of the object such as equals (), hashCode (), toString (). It also creates a factory method to instantiate that class. For example, the Point class defined in case class Point (x: Int, y: Int) does not explicitly define the equals () method,


Point(1, 2).equals(Point(1, 2))

The value evaluated for is true.

Companion object Singleton objects defined with the same name in the same file as the class are called companion objects. The companion object has privileged access to the corresponding class. For example, if weight is set to private


class Person(name: String, age: Int, private val weight: Int)

object Hoge {
  def printWeight(): Unit = {
    val taro = new Person("Taro", 20, 70)
    println(taro.weight)
  }
}

Is NG,


class Person(name: String, age: Int, private val weight: Int)

object Person {
  def printWeight(): Unit = {
    val taro = new Person("Taro", 20, 70)
    println(taro.weight)
  }
}

Is OK. Note that even companion objects cannot access members of classes that are private [this](accessible only from within that object). If you simply make it private, it will be accessible from the companion object.

If you want to try code with companion objects like the one above in the REPL, be sure to use the REPL's: paste command to paste the class and companion object together. The class and companion object must be in the same file, but if you enter them separately in the REPL, the REPL will not correctly recognize the companion relationship.


scala> :paste
// Entering paste mode (ctrl-D to finish)

class Person(name: String, age: Int, private val weight: Int)

object Person {
  def printWeight(): Unit = {
    val taro = new Person("Taro", 20, 70)
    println(taro.weight)
  }
}

// Exiting paste mode, now interpreting.

defined class Person
defined object Person

Recommended Posts

I touched Scala ~ [Object] ~
I touched Scala
I touched Scala ~ [Class] ~
I touched Scala ~ [Trait] ~
I touched Scala ~ [Control syntax] ~
I first touched Java ②
I first touched Java ③
I first touched Java ④
I touched Scala ~ [Type parameters and displacement specification] ~
I first touched Java
I went to Scala Fukuoka 2019!
I tried Cassandra's Object Mapper for Java
I made an eco server with scala
I touched ODM for Developer ② Rule development