Home> Java> javaTutorial> body text

How to use implicit conversion in Java Scala

WBOY
Release: 2023-05-14 16:46:06
forward
834 people have browsed it

Implicit conversion is when the Scala compiler performs type matching. If a suitable type cannot be found, implicit conversion will allow the compiler to automatically deduce the appropriate type within the scope.

1. Implicit values and implicit parameters

The implicit value refers to adding implicit in front of the parameter when defining it. Implicit parameters mean that when defining a method, some parameters in the method are modified by implicit [currying must be used, and the implicit parameters must be written in parentheses at the end]. The function of implicit conversion is: when calling a method, you do not have to manually pass in the implicit parameters in the method. Scala will automatically find the implicit value in the scope and pass it in automatically.

Implicit values and implicit parameters need to pay attention to the following points:
1. The implicit value of the same type of parameter can only appear once in the scope, and multiple definitions cannot be defined in the same scope. implicit values of the same type.

2. The implicit keyword must be placed at the beginning of the implicit parameter definition

3. When a method has only one parameter that is an implicit conversion parameter, the parameter modified by the implicit keyword can be directly defined , just create the type directly without passing in parameters when calling.

4. If a method has multiple parameters, to achieve implicit conversion of some parameters, currying must be used. The implicit keyword appears at the end and can only appear once

package com.bjsxt.scala object ImplicitValue { /** * 隐式值和隐式参数需要注意以下几点: * 1.同类型的参数的隐式值只能在作用域内出现一次,同一个作用域内不能定义多个类型一样的隐式值。 * 2.implicit关键字必须放在隐式参数定义的开头 * 3.一个方法只有一个参数是隐式转换参数时,那么可以直接定义implicit关键字修饰的参数,调用时直接创建类型不传入参数即可。 * 4.一个方法如果有多个参数,要实现部分参数的隐式转换,必须使用柯里化这种方式,隐式关键字出现在后面,只能出现一次 */ // * 4.一个方法如果有多个参数,要实现部分参数的隐式转换, // 必须使用柯里化这种方式,隐式关键字出现在后面,只能出现一次 def Student(age:Int)(implicit name:String,i:Int) = { println(s"student = $name,age = $age,score = $i") } // * 3.一个方法只有一个参数是隐式转换参数时,那么可以直接定义implicit关键字修饰的参数, // 调用时直接创建类型不传入参数即可。 def Teacher(implicit name:String) = { println(s"teacher name is =$name") } def main(args: Array[String]): Unit = { // * 1.同类型的参数的隐式值只能在作用域内出现一次, // 同一个作用域内不能定义多个类型一样的隐式值。比如这里的String、Int类型,各自只能定义一个 implicit val name:String = "zhangsan" implicit val i:Int = 100 //这里使用了隐式参数,只用传入age那个参数,后面两个参数可以不填,默认使用隐式值 Student(100) //这里是直接使用了隐式值,可以不填参数,当然也可以给定参数,给定参数的话就是取给的参数的值 Teacher } }
Copy after login

The output result: the corresponding function will be automatically found and implicit conversion will be performed.

student = zhangsan,age = 100,score = 100 teacher name is =zhangsan
Copy after login

2. Implicit conversion function

The implicit conversion function is a method modified by using the keyword implicit. When Scala is running, suppose that if a variable of type A calls the method() method, and it is found that the variable of type A does not have a method() method, and the variable of type B has this method() method, it will look for any implicit conversion in the scope. The function converts type A into type B. If there is an implicit conversion function, then type A can call method().

Implicit conversion function Note: The implicit conversion function is only related to the parameter type and return type of the function, and has nothing to do with the function name, so there cannot be different names of the same parameter type and return type in the scope Implicit conversion function.

package com.bjsxt.scala class Animal(name:String){ def canFly()= { println(s"$name can fly....") } } class Rabbit(xname:String){ val name = xname } object ImplicitValue2 { //定义隐式转换函数 implicit def RabbitToAnimal(r:Rabbit):Animal = { new Animal(r.name) } //注意隐式转换函数只与参数类型和返回值类型有关, // 与函数名无关,所以不能定义两个相同类型的函数,虽然函数名不同,下面这个就是非法的 // implicit def Rabbit(r:Rabbit):Animal = { // new Animal(r.name) // } def main(args: Array[String]): Unit = { val rabbit = new Rabbit("rabbit") //这里rabbit调用不了canFly,因为Rabbit类中没有canFly方法, // 但定义了隐式转换函数,那就会往Animal类中找 rabbit.canFly() } }
Copy after login

Running results:

rabbit can fly....
Copy after login

3. Implicit classes

Classes modified with the implicit keyword are implicit classes. If a variable A does not have certain methods or variables, and this variable A can call certain methods or certain variables, you can define an implicit class. These methods or variables are defined in the implicit class. In the implicit class Just pass in A.

Note on implicit classes:

1. Implicit classes must be defined in classes, package objects, and companion objects

2. The construction of implicit classes must have only one parameter , Implicit classes with the same type of construction cannot appear in the same class, package object, and companion object.

package com.bjsxt.scala //隐式类 class Rabbit1(xname:String){ val name = xname } object ImplicitValue3 { //定义隐式类,只能在object中定义,并且只能传入一个参数 //不能出现同类型构造的隐式类。(同类型:参数类型和返回值类型,与类名无关) implicit class Animal1(r:Rabbit1){ def showName() = { println(s"${r.name} is Rabbit....")//对象.属性需要加“{}” } } def main(args: Array[String]): Unit = { val rabbit = new Rabbit1("RABBIT") // Rabbit1类中没有showName方法,这是会找隐式类 rabbit.showName() } }
Copy after login

Run result:

RABBIT is Rabbit....
Copy after login

The above is the detailed content of How to use implicit conversion in Java Scala. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!