Scala-six, objects

Key points of this chapter:

  • Use object as singleton or storage tool method
  • Class can have a companion object with the same name
  • Objects can extend classes and traits
  • The apply method of an object is usually used to construct a new instance of the companion class
  • If you don’t want to explicitly define the main method, you can extend the object of App characteristics
  • Enumeration can be achieved by extending the Enumeration object

1. Singleton object

Scala does not have static methods or static fields. Use object syntax results to achieve the same purpose.

object Accounts{
    
    
	private var lastNumber = 0
	def newUniqueNumber() = {
    
    lastNumber += 1;lastName}
}

The object's constructor is called when the object is used for the first time. In this case, Account.newUniqueNumber()it is executed on the first call. If an object has never been used, then its constructor will not be executed.
The object can essentially have all the characteristics of the class, but it cannot provide construction parameters for it.

2. Accompanying objects

In C++ or Java, classes with both instance methods and static methods are used. In Scala, the same goal can be achieved by the companion object of the same name as the class .

class Account{
    
    
	val id = Account.newUniqueNumber()
}

object Account{
    
    
	private var lastNumber = 0
	private def newUniqueNumber() = {
    
    }
}

The class and its companion objects can access each other privately , and they must exist in the same source file . The functional characteristics of the companion object of the class are not within the scope of the class . So looking at the above code, you can find that the prefix is ​​used in the call newUniqueNumberin Account.the class .

3. Objects that extend a class or trait

A objectclass that can extend a class and one or more traits, the result is an object that extends the class that defines the class and traits , and has all the properties given in the object definition.

4.apply method

We usually define and use the apply method of the object. When an expression of the following form is encountered, the apply method will be called:, Object(参数1,参数2,……,参数N)which returns an object of the companion class

Array("Marry","had","a","little","lamb")//不需要构造器就可以创建Array

Note: Array(100)和new Array(100)There is a difference. The former means an array with 100 elements, while the latter means an array with 100 space, which is 100 nulls.

5. Application Object

Every Scala program starts from the main method of an object, the type of this method is

Array[String]=>Unit:
	object Hello{
    
    
		def main(args:Array[String]){
    
    
			println("Hello,World!")
		}
	}

In addition to providing your own main method each time, you can also extend the App characteristics and put the program code into the constructor method body class. If you want to get the command line parameters, you can pass the argsattributes.

6. Enumeration

Scala does not have enumerated types. The standard library provides an Enumeration helper class that can be used to generate enumerations.
Define an object that extends the Enumeration class and initialize all optional values ​​in the enumeration by calling the Value method.

object TrafficLight Color extend Enumeration{
    
    
	val Red,Yellow,Green = Value
}

Then we can initialize them by calling Value

val Red = Value
val Yellow = Value
val Green = Value

You can also pass ID, name , or both parameters to the Value method :

val Red = Value(0,"stop")
val Yellow = Value(10)
val Green = Value("Go")

Guess you like

Origin blog.csdn.net/Cxf2018/article/details/109509885