隐式转换,面向切面重构
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package top.fjy8018.scala
|
package top.fjy8018.scala
|
||||||
|
|
||||||
|
import ImplicitAspect._
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 隐式转换
|
* 隐式转换
|
||||||
*
|
*
|
||||||
@@ -8,8 +10,8 @@ package top.fjy8018.scala
|
|||||||
*/
|
*/
|
||||||
object ImplicitApp extends App {
|
object ImplicitApp extends App {
|
||||||
|
|
||||||
// 定义隐式转换函数
|
// // 定义隐式转换函数
|
||||||
implicit def man2superman(man: Man): Superman = new Superman(man.name)
|
// implicit def man2superman(man: Man): Superman = new Superman(man.name)
|
||||||
|
|
||||||
val man = new Man("FJY")
|
val man = new Man("FJY")
|
||||||
man.fly()
|
man.fly()
|
||||||
|
|||||||
15
src/main/scala/top/fjy8018/scala/ImplicitAspect.scala
Normal file
15
src/main/scala/top/fjy8018/scala/ImplicitAspect.scala
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package top.fjy8018.scala
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* F嘉阳
|
||||||
|
* 2019-01-03 11:19
|
||||||
|
*/
|
||||||
|
object ImplicitAspect {
|
||||||
|
// 定义隐式转换函数
|
||||||
|
implicit def man2superman(man: Man): Superman = new Superman(man.name)
|
||||||
|
|
||||||
|
// 目的在于为Java IO类增加读取文件内容方法
|
||||||
|
implicit def file2RichFile(file: File): RichFile = new RichFile(file)
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ package top.fjy8018.scala
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
import scala.io.{BufferedSource, Source}
|
import scala.io.{BufferedSource, Source}
|
||||||
|
import ImplicitAspect._
|
||||||
/**
|
/**
|
||||||
* 隐式转换样例2
|
* 隐式转换样例2
|
||||||
*
|
*
|
||||||
@@ -12,8 +12,8 @@ import scala.io.{BufferedSource, Source}
|
|||||||
*/
|
*/
|
||||||
object RichFileApp extends App {
|
object RichFileApp extends App {
|
||||||
|
|
||||||
// 目的在于为Java IO类增加读取文件内容方法
|
// // 目的在于为Java IO类增加读取文件内容方法
|
||||||
implicit def file2RichFile(file: File): RichFile = new RichFile(file)
|
// implicit def file2RichFile(file: File): RichFile = new RichFile(file)
|
||||||
|
|
||||||
val f = new File("tmp/hello.txt")
|
val f = new File("tmp/hello.txt")
|
||||||
println(f.readConent)
|
println(f.readConent)
|
||||||
|
|||||||
Reference in New Issue
Block a user