Scala study notes (2) - classes, objects, inheritance, traits

lazy feature, if a variable is set to lazy, the variable will only be calculated when it is used for the first time

<class>

//In Scala, classes don't have to be declared public.
//Scala source files can contain multiple classes, all of which have public visibility.
class Student {
  //Variables decorated with val are read-only properties, with getters but no setters
  //(equivalent to variables modified with final in Java)
  val id = 666

  //Variables decorated with var have both getters and setters
  var age: Int = 20

  //Class private fields, can only be used inside the class
  private var name: String = "tom"

  //Object private fields, access permissions are more strict, the methods of the Person class can only access the fields of the current object
  private[this] val pet = "小强"
}

Main constructor:
   1) intertwined with the class name
   2) The main constructor runs, causing the code in the curly brackets after the class name to run
Auxiliary constructor:
   1) Must be named this
   2) Must start with a call to the primary constructor or other auxiliary constructors.

   3) The attributes inside cannot write modifiers

<object>

1) The methods in the object are all static methods

2) The fields in Object are all static fields

3) It itself is a singleton, (because there is no need to go to new)

In a Scala class, an object with the same name as the class is called a companion object , and private methods and properties can be accessed between the class and the companion object.

Usually we define the apply method in the companion object of the class, and the apply method will be called when the class name ( parameter 1, ... parameter n) is encountered

<Inheritance>

In Scala , let the subclass inherit the parent class. Like Java , it also means using the extends keyword
to inherit. The subclass can inherit the fields and methods of the parent class from the parent class; , subclass-specific fields and methods; use inheritance to effectively reuse code

The subclass can override the field and method of the parent class; but if the parent class is modified with final, and the field and method are modified with final, the class cannot be inherited, and the field and method cannot be overridden


In Scala , if a subclass wants to override a non-abstract method in a parent class, it must use the override keyword . The
override
keyword can help us find errors in the code as soon as possible, such as: We spell the method name of the override-modified parent class method. Wrong; for example, we wrote the wrong parameter of the parent class method to be overridden; etc.

 In addition, after the subclass overrides the parent class method, what if we want to call the overridden method of the parent class in the subclass ? Then you can use the super keyword to explicitly specify the method to call the parent class

// If there are some methods in the parent class that cannot be implemented immediately, but need to rely on different children to override, override and implement their own different method implementations. At this time, these methods in the parent class can be given no specific implementation, only the method signature, which is an abstract method.

// If there is an abstract method in a class, then the class must be declared as an abstract class with abstract, and the abstract class cannot be instantiated at this time

// When overriding an abstract method of an abstract class in a subclass, you do not need to use the override keyword


<trait>

java-like interface

The methods in the trait can either be implemented or not.

Implement the trait, if you do not inherit other classes, then use the first trait to use the extension after the use of with

Triat in Scala is a special concept
. First of all, we can use Trait as an interface. At this time, Triat is very similar to the interface in Java.
Abstract methods can be defined in triat, just like abstract methods in abstract classes. As long as the specific implementation of the method is not given, the
class can use the extends keyword to inherit the trait. Note that this is not implement, but extends. There is no concept of implement in scala. Whether inheriting a class or a trait, the unity is that the extends
class inherits the trait . After that, you must implement the abstract methods in it. You don't need to use the override keyword when implementing.
Scala does not support multiple inheritance of classes, but supports multiple inheritance traits, just use the with keyword.


















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324744693&siteId=291194637