Copyright statement: This article is the original article for bloggers, without permission from the blogger.
1, Scala defines the type of the two ways
2, path dependence in Scala
3, Scala in their own types and their own types of restrictions
4, dependency injection in Scala
5, Scala chain style call - the use of -Type mechanism
6, Scala composite data type
7. Abstract types in Scala
8, type parameters in Scala
9, type constraints in Scala
In 10, Scala, inverter, covariant variable
11, the type of structure in Scala
|1, Scala defines the type of the two ways|
In Scala, you can define the type in two ways:
Defining classes, attributes, or objects
Define the type directly with the type keyword
In Scala, you can use the name of the class and the name of the character to refer to the type of the type directly. To quote the type of the object, you need to use the type member of the object to refer to the type.
|2, path dependence in Scala|
In Scala, the path dependence of the object is different, its type is not the same, the path dependence is different, resulting in different types.
The path is not a type, but a similar position.
|3, Scala in their own types and their own types of restrictions|
Each class itself has a this pointer, through the this pointer can refer to its own instance; in its own type, self is not a keyword, is the this alias, with a stronger readability.
In the Scala, can be in the class, the characteristics of the Object in their own type of this constraints, through their own types of restrictions, can be very good to limit our type and behavior.
1, limit the creation of S2 objects, must be mixed with the characteristics of S1
2, S2 subclasses must also be mixed with S1 this trait
|4, dependency injection in Scala|
The dependency injection in Scala is accomplished by means of its own type constraints, which can be used to carry the attributes and methods in the class
Scala dependency injection can be seen as a mixture of Scala among the third ways
Through the dependency injection in Scala, we can make use of the attributes and methods in the class to carry out the same function.
From the above procedures can be seen that the powerful conversion function.
|5, Scala chain style call - the use of -Type mechanism|
In the Scala, we can do the chain style programming, because we use the type mechanism, which is called a singleton type of way, some of the method calls in series
In Scala, any class object has a type property, which may be either a class or an object, or an empty value, in type.
This.type is a mechanism provided by Scala that points to the type of the current object.
|6, Scala composite data type|
The composite data type in Scala is the specific type constructed by combining other types, and the new type is called the compound data type
The A type is a composite data type, both B and C is a sub type of D, that is, the requirements of both A
B, but also has C, but also has the function of D. The syntax of Java is different from this.
|7. Abstract types in Scala|
In Scala, the type keyword is used to define an abstract type, and an abstract type is a type that does not specify a specific type
In Scala, because sometimes the type parameter grammar is too complex, we can use the abstract type as far as possible
Sample program 1:
Sample program 2:
|8, type parameters in Scala|
In Scala, the type parameter is in general use before the parameters defined in brackets , then the common parameters can be used as a parameter type parameter type, simple type parameter definition method:
Type parameters are very much like the method parameters, but they are parameterized at compile time
|9, type constraints in Scala|
In Scala, the constraints of the type variables are divided into two types: the upper bound of the type variable and the lower bound of the type variable. By defining the type variables, we can easily express the types of variables with certain characteristics and methods
Definition of upper bounds of type variables:
Lower bounds for type variables:
In Scala, the maximum upper bound of all types is Any, and the lower bound is Nothing.
Because all of the types in Scala are inherited from Any, and all types are inherited by Nothing
|10, Scala type variable, contravariant and covariant|
The type variable refers to the ability parameters of high-order types like T[A] can change or change the type variable has three forms: constant (Invariance) and the covariance (Covariance), inverter (Contravariance). The covariant refers to the type parameters to replace the ability of the superclass, i.e. compromise inverter change. Refers to the type parameters to replace the ability of its subclasses, and change of guards.
If a type parameter of a covariant or contravariant support, is called the type parameter is Variance (variable), otherwise known as immutable.
A mutable generic type does not inherit from a class, and if the parent class is declared to be mutable, the subclass needs to be declared as a mutable type again if you want to keep the variable type
For the function, the function parameter type is inverse or invariant, and the return type of the function is covariant or constant.
In general, when the fault with covariant and contravariant methods in the time, the usual solution is to introduce a new type of parameters, with the introduction of the new type parameters in the method signature.
|11, the type of structure in Scala|
The structure type is about a set of abstractions or fields or types of specifications, these abstract methods or fields or types of parameters we passed must have: parameter or object passed must have abstract methods or fields or types of corresponding.
In the Scala structure type, we do not care about the type of the object to be passed, we have only one requirement for the parameters or objects to be passed.
If you have any questions, please comment!
- step on
- Previous articleIntroduction to ZooKeeper notes - Architecture and Application
- Next articleLarge data related diagram
Reference knowledge base
- Guess you are looking for