Plan du site  
pixel
pixel

Articles - Étudiants SUPINFO

Scala tutorial: object-oriente

Par Zhihao WANG Publié le 03/04/2020 à 02:24:34 Noter cet article:
(0 votes)
Avis favorable du comité de lecture

We know that scala is a JVM language that can seamlessly integrate with Java, which greatly expands the scope of it. The famous spark in big data is written in scala. So what are the features of scala? Let’s see them one by one.

First of all, scala is an object-oriented programming language, and its object-oriented performance is mainly reflected in the following aspects

1 Unified Types

2 Classes

3 Traits

Now we explain their respective characteristics

Unified Types

In Scala, there are no basic types in Java. The following is a subset of the type hierarchy.

We can see that Any is the parent type of all types, and all types inherit Any directly or indirectly. Any provides some common methods such as equals, hashCode, and toString. Any has two direct subclasses: AnyVal and AnyRef.

AnyVal represents the value type, which has 9 predefined non-null value types are: Double, Float, Long, Int, Short, Byte, Char, Unit and Boolean.

Among them, Unit is a special type. You can think of it as void in java. Because all functions in Scala must have a return type, when there is no return, it will use Unit.

AnyRef represents a reference type. All non-value types are reference types. And all types of user-defined type is a subclass AnyRef. In the Java environment, AnyRef is equivalent to java.lang.Object in Java.

Value types can be converted as follows:

Example:

val x: Long = 987654321
val y: Float = x  // 9.8765434E8 (note that some precision is lost in this case)
val face: Char = '☺'
val number: Int = face  // 9786

Nothing is a subclass of all types, which is commonly used to indicate the abnormal end signal, such as throwing an exception, exiting a program and so on

Null is a subtype of all reference types. Null is mainly used to interact with the JVM. It is usually not necessary to use it in Scala.

Classes

Class is similar to a class in Java. It can contain methods, constants, variables, types, objects, traits, classes, etc.

The simplest definition of a class is the keyword class + identifier. The initials of the class name should be capitalized. As follows:

class Family

val family = new Family

The new keyword is used to create instances of the class. In the example above, Family does not define a constructor, so it defaults to a constructor with no parameters

So how can we add a constructor to a class?

class Point(var x: Int, var y: Int) {

  override def toString: String =
    s"($x, $y)"
}

val point1 = new Point(2, 3)
point1.x  // 2
println(point1)  // prints (2, 3)

Unlike other programming languages, Scala's class constructor is defined in: (var x: Int, var y: Int). Here we also override the toString method in AnyRef. Constructors can also have default values:

class Point(var x: Int = 0, var y: Int = 0)

val origin = new Point  // x and y are both set to 0
val point1 = new Point(1)
println(point1.x)  // prints 1

Traits

Traits are used to share program interfaces and fields between classes. They are similar to Java 8 interfaces. Classes and Objects can extend traits, but traits cannot be instantiated, so traits have no parameters

1 Defining Traits

The simplest trait is the keyword trait + identifier

trait HelloWorld

Traits are useful as generic types and abstract methods

trait Iterator[A] {
  def hasNext: Boolean
  def next(): A
}

Extending trait Iterator A requires a type A and methods hasNext and next.

2 Using Traits

Use the extends keyword to extend a feature. Then use the override keyword to implement any abstract members in the trait:

trait Iterator[A] {
  def hasNext: Boolean
  def next(): A
}

class IntIterator(to: Int) extends Iterator[Int] {
  private var current = 0
  override def hasNext: Boolean = current
  override def next(): Int =  {
    if (hasNext) {
      val t = current
      current += 1
      t
    } else 0
  }
}


val iterator = new IntIterator(10)
iterator.next()  // returns 0
iterator.next()  // returns 1

3 Subtype

Just like java, wherever Traits is needed, we can use its subtype to replace it.

import scala.collection.mutable.ArrayBuffer

trait Pet {
  val name: String
}

class Cat(val name: String) extends Pet
class Dog(val name: String) extends Pet

val dog = new Dog("Harry")
val cat = new Cat("Sally")

val animals = ArrayBuffer.empty[Pet]
animals.append(dog)
animals.append(cat)
animals.foreach(pet - println(pet.name))  // Prints Harry Sally

Here animals need Pet type, we can use dog and cat instead

Ecrit avec Supinfo-Articles.com

A propos de SUPINFO | Contacts & adresses | Enseigner à SUPINFO | Presse | Conditions d'utilisation & Copyright | Respect de la vie privée | Investir
Logo de la société Cisco, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société IBM, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sun-Oracle, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Apple, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Sybase, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Novell, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Intel, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Accenture, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société SAP, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Prometric, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo de la société Toeic, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management Logo du IT Academy Program par Microsoft, partenaire pédagogique de SUPINFO, la Grande École de l'informatique, du numérique et du management

SUPINFO International University
Ecole d'Informatique - IT School
École Supérieure d'Informatique de Paris, leader en France
La Grande Ecole de l'informatique, du numérique et du management
Fondée en 1965, reconnue par l'État. Titre Bac+5 certifié au niveau I.
SUPINFO International University is globally operated by EDUCINVEST Belgium - Avenue Louise, 534 - 1050 Brussels