What is an implicit conversion?
We need a method in a class that does not provide such a method, so we need an implicit conversion to a class that provides the method, which becomes an enhancement of the original class. A class + methods that the class does not have -> enhanced class.
For example, an ordinary person has basic methods such as eating and sleeping. When an ordinary person like people wants to become a beefcake one day, according to our previous thinking, we should add a method of muscleMan for the ordinary person or inherit the method of beefcake to get beefcake. However, this implementation is not suitable for some special scenarios, such as:
- 1) If the stud class is black box, how do you do it for ordinary people
people
How about adding an existing stud method? - 2) If you use inheritance too often, do you ignore the principles of development? High cohesion, low coupling?
So implicit is introduced here, and it’s a step-by-step guide to its appeal.
The implementation of implicit conversion
- 1) Mark a method or type as implicit
- 2) Flag classes as implicit
- 3) Mark the method’s argument list as implicit (this is not commonly used)
Mark the method as implicit
Since Biliking is a new object from the Pepole class, it only eats and drinks, but is not a beefcake
object Myimplicit {
def main(args: Array[String]): Unit = {
// King Billy was once an ordinary man
val BiliKing = new People("King Billy")
BiliKing.
}
}
class People(val name: String){
def eat(a):Unit = {
println(s"$name has eaten")}def sleep(a):Unit = {
println(s$name is sleeping)}}Copy the code
What we need now is an implicit transformation to get Billy the way to be a stud
// Put the ordinary person in and return SuperMan
implicit def peopleToSuperMan(people: People):SuperMan = {
new SuperMan(people.name)
}
Copy the code
Final code:
object Myimplicit {
def main(args: Array[String]): Unit = {
// King Billy was once an ordinary man
val BiliKing = new People("King Billy")
// Put the ordinary person in and return SuperMan
implicit def peopleToSuperMan(people: People):SuperMan = {
new SuperMan(people.name)
}
// With implicit conversion, all objects created from Peope will have methods of muscleMan(), and Billy will become a beefy guy
BiliKing.muscleMan()
}
}
class People(val name: String){
def eat(a):Unit = {
println(s"$name has eaten")}def sleep(a):Unit = {
println(s$name is sleeping)}}class SuperMan(val name: String){
def muscleMan(a):Unit = {
println(s"$Name becomes a big beefcake")}}Copy the code
Mark the class as implicit
object Myimplicit {
def main(args: Array[String]): Unit = {
// King Billy was once an ordinary man
val BiliKing = new People("King Billy")
BiliKing.muscleMan()
}
// With implicit conversion, all objects created from Peope will have methods of muscleMan(), and Billy will become a beefy guy
implicit class ChangeMuscleMan(people: People){
def muscleMan(a):Unit = {
println(s${people. Name} becomes a monster)}}}class People(val name: String){
def eat(a):Unit = {
println(s"$name has eaten")}def sleep(a):Unit = {
println(s$name is sleeping)}}Copy the code
Mark the type as implicit
Using int as an example, we can augment the addition, subtraction, and multiplication method aSM for all int types
object Myimplicit {
def main(args: Array[String]): Unit = {
1.aSM("add".1)
10.aSM("sub".1)}// Define an implicit conversion of type int
implicit class AddSubXAvg(x:Int){
// Pattern matching
def aSM(input:String ,value:Long):Long = input match {
case "add" => x + value
case "sub" => x + value
case "X" => x + value
}
}
}
Copy the code
Flag the method’s parameter list as implicit
By default, this parameter is mandatory and an error will be reported if it is not specified
object Myimplicit {
def main(args: Array[String]): Unit = {
val yy = new People("YY elder brother")
// yy.eat
implicit val lala = 2
yy.eat
}
}
class People(val name: String){
def eat(implicit eatHowMuch:Int):Unit = {
println(s$name ate the $eatHowMuch bowl)}}Copy the code
Note: If multiple ints are implicit, the application will not respond. So only one variable value can be converted implicitly
Abuse of implicit conversions
It is recommended to make a separate implicit conversion section, all implicit conversions are placed in the class, all of the following implicit conversions are inimplicitGuiDang
In the. Where do you want to use it, import it in the class you want to use it inimplicitGuiDang
Can be
object implicitGuiDang{
implicit class AddSubXAvg(x:Int){
// Pattern matching
def aSM(input:String ,value:Long):Long = input match {
case "add" => x + value
case "sub" => x + value
case "X" => x + value
}
}
}
Copy the code
The scope of implicit conversions
- In some cases, implicit conversions need to be at the top of a code block
- Implicit conversions can be defined in associated objects, and implicit conversions are to be found in associated objects of related classes, such as pepole and the stud class