diff --git a/src/main/scala/top/fjy8018/scala/ImplicitApp.scala b/src/main/scala/top/fjy8018/scala/ImplicitApp.scala index b8f749b..85ced79 100644 --- a/src/main/scala/top/fjy8018/scala/ImplicitApp.scala +++ b/src/main/scala/top/fjy8018/scala/ImplicitApp.scala @@ -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() diff --git a/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala b/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala new file mode 100644 index 0000000..9450243 --- /dev/null +++ b/src/main/scala/top/fjy8018/scala/ImplicitAspect.scala @@ -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) +} diff --git a/src/main/scala/top/fjy8018/scala/RichFileApp.scala b/src/main/scala/top/fjy8018/scala/RichFileApp.scala index 7ae9e6f..4e7d78d 100644 --- a/src/main/scala/top/fjy8018/scala/RichFileApp.scala +++ b/src/main/scala/top/fjy8018/scala/RichFileApp.scala @@ -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)