3. Scala classes and objects

In Class 1. Scala

1.1 class declaration

And differences in Java classes

  • public class is defined in the scala, not declared as public. The case of a member variable in the class without other modifiers are also default to the public.
  • And there may be a plurality of classes defined in a document, these classes are public properties having
  • This document and file defines the class name can be different
  • After a good definition of a class can no longer define the same name as a class, but you can define the object of the same name

Constructor class
Each class can have primary constructor, the main parameters of the constructor directly placed behind the class name

  • Primary constructor parameters may be var, val, private var / val, private [this] var / val modified with the modifier access characteristics; default private modifier without being modified [this]

Class can be defined in the auxiliary builder, 3 points

  • Name of the secondary constructor can only be called this
  • Auxiliary constructor parameter list can not have modifiers
  • The first code is an auxiliary device must be configured to call the main builder or other auxiliary builder
1.2 class attributes

The definition of property, no matter what type, must be initialized. Attribute is defined in several ways as follows:

  • val modifications
    can be accessed externally, but only read, can not be modified.
  • var modifications
    may be accessed and modified external
  • private var / val modifications
    can only be accessed or modified object type or the same name of the present
  • private [this] var / val modifications
    can only be accessed or modified in this class
1.3 Method class

Several ways to modify classes and attributes, modification effect is the same.
An important feature of Scala method arguments is that they are val instead of var, it can not be reassigned to the parameters, the compiler will complain at Scala's method.

object HelloWorld {
  def main(args: Array[String]): Unit = {
    val r1 = new Rational(3, 4)
    val r2 = new Rational(4, 5)
    println(r1 add r2)   // 31/20
    println(r1*r2)   // 3/5
    println(r1*3)    // 9/4

    val r3 = new Rational(6)
    println(r3)   // 6/1

    val r4 = new Rational(66, 42)
    println(r4)   // 11/7
  }
}

/**
 * 有理数类
 * @param n:代表分子
 * @param d:代表分母
 */
class Rational(n:Int, d:Int){
  // 对于一个分数,分母不能为0,可以通过require定义一个前置条件
  require(d!=0) // 如果是true正常返回,否则抛出异常IllegalArgumentException

  private val g = gcd(n.abs, d.abs)
  val numer:Int = n/g
  val denom:Int = d/g

  // 辅助构造方法
  def this(n:Int) = this(n, 1)

  // 重写toString方法
  override def toString: String = numer + "/" + denom

  // 有理数加法, 下面这种声明方式编译会报错:value d is not a member of Rational, value n is not.....
  // 原因是n和d没有修饰符修饰,默认是类私有的,所以that的n和d不能被访问,如果定义类的时候给n和d加上修饰符val,下面这个方法就可执行
  // def add(that:Rational) = new Rational(n*that.d + d*that.n, d*that.d)

  // 需要把 n和d 做成字段,才能访问
  def add(that:Rational) =
    new Rational(
       numer * that.denom + denom * that.numer
      ,denom * that.denom
    )

  // 有理数乘法(Scala中 + *等是合理的方法名)
  def *(that:Rational) =
    new Rational(
       numer*that.numer
      ,denom*that.denom
    )

  // 方法重载
  def *(a:Int) =
    new Rational(
       numer*a
      ,denom
    )

  // 为了将分数进行正规化(例如 66/42 --> 11/7), 定义一个找分子和分母最小公约数的方法
  private def gcd(a:Int, b:Int): Int =
    if (b==0) a else gcd(b, a%b)

}

2. Scala in object

Scala static members are not allowed, use of such a scene, Scala provides a singleton object. Definition and similar class, but the class keyword into the object keyword.

object HelloWorld {
  def main(args: Array[String]): Unit = {
    val a1 = new A      // class A (对于类中可执行的方法,在创建对象的时候首先都会执行一遍,类似Java的静态代码块)
    val a2 = new A()    // class A
    val a3 = A          // object A (第一次创建的时候会打印,后面就不会了)
    val a4 = A
    println(a1)   // A@35851384
    println(a2)   // A@649d209a
    println(a3)   // A$@6adca536
    println(a4)   // A$@6adca536
    println(a1.func(4,12))  // 12
    println(a3.func(4,12))  // 16
    println(A.func(4,12))   // 16
  }
}
/**
 * 不带构造器的类
 */
class A{
  println("class A")
  def func(a:Int, b:Int):Int = if(a>=b) a else b
}
// 伴生对象
object A{
  println("object A")
  def func(a:Int, b:Int):Int = a+b
}
2.1 object classes and methods
2.2 companion objects

When a singleton object share the same name with a class, are called companion objects of this class. Companion must define classes and objects, while the class of this class also called singleton object associated in the same source file.
Class and its companion object can access each other's private members.
No single embodiment of the object with the same name is called a companion class orphaned objects.

2.3 APP qualities
Released eight original articles · won praise 0 · Views 107

Guess you like

Origin blog.csdn.net/qq_42994084/article/details/102908832