隐式转换,面向切面重构

This commit is contained in:
2019-01-03 11:21:03 +08:00
parent 58da613750
commit 790f45d25b
3 changed files with 22 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
package top.fjy8018.scala
import ImplicitAspect._
/**
* 隐式转换
*
@@ -8,8 +10,8 @@ package top.fjy8018.scala
*/
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")
man.fly()

View 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)
}

View File

@@ -3,7 +3,7 @@ package top.fjy8018.scala
import java.io.File
import scala.io.{BufferedSource, Source}
import ImplicitAspect._
/**
* 隐式转换样例2
*
@@ -12,8 +12,8 @@ import scala.io.{BufferedSource, Source}
*/
object RichFileApp extends App {
// 目的在于为Java IO类增加读取文件内容方法
implicit def file2RichFile(file: File): RichFile = new RichFile(file)
// // 目的在于为Java IO类增加读取文件内容方法
// implicit def file2RichFile(file: File): RichFile = new RichFile(file)
val f = new File("tmp/hello.txt")
println(f.readConent)